步骤:
1、在客户端创建一个与服务器端大小一样的空白文件 2、设置子线程的个数 3、计算每个子线程下载的数据块大小和下载起始位置、结束位置 4、创建子线程开始下载数据 5、得到每个子线程都下载完成的标记代码:
MultiThreadDownLoader.java:
ThreadDownLoader.java:
import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class ThreadDownLoader extends Thread { private String path; private int startIndex; private int endIndex; private int threadId; private int threadCount; //正在运行的线程个数 private static int runningThreadCount = 3; ThreadDownLoader(String path,int startIndex,int endIndex,int threadId,int threadCount){ this.path = path; this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; this.threadCount = threadCount; } @Override public void run() { downLoad(path,startIndex,endIndex,threadId,threadCount); } public void downLoad(String path,int startIndex,int endIndex,int threadId,int threadCount) { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(3000); //设置子线程请求数据的范围 conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex); int code = conn.getResponseCode(); if(code == 206){//请求部分数据 InputStream is = conn.getInputStream(); RandomAccessFile file = new RandomAccessFile("temp.exe","rw"); //指定从哪个位置开始写数据 file.seek(startIndex); byte[] buffer = new byte[1024]; int len = -1; while((len = is.read(buffer)) != -1){ file.write(buffer, 0, len); } file.close(); System.out.println("线程"+threadId+"下载完成..............."); synchronized (ThreadDownLoader.this) { runningThreadCount--; if(runningThreadCount == 0){ System.out.println("文件下载完成..............."); } } } } catch (Exception e) { System.out.println("线程"+threadId+"下载失败..............."); e.printStackTrace(); } } } 断点续传下载1、实时记录线程下载的位置;
2、接上一次下载的位置继续下载;
代码:
import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import java.nio.channels.ReadableByteChannel; public class ThreadDownLoader extends Thread { private String path; private int startIndex; private int endIndex; private int threadId; private int threadCount; //正在运行的线程个数 private static int runningThreadCount = 3; ThreadDownLoader(String path,int startIndex,int endIndex,int threadId,int threadCount){ this.path = path; this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; this.threadCount = threadCount; } @Override public void run() { downLoad(path,startIndex,endIndex,threadId,threadCount); } public void downLoad(String path,int startIndex,int endIndex,int threadId,int threadCount){ try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(3000); //获取上一次下载的位置,接着下载 File threadFile = new File(threadId+".txt"); if(threadFile.exists() && threadFile.length() > 0){ FileReader fr = new FileReader(threadFile); BufferedReader br = new BufferedReader(fr); String position = br.readLine(); //设置子线程请求数据的范围 conn.setRequestProperty("Range", "bytes="+position+"-"+endIndex); startIndex = Integer.valueOf(position); }else{ //设置子线程请求数据的范围 conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex); } int code = conn.getResponseCode(); if(code == 206){//请求部分数据 InputStream is = conn.getInputStream(); RandomAccessFile file = new RandomAccessFile("temp.exe","rw"); //指定从哪个位置开始写数据 file.seek(startIndex); byte[] buffer = new byte[1024*1024]; int len = -1; int total = 0; while((len = is.read(buffer)) != -1){ file.write(buffer, 0, len); total = total + len; int currentPosition = startIndex + total; //能够即时的把数据写到数据 RandomAccessFile f = new RandomAccessFile(threadId+".txt", "rwd"); f.write((currentPosition+"").getBytes()); f.close(); } file.close(); System.out.println("线程"+threadId+"下载完成..............."); synchronized (ThreadDownLoader.this) { runningThreadCount--; if(runningThreadCount == 0){ System.out.println("文件下载完成..............."); } } } } catch (Exception e) { System.out.println("线程"+threadId+"下载失败..............."); e.printStackTrace(); } } } 多线程下载移植到Android项目上(重点)