当年一个简单可用的多线程断点续传类
代码如下:
package com; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; /** *这个类已经实现了多线程断点下载的功能,每次运行都会从进度文件中读取进度接着下,下完了会把进度文件删除掉 */ public class MultiDownload { static int threadCount = 3; static int finishedThread = 0; //这个地址对吗 static String path = "http://192.168.3.14:8080/EasyDownload/download.do"; public static void main(String[] args) { try{ URL url = new URL(path); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET");//get方式可以下载吗? conn.setConnectTimeout(5000); conn.setReadTimeout(5000); if(conn.getResponseCode()==200){ int length = conn.getContentLength(); File file = new File("a.exe"); RandomAccessFile raf = new RandomAccessFile(file,"rwd"); raf.setLength(length); raf.close(); int size = length/MultiDownload.threadCount; for(int i=0;i<threadCount;i++){ int startIndex = i*size; int endIndex = (i+1)*size-1; if(i==threadCount-1){ endIndex = length-1; } new DownloadThread(startIndex, endIndex, i).start(); } } }catch(Exception e){ e.printStackTrace(); } } } class DownloadThread extends Thread{ int startIndex; int endIndex; int threadId; public DownloadThread(int startIndex, int endIndex, int threadId) { super(); this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; } @Override public void run() { int total = 0; //再次发送请求,下载 try{ File progressFile = new File(threadId+".txt"); if(progressFile.exists()){//如果存在txt文件 FileInputStream fis = new FileInputStream(progressFile); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); int numberInTxt= Integer.parseInt(br.readLine()); startIndex +=numberInTxt; total = numberInTxt;//total必须要在这里给它赋值,不然就不对了 fis.close(); br.close(); } System.out.println(threadId+"开始:"+startIndex+"结束:"+endIndex); URL url = new URL(MultiDownload.path); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET");//get方式可以下载吗? conn.setConnectTimeout(5000); conn.setReadTimeout(5000); //看好了!!!!!!!!!!!!!!!!!!!!!!!!!!!!! conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex); if(conn.getResponseCode()==206){ InputStream is = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; File file = new File("a.exe"); RandomAccessFile raf = new RandomAccessFile(file,"rwd"); raf.seek(startIndex);//这句很重要!!!!!!!!!!!!!! while((len = is.read(b))!=-1){ raf.write(b,0,len); total+=len; if(total%1000 == 0){ System.out.println("线程"+threadId+"下载了"+total); } //生成临时文件 RandomAccessFile progressRaf = new RandomAccessFile(progressFile,"rwd"); progressRaf.write((""+total).getBytes()); progressRaf.close(); } System.out.println("线程"+threadId+"下载完毕--------------"); raf.close(); MultiDownload.finishedThread++; synchronized (MultiDownload.path) { if(MultiDownload.finishedThread == MultiDownload.threadCount){ for(int i=0;i<MultiDownload.threadCount;i++){ File f = new File(i+".txt"); f.delete(); } MultiDownload.finishedThread = 0; } } } }catch(Exception e){ e.printStackTrace(); } } }