爷的眼睛闪亮
insideDotNet En_summerGarden

java的断点续传是基于之前java文件下载基础上的功能拓展

首先设置一个以线程ID为名的下载进度文件,

每一次下载的进度会保存在这个文件中,下一次下载的时候,会根据进度文件里面的内容来判断下载的进度。

  1. package com.ldw.multilthreaddownload;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.InputStream;  
  7. import java.io.InputStreamReader;  
  8. import java.io.RandomAccessFile;  
  9. import java.net.HttpURLConnection;  
  10. import java.net.URL;  
  11.   
  12. public class Multidownload {  
  13.   
  14.     static int ThreadCount = 3;   //线程的个数  
  15.     static int finishedThread = 0;   //初始化下载完成的线程的个数  
  16.     static String path = "http://192.168.0.102:8080/QQ.exe";  //确定下载地址  
  17.     public static void main(String[] args) {  
  18.         // TODO Auto-generated method stub  
  19.           
  20.         //发送get请求,请求这个地址的资源  
  21.         try {  
  22.             URL url = new URL(path);  
  23.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  24.             conn.setRequestMethod("GET");  
  25.             conn.setConnectTimeout(5000);  
  26.             conn.setReadTimeout(5000);  
  27.             if(conn.getResponseCode() == 200){  
  28.                 //获取到请求资源文件的长度  
  29.                 int length = conn.getContentLength();  
  30.                 File file = new File("QQ.exe");  
  31.                 //创建随机存储文件  
  32.                 RandomAccessFile raf = new RandomAccessFile(file, "rwd");  
  33.                 //设置临时文件的大小  
  34.                 raf.setLength(length);  
  35.                 //关闭raf  
  36.                 raf.close();  
  37.                 //计算出每一个线程下载多少字节  
  38.                   
  39.                 int size = length / Multidownload.ThreadCount;  
  40.                   
  41.                 for(int i = 0; i < Multidownload.ThreadCount; i ++){  
  42.                     //startIndex,endIndex分别代表线程的开始和结束位置  
  43.                     int startIndex = i * size;  
  44.                     int endIndex = (i + 1) * size - 1;  
  45.                     if(i == ThreadCount - 1){  
  46.                         //如果是最后一个线程,那么结束位置写死  
  47.                         endIndex = length -1;  
  48.                     }  
  49.                     //System.out.println("线程" + i + "的下载区间是" + startIndex + "到" + endIndex);  
  50.                     new DownLoadThread(startIndex, endIndex, i).start();  
  51.                 }  
  52.             }  
  53.         } catch (Exception e) {  
  54.             // TODO Auto-generated catch block  
  55.             e.printStackTrace();  
  56.         }  
  57.   
  58.     }  
  59.   
  60. }  
  61.   
  62. class DownLoadThread extends Thread{  
  63.     int startIndex;  
  64.     int endIndex;  
  65.     int threadId;  
  66.       
  67.     public DownLoadThread(int startIndex, int endIndex, int threadId) {  
  68.         super();  
  69.         this.startIndex = startIndex;  
  70.         this.endIndex = endIndex;  
  71.         this.threadId = threadId;  
  72.     }  
  73.   
  74.     @Override  
  75.     public void run(){  
  76.         //使用http请求下载安装包文件  
  77.         URL url;  
  78.         try {  
  79.             File fileProgress = new File(threadId + ".txt");  
  80.             //判断存储下载进度的临时文件是否存在,  
  81.             if(fileProgress.exists()){  
  82.                 FileInputStream fis = new FileInputStream(fileProgress);  
  83.                 BufferedReader br = new BufferedReader(new InputStreamReader(fis));  
  84.                 //从下载进度的临时文件中读取上一次下载的总进度,然后和原来文本的开始位置相加,得到新的下载位置  
  85.                 startIndex += Integer.parseInt(br.readLine());  
  86.                 fis.close();  
  87.             }  
  88.             System.out.println("线程" + threadId + "下载区间是" + startIndex +"====" + endIndex);  
  89.             url = new URL(Multidownload.path);  
  90.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  91.             conn.setRequestMethod("GET");  
  92.             conn.setConnectTimeout(5000);  
  93.             conn.setReadTimeout(5000);  
  94.             //设置请求数据的区间  
  95.             conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);  
  96.             //请求部分数据的响应码是206  
  97.             if(conn.getResponseCode() == 206){  
  98.                 //获取一部分数据来读取  
  99.                 InputStream is = conn.getInputStream();  
  100.                 byte[] b = new byte[1024];  
  101.                 int len = 0;  
  102.                 int total = 0;  
  103.                 //拿到临时文件的引用  
  104.                 File file = new File("QQ.exe");  
  105.                 RandomAccessFile raf = new RandomAccessFile(file, "rwd");  
  106.                 //更新文件的写入位置,startIndex  
  107.                 raf.seek(startIndex);  
  108.                 while((len = is.read(b)) != -1 ){  
  109.                     //每次读取流里面的数据,同步吧数据写入临时文件  
  110.                     raf.write(b, 0, len);  
  111.                     total += len;  
  112.                     //System.out.println("线程" + threadId + "下载了" + total);  
  113.                       
  114.                     //生成一个专门记录下载进度的临时文件  
  115.                     //File fileProgress = new File(threadId + ".txt");  
  116.                     RandomAccessFile fileProgressraf = new RandomAccessFile(fileProgress, "rwd");  
  117.                     //每一次读取流里面的数据以后,把当前线程下载的总进度写入临时文件中  
  118.                     fileProgressraf.write((total + "").getBytes());  
  119.                     fileProgressraf.close();  
  120.                       
  121.                 }  
  122.                 System.out.println("线程" + threadId + "下载过程结束===========================");  
  123.                 raf.close();  
  124.                 //三条线程下载完成以后,清理临时文件  
  125.                 Multidownload.finishedThread++;  
  126.                 //线程安全  
  127.                 synchronized(Multidownload.path){  
  128.                     if(Multidownload.finishedThread == Multidownload.ThreadCount){  
  129.                         for(int i = 0; i < Multidownload.ThreadCount; i++){  
  130.                             File filefinish = new File(i + ".txt");  
  131.                             filefinish.delete();  
  132.                               
  133.                         }  
  134.                         Multidownload.finishedThread = 0;  
  135.                     }  
  136.                 }  
  137.             }  
  138.         } catch (Exception e) {  
  139.             // TODO Auto-generated catch block  
  140.             e.printStackTrace();  
  141.         }  
  142.           
  143.     }  
  144. }  
posted on 2018-04-08 16:10  爷的眼睛闪亮  阅读(320)  评论(0编辑  收藏  举报