Android 断点下载

其实写起来很简单 没什么难度 网上的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值