android4.0从服务器下载apk时,HttpURLConnection报java.io.FileNotFoundException: http://***

系统:android  4.0.1

用下面代码实现断点续传下载

url = new URL(downLoadPath);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setConnectTimeout(5 * 1000);
http.setRequestMethod("GET");
http.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
 http.setRequestProperty("Accept-Language", "zh-CN");
http.setRequestProperty("Referer", url.toString()); 
http.setRequestProperty("Charset", "UTF-8");


http.setRequestProperty("Range", "bytes=" + downLength + "-" + block);// 设置获得实体数据的范围,downLength是文件已经下载的长度,block是下载文件的总长度
 InputStream isStream = http.getInputStream();
 byte[] buffer = new byte[1024];
 int offset = 0; 
RandomAccessFile threadFile = new RandomAccessFile(saveFile, "rwd");
threadFile.seek(downLength); 

while (flag && (offset = isStream.read(buffer, 0, 1024)) != -1) {
          threadFile.write(buffer, 0, offset);
          downLength += offset; 
          Log.e("WEH", "downLength is:" + downLength);
          fileServer.update(downLoadPath, downLength); //将下载位置更新到数据库中

运行时,上面代码的InputStream isStream = http.getInputStream();会抛出java.io.FileNotFoundException: http://***异常

解决办法:把http.setRequestProperty("Range", "bytes=" + downLength + "-" + block);修改为http.setRequestProperty("Range", "bytes=" + downLength + "-" );即可

修改后,效果是一样的,因为:当block省略时,默认是下载到文件的结尾。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值