url 断点续传
来源:互联网 发布:程序员培训课程付费 编辑:程序博客网 时间:2024/06/08 07:14
通过url下载文件,中间服务器可能中断,需要断点续传 private void copyUrlPartly( URL url) throws IOException { List<PartETag> partETags = new ArrayList<PartETag>(); long totalUplod = 0L; int index = 1; long remain = fileSize; int retry = 0; HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); long fileSize = httpURLConnection.getContentLengthLong(); /*如果下载的size小于size那么,就重新请求连接*/ while (totalUplod < fileSize && retry < 5) { byte readBuffer[] = new byte[1024]; HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestProperty("RANGE", "bytes="+totalUplod+"-"); httpURLConnection.connect(); InputStream stream = httpURLConnection.getInputStream(); BufferedInputStream bis = new BufferedInputStream(stream); if(remain <= 0) { break; } while (remain > 0) { long partSize = 0L; File tmpFile = null; try { /* 使用临时文件作为中转 */ tmpFile = File.createTempFile( "test", null); /* 将part拷贝到临时文件夹下面的临时文件 */ partSize = downloadPart(bis, readBuffer, tmpFile); // 在这可以对tmpFile处理你 省略不写 remain -= partSize; totalUplod += partSize; retry = 0; } catch (Exception e) { retry++; logger.error("copyUrlPartly failed. ", e); break; } finally { if (null != tmpFile) { tmpFile.delete(); } } } } }//将网上下载部分保存到临时文件tmpFile中private long downloadPart(BufferedInputStream bis, byte[] buffer, File tmpFile) throws Exception { BufferedOutputStream bos = null; long total = 0; try { bos = new BufferedOutputStream(new FileOutputStream(tmpFile)); int count; while ((count = bis.read(buffer)) > 0) { total += count; bos.write(buffer, 0, count); } } finally { try { if (null != bos) { bos.close(); } } catch (Exception e) { logger.debug("IoCloseException", e); } } return total; }Range头域 Range头域可以请求实体的一个或者多个子范围。例如, 表示头500个字节:bytes=0-499 表示第二个500字节:bytes=500-999 表示最后500个字节:bytes=-500 表示500字节以后的范围:bytes=500- 第一个和最后一个字节:bytes=0-0,-1 同时指定几个范围:bytes=500-600,601-999
0 0
- url 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- 断点续传
- JVM类加载机制1
- tools:context=".MainActivity的作用
- android 布局文件中控件ID、name标签属性的命名包含“@”、“.”、“+”等等符号的含义
- jquery对combo和radio的操作
- JVM性能调优
- url 断点续传
- Eclipse快捷键 10个最有用的快捷键
- 第一部分 走进Java
- Android ActionBar使用方法
- struts2的应用
- 登录页
- (29):优先考虑类型安全的异构容器
- Android UI开发第二十四篇——Action Bar
- jquery写的一个分页grid(非jqGrid插件)