java实现文件的断点续传的下载

时间:2021-05-07 14:36:27

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

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

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

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