Android 断点下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013148839/article/details/78840449

其实写起来很简单 没什么难度 网上的demo 比较多都是用数据库 自己根据文件大小来判断是否下载完成

Demo 下载地址 有时间在更新 完善点 https://github.com/huangqiqiang/Breakpoint

   URL url = new URL(mFileInfo.getUrl());
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(3000);

            //从上次下载完成的地方下载
            int start = mFileInfo.getFinished();
            //设置下载位置(从服务器上取要下载文件的某一段)
            connection.setRequestProperty("Range", "bytes=" + start + "-" + mFileInfo.getLength());//设置下载范围
            //设置文件写入位置
            File file = new File(BreakpointManger.FILE_PATH, mFileInfo.getFileName());
            rwd = new RandomAccessFile(file, "rwd");
            //从文件的某一位置开始写入
            rwd.seek(start);
            finished += mFileInfo.getFinished();

这里写图片描述

重复的字符串 还是写成key 比较好 免得 写错 或者 复制成空格 养成好习惯
遇到的错误 比较棘手 IO流没有正常关闭 或者异常 退出 或者在debug 版本 上 会出现

java.lang.IllegalStateException: Cannot set request property after connection is made error when setRequestProperty method is called after url.openConnection();

https://stackoverflow.com/questions/22320418/setrequestproperty-throwing-java-lang-illegalstateexception-cannot-set-request

猜你喜欢

转载自blog.csdn.net/u013148839/article/details/78840449