美文网首页
文件断点续传

文件断点续传

作者: 流水潺湲 | 来源:发表于2019-01-15 11:12 被阅读9次
     /**
         * 单线程支持断点续传下载
         * 断点续传
         */
        public static void downloadFile(final DownLoadProgressListener loadProgressListener) {
            Runnable runnable = new Runnable() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void run() {
                    try {
                        URL url = new URL("https://codeload.github.com/yanzhenjie/Sofia/zip/master");
                        HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
                        // 设置User-Agent
                        httpConnection.setRequestProperty("User-Agent", "NetFox");
                        //                    setHeader(httpConnection);
                        long bytes = AppConfig.getInstance().getLong("length", 0L);
                        // 得到content的长度
    
                        // 设置断点续传的开始位置
                        httpConnection.setRequestProperty("Range", "bytes=" + bytes + "-");
                        long contentLength = httpConnection.getContentLengthLong();
                        Log.i(TAG, "---" + "bytes=" + bytes + "-" + contentLength);
                        // 获得输入流
                        InputStream input = httpConnection.getInputStream();
                        File file = FileUtils.getLocalFile("Sofia.zip");
                        // 采用IO包中的RandAccessFile类。
                        RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
                        long position = bytes;
                        //定位文件指针到nPos位置
                        accessFile.seek(position);
                        byte[] buffer = new byte[1024];
                        long total = position;
                        int statPosition = Integer.valueOf(position + "");
                        int len = -1;
                        long sTime = System.currentTimeMillis();
                        long eTime = 0;
                        int p = 0;
                        //跳过已经读取过的
                        long skipL = input.skip(position);
                        Log.i(TAG, " nStartPos --> " + position + ", skipL = " +
                                skipL);
                        //                    skipBytesFromStream(input, position);
                        //从输入流中读入字节流,然后写到文件中
                        while ((len = input.read(buffer, 0, 1024)) > 0) {
                            accessFile.write(buffer, 0, len);
                            total = total + len;
                            //记录文件日志长度
                            AppConfig.getInstance().putLong("length", total);
    
                            eTime = System.currentTimeMillis();
                            if (eTime - sTime > 200) {
                                //大于500ms 才进行一次监听
                                sTime = eTime;
                                Log.i(TAG, "file---Length:" + total);
                                loadProgressListener.onProgressUpdate(total, contentLength);
                            }
                        }
                        loadProgressListener.onCompleteLoad(contentLength);
                        Log.i(TAG, "file---end:" + total);
                    } catch (IOException e) {
                        e.printStackTrace();
                        loadProgressListener.onError(0);
                    }
    
                }
            };
            AppContext.MAIN_EXECUTOR.execute(runnable);
        }
    
        public interface DownLoadProgressListener {
            void onProgressUpdate(long percent, long length);
    
            void onCompleteLoad(long length);
    
            void onError(int error);
        }
    
    

    相关文章

      网友评论

          本文标题:文件断点续传

          本文链接:https://www.haomeiwen.com/subject/eklbkftx.html