java SE 文件多线程下载,断点续传 原理
2013-08-31 21:56
239 查看
package com.itheima.download; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class TestDownload { private final static String path = "http://192.168.1.251/web/youdao.exe"; public static void main(String[] args) throws Exception { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); int code = conn.getResponseCode(); if (code == 200) { int len = conn.getContentLength(); RandomAccessFile file = new RandomAccessFile(getFilenName(path), "rwd"); // 1.设置本地文件大小跟服务器的文件大小一致 file.setLength(len); // 2 .假设开启3 个线程 int threadnumber = 3; int blocksize = len / threadnumber; /** * 线程1 0~ blocksize 线程2 1*bolocksize ~ 2*blocksize 线程3 2*blocksize ~ * 文件末尾 */ for (int i = 0; i < threadnumber; i++) { int startposition = i * blocksize; int endpositon = (i + 1) * blocksize; if (i == (threadnumber - 1)) { // 最后一个线程 endpositon = len; } DownLoadTask task = new DownLoadTask(i, path, startposition, endpositon); task.start(); } } } public static String getFilenName(String path) { int start = path.lastIndexOf("/") + 1; return path.substring(start, path.length()); } } class DownLoadTask extends Thread { public static final String path = "http://192.168.1.251/web/youdao.exe"; int threadid; String filepath; int startposition; int endpositon; public DownLoadTask(int threadid, String filepath, int startposition, int endpositon) { this.threadid = threadid; this.filepath = filepath; this.startposition = startposition; this.endpositon = endpositon; } @Override public void run() { try { File postionfile = new File(threadid + ".txt"); URL url = new URL(filepath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); System.out.println("线程" + threadid + "正在下载 " + "开始位置 : " + startposition + "结束位置 " + endpositon); if (postionfile.exists()) { FileInputStream fis = new FileInputStream(postionfile); byte[] result = StreamTool.getBytes(fis); int newstartposition = Integer.parseInt(new String(result)); if (newstartposition > startposition) { startposition = newstartposition; } } conn.setRequestProperty("Range", "bytes=" + startposition + "-" + endpositon); conn.setRequestMethod("GET"); conn.setConnectTimeout(50000); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); InputStream is = conn.getInputStream(); RandomAccessFile file = new RandomAccessFile(getFilenName(path), "rwd"); // 设置数据从哪个位置开始写 file.seek(startposition); byte[] buffer = new byte[1024]; int leng = 0; // 代表当前读到的服务器数据的位置,同时这个值是已经存储的位置 int currentPostition = startposition; // 创建一个文件对象 记录当前某个文件的下载地址 while ((leng = is.read(buffer)) != -1) { file.write(buffer, 0, leng); currentPostition += leng;// 每次写完,当前的位置加上leng 持久化到存储设备里 String positon = currentPostition + ""; FileOutputStream fos = new FileOutputStream(postionfile); fos.write(positon.getBytes()); fos.flush(); fos.close(); } file.close(); System.out.println("线程" + threadid + "下载完毕"); System.out.println("线程" + threadid + "开始位置" + startposition + "结束位置" + endpositon); // 当线程下载完毕后,把文件删除掉 if (postionfile.exists()) { postionfile.delete(); } } catch (Exception e) { e.printStackTrace(); } super.run(); } public static String getFilenName(String path) { int start = path.lastIndexOf("/") + 1; return path.substring(start, path.length()); } }
相关文章推荐
- Http 206 文件断点续传下载原理
- ASP.NET文件下载各种方式比较:对性能的影响、对大文件的支持、对断点续传和多线程下载的支持
- HTTP文件断点续传的原理
- Java SE 多线程下载网络上的文件代码+注释
- php 文件下载 断点续传 原理
- ASP.NET文件下载各种方式比较:对性能的影响、对大文件的支持、对断点续传和多线程下载的支持
- http协议 文件下载原理及多线程断点续传
- Http 206 文件断点续传下载原理
- ASP.NET文件下载各种方式比较:对性能的影响、对大文件的支持、对断点续传和多线程下载的支持
- http协议学习-断点续传和多线程下载的实现原理
- HTTP文件断点续传的原理
- [转载]http协议 文件下载原理及多线程断点续传
- 干货,Java多线程下载文件以及断点续传
- http协议学习系列(断点续传和多线程下载的实现原理)
- ASP.NET文件下载各种方式比较:对性能的影响、对大文件的支持、对断点续传和多线程下载的支持
- HTTP文件断点续传的原理
- ASP.NET文件下载各种方式比较:对性能的影响、对大文件的支持、对断点续传和多线程下载的支持
- Http 206 文件断点续传下载原理
- ASP.NET文件下载各种方式比较:对性能的影响、对大文件的支持、对断点续传和多线程下载的支持
- [转]ASP.NET文件下载各种方式比较:对性能的影响、对大文件的支持、对断点续传和多线程下载的支持