本文转载于:http://blog.csdn.net/ibm_hoojo/article/details/6838222

基本原理:利用URLConnection获取要下载文件的长度、头部等相关信息,并设置响应的头部信息。并且通过URLConnection获取输入流,将文件分成指定的块,每一块单独开辟一个线程完成数据的读取、写入。通过输入流读取下载文件的信息,然后将读取的信息用RandomAccessFile随机写入到本地文件中。同时,每个线程写入的数据都文件指针也就是写入数据的长度,需要保存在一个临时文件中。这样当本次下载没有完成的时候,下次下载的时候就从这个文件中读取上一次下载的文件长度,然后继续接着上一次的位置开始下载。并且将本次下载的长度写入到这个文件中。

个人博客:

http://hoojo.cnblogs.com

http://blog.csdn.net/IBM_hoojo

email: hoojo_@126.com

一、下载文件信息类、实体

封装即将下载资源的信息

[java] view plaincopyprint?
  1. package com.hoo.entity;
  2. /**
  3. * <b>function:</b> 下载文件信息类
  4. * @author hoojo
  5. * @createDate 2011-9-21 下午05:14:58
  6. * @file DownloadInfo.java
  7. * @package com.hoo.entity
  8. * @project MultiThreadDownLoad
  9. * @blog http://blog.csdn.net/IBM_hoojo
  10. * @email hoojo_@126.com
  11. * @version 1.0
  12. */
  13. public class DownloadInfo {
  14. //下载文件url
  15. private String url;
  16. //下载文件名称
  17. private String fileName;
  18. //下载文件路径
  19. private String filePath;
  20. //分成多少段下载, 每一段用一个线程完成下载
  21. private int splitter;
  22. //下载文件默认保存路径
  23. private finalstatic String FILE_PATH = "C:/temp";
  24. //默认分块数、线程数
  25. private finalstatic int SPLITTER_NUM =5;
  26. public DownloadInfo() {
  27. super();
  28. }
  29. /**
  30. * @param url 下载地址
  31. */
  32. public DownloadInfo(String url) {
  33. this(url, null, null, SPLITTER_NUM);
  34. }
  35. /**
  36. * @param url 下载地址url
  37. * @param splitter 分成多少段或是多少个线程下载
  38. */
  39. public DownloadInfo(String url, int splitter) {
  40. this(url, null, null, splitter);
  41. }
  42. /***
  43. * @param url 下载地址
  44. * @param fileName 文件名称
  45. * @param filePath 文件保存路径
  46. * @param splitter 分成多少段或是多少个线程下载
  47. */
  48. public DownloadInfo(String url, String fileName, String filePath,int splitter) {
  49. super();
  50. if (url == null ||"".equals(url)) {
  51. throw new RuntimeException("url is not null!");
  52. }
  53. this.url =  url;
  54. this.fileName = (fileName ==null || "".equals(fileName)) ? getFileName(url) : fileName;
  55. this.filePath = (filePath ==null || "".equals(filePath)) ? FILE_PATH : filePath;
  56. this.splitter = (splitter < 1) ? SPLITTER_NUM : splitter;
  57. }
  58. /**
  59. * <b>function:</b> 通过url获得文件名称
  60. * @author hoojo
  61. * @createDate 2011-9-30 下午05:00:00
  62. * @param url
  63. * @return
  64. */
  65. private String getFileName(String url) {
  66. return url.substring(url.lastIndexOf("/") +1, url.length());
  67. }
  68. public String getUrl() {
  69. return url;
  70. }
  71. public void setUrl(String url) {
  72. if (url == null || "".equals(url)) {
  73. throw new RuntimeException("url is not null!");
  74. }
  75. this.url = url;
  76. }
  77. public String getFileName() {
  78. return fileName;
  79. }
  80. public void setFileName(String fileName) {
  81. this.fileName = (fileName ==null || "".equals(fileName)) ? getFileName(url) : fileName;
  82. }
  83. public String getFilePath() {
  84. return filePath;
  85. }
  86. public void setFilePath(String filePath) {
  87. this.filePath = (filePath ==null || "".equals(filePath)) ? FILE_PATH : filePath;
  88. }
  89. public int getSplitter() {
  90. return splitter;
  91. }
  92. public void setSplitter(int splitter) {
  93. this.splitter = (splitter < 1) ? SPLITTER_NUM : splitter;
  94. }
  95. @Override
  96. public String toString() {
  97. return this.url +"#" + this.fileName +"#" + this.filePath +"#" + this.splitter;
  98. }
  99. }
package com.hoo.entity;/*** <b>function:</b> 下载文件信息类* @author hoojo* @createDate 2011-9-21 下午05:14:58* @file DownloadInfo.java* @package com.hoo.entity* @project MultiThreadDownLoad* @blog http://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com* @version 1.0*/
public class DownloadInfo {//下载文件urlprivate String url;//下载文件名称private String fileName;//下载文件路径private String filePath;//分成多少段下载, 每一段用一个线程完成下载private int splitter;//下载文件默认保存路径private final static String FILE_PATH = "C:/temp";//默认分块数、线程数private final static int SPLITTER_NUM = 5;public DownloadInfo() {super();}/*** @param url 下载地址*/public DownloadInfo(String url) {this(url, null, null, SPLITTER_NUM);}/*** @param url 下载地址url* @param splitter 分成多少段或是多少个线程下载*/public DownloadInfo(String url, int splitter) {this(url, null, null, splitter);}/**** @param url 下载地址* @param fileName 文件名称* @param filePath 文件保存路径* @param splitter 分成多少段或是多少个线程下载*/public DownloadInfo(String url, String fileName, String filePath, int splitter) {super();if (url == null || "".equals(url)) {throw new RuntimeException("url is not null!");}this.url =  url;this.fileName = (fileName == null || "".equals(fileName)) ? getFileName(url) : fileName;this.filePath = (filePath == null || "".equals(filePath)) ? FILE_PATH : filePath;this.splitter = (splitter < 1) ? SPLITTER_NUM : splitter;}/*** <b>function:</b> 通过url获得文件名称* @author hoojo* @createDate 2011-9-30 下午05:00:00* @param url* @return*/private String getFileName(String url) {return url.substring(url.lastIndexOf("/") + 1, url.length());}public String getUrl() {return url;}public void setUrl(String url) {if (url == null || "".equals(url)) {throw new RuntimeException("url is not null!");}this.url = url;}public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = (fileName == null || "".equals(fileName)) ? getFileName(url) : fileName;}public String getFilePath() {return filePath;}public void setFilePath(String filePath) {this.filePath = (filePath == null || "".equals(filePath)) ? FILE_PATH : filePath;}public int getSplitter() {return splitter;}public void setSplitter(int splitter) {this.splitter = (splitter < 1) ? SPLITTER_NUM : splitter;}@Overridepublic String toString() {return this.url + "#" + this.fileName + "#" + this.filePath + "#" + this.splitter;}
}

二、随机写入一段文件

[java] view plaincopyprint?
  1. package com.hoo.download;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;
  4. /**
  5. * <b>function:</b> 写入文件、保存文件
  6. * @author hoojo
  7. * @createDate 2011-9-21 下午05:44:02
  8. * @file SaveItemFile.java
  9. * @package com.hoo.download
  10. * @project MultiThreadDownLoad
  11. * @blog http://blog.csdn.net/IBM_hoojo
  12. * @email hoojo_@126.com
  13. * @version 1.0
  14. */
  15. public class SaveItemFile {
  16. //存储文件
  17. private RandomAccessFile itemFile;
  18. public SaveItemFile()throws IOException {
  19. this("",0);
  20. }
  21. /**
  22. * @param name 文件路径、名称
  23. * @param pos 写入点位置 position
  24. * @throws IOException
  25. */
  26. public SaveItemFile(String name,long pos) throws IOException {
  27. itemFile = new RandomAccessFile(name,"rw");
  28. //在指定的pos位置开始写入数据
  29. itemFile.seek(pos);
  30. }
  31. /**
  32. * <b>function:</b> 同步方法写入文件
  33. * @author hoojo
  34. * @createDate 2011-9-26 下午12:21:22
  35. * @param buff 缓冲数组
  36. * @param start 起始位置
  37. * @param length 长度
  38. * @return
  39. */
  40. public synchronizedint write(byte[] buff,int start, int length) {
  41. int i = -1;
  42. try {
  43. itemFile.write(buff, start, length);
  44. i = length;
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. return i;
  49. }
  50. public void close()throws IOException {
  51. if (itemFile != null) {
  52. itemFile.close();
  53. }
  54. }
  55. }
package com.hoo.download;import java.io.IOException;
import java.io.RandomAccessFile;/*** <b>function:</b> 写入文件、保存文件* @author hoojo* @createDate 2011-9-21 下午05:44:02* @file SaveItemFile.java* @package com.hoo.download* @project MultiThreadDownLoad* @blog http://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com* @version 1.0*/
public class SaveItemFile {//存储文件private RandomAccessFile itemFile;public SaveItemFile() throws IOException {this("", 0);}/*** @param name 文件路径、名称* @param pos 写入点位置 position* @throws IOException*/public SaveItemFile(String name, long pos) throws IOException {itemFile = new RandomAccessFile(name, "rw");//在指定的pos位置开始写入数据itemFile.seek(pos);}/*** <b>function:</b> 同步方法写入文件* @author hoojo* @createDate 2011-9-26 下午12:21:22* @param buff 缓冲数组* @param start 起始位置* @param length 长度* @return*/public synchronized int write(byte[] buff, int start, int length) {int i = -1;try {itemFile.write(buff, start, length);i = length;} catch (IOException e) {e.printStackTrace();}return i;}public void close() throws IOException {if (itemFile != null) {itemFile.close();}}
}

这个类主要是完成向本地的指定文件指针出开始写入文件,并返回当前写入文件的长度(文件指针)。这个类将被线程调用,文件被分成对应的块后,将被线程调用。每个线程都将会调用这个类完成文件的随机写入。

三、单个线程下载文件

[java] view plaincopyprint?
  1. package com.hoo.download;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.net.HttpURLConnection;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import com.hoo.util.LogUtils;
  9. /**
  10. * <b>function:</b> 单线程下载文件
  11. * @author hoojo
  12. * @createDate 2011-9-22 下午02:55:10
  13. * @file DownloadFile.java
  14. * @package com.hoo.download
  15. * @project MultiThreadDownLoad
  16. * @blog http://blog.csdn.net/IBM_hoojo
  17. * @email hoojo_@126.com
  18. * @version 1.0
  19. */
  20. public class DownloadFileextends Thread {
  21. //下载文件url
  22. private String url;
  23. //下载文件起始位置
  24. private long startPos;
  25. //下载文件结束位置
  26. private long endPos;
  27. //线程id
  28. private int threadId;
  29. //下载是否完成
  30. private boolean isDownloadOver =false;
  31. private SaveItemFile itemFile;
  32. private staticfinal int BUFF_LENGTH =1024 * 8;
  33. /**
  34. * @param url 下载文件url
  35. * @param name 文件名称
  36. * @param startPos 下载文件起点
  37. * @param endPos 下载文件结束点
  38. * @param threadId 线程id
  39. * @throws IOException
  40. */
  41. public DownloadFile(String url, String name,long startPos, long endPos,int threadId) throws IOException {
  42. super();
  43. this.url = url;
  44. this.startPos = startPos;
  45. this.endPos = endPos;
  46. this.threadId = threadId;
  47. //分块下载写入文件内容
  48. this.itemFile = new SaveItemFile(name, startPos);
  49. }
  50. @Override
  51. public void run() {
  52. while (endPos > startPos && !isDownloadOver) {
  53. try {
  54. URL url = new URL(this.url);
  55. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  56. // 设置连接超时时间为10000ms
  57. conn.setConnectTimeout(10000);
  58. // 设置读取数据超时时间为10000ms
  59. conn.setReadTimeout(10000);
  60. setHeader(conn);
  61. String property = "bytes=" + startPos +"-";
  62. conn.setRequestProperty("RANGE", property);
  63. //输出log信息
  64. LogUtils.log("开始 " + threadId +":" + property + endPos);
  65. //printHeader(conn);
  66. //获取文件输入流,读取文件内容
  67. InputStream is = conn.getInputStream();
  68. byte[] buff =new byte[BUFF_LENGTH];
  69. int length = -1;
  70. LogUtils.log("#start#Thread: " + threadId +", startPos: " + startPos + ", endPos: " + endPos);
  71. while ((length = is.read(buff)) >0 && startPos < endPos && !isDownloadOver) {
  72. //写入文件内容,返回最后写入的长度
  73. startPos += itemFile.write(buff, 0, length);
  74. }
  75. LogUtils.log("#over#Thread: " + threadId +", startPos: " + startPos + ", endPos: " + endPos);
  76. LogUtils.log("Thread " + threadId +" is execute over!");
  77. this.isDownloadOver =true;
  78. } catch (MalformedURLException e) {
  79. e.printStackTrace();
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. } finally {
  83. try {
  84. if (itemFile !=null) {
  85. itemFile.close();
  86. }
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. }
  92. if (endPos < startPos && !isDownloadOver) {
  93. LogUtils.log("Thread " + threadId  +" startPos > endPos, not need download file !");
  94. this.isDownloadOver =true;
  95. }
  96. if (endPos == startPos && !isDownloadOver) {
  97. LogUtils.log("Thread " + threadId  +" startPos = endPos, not need download file !");
  98. this.isDownloadOver =true;
  99. }
  100. }
  101. /**
  102. * <b>function:</b> 打印下载文件头部信息
  103. * @author hoojo
  104. * @createDate 2011-9-22 下午05:44:35
  105. * @param conn HttpURLConnection
  106. */
  107. public staticvoid printHeader(URLConnection conn) {
  108. int i = 1;
  109. while (true) {
  110. String header = conn.getHeaderFieldKey(i);
  111. i++;
  112. if (header != null) {
  113. LogUtils.info(header + ":" + conn.getHeaderField(i));
  114. } else {
  115. break;
  116. }
  117. }
  118. }
  119. /**
  120. * <b>function:</b> 设置URLConnection的头部信息,伪装请求信息
  121. * @author hoojo
  122. * @createDate 2011-9-28 下午05:29:43
  123. * @param con
  124. */
  125. public staticvoid setHeader(URLConnection conn) {
  126. conn.setRequestProperty("User-Agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3");
  127. conn.setRequestProperty("Accept-Language","en-us,en;q=0.7,zh-cn;q=0.3");
  128. conn.setRequestProperty("Accept-Encoding","utf-8");
  129. conn.setRequestProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
  130. conn.setRequestProperty("Keep-Alive","300");
  131. conn.setRequestProperty("connnection","keep-alive");
  132. conn.setRequestProperty("If-Modified-Since","Fri, 02 Jan 2009 17:00:05 GMT");
  133. conn.setRequestProperty("If-None-Match","\"1261d8-4290-df64d224\"");
  134. conn.setRequestProperty("Cache-conntrol","max-age=0");
  135. conn.setRequestProperty("Referer","http://www.baidu.com");
  136. }
  137. public boolean isDownloadOver() {
  138. return isDownloadOver;
  139. }
  140. public long getStartPos() {
  141. return startPos;
  142. }
  143. public long getEndPos() {
  144. return endPos;
  145. }
  146. }
package com.hoo.download;import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.hoo.util.LogUtils;/*** <b>function:</b> 单线程下载文件* @author hoojo* @createDate 2011-9-22 下午02:55:10* @file DownloadFile.java* @package com.hoo.download* @project MultiThreadDownLoad* @blog http://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com* @version 1.0*/
public class DownloadFile extends Thread {//下载文件urlprivate String url;//下载文件起始位置  private long startPos;//下载文件结束位置private long endPos;//线程idprivate int threadId;//下载是否完成private boolean isDownloadOver = false;private SaveItemFile itemFile;private static final int BUFF_LENGTH = 1024 * 8;/*** @param url 下载文件url* @param name 文件名称* @param startPos 下载文件起点* @param endPos 下载文件结束点* @param threadId 线程id* @throws IOException*/public DownloadFile(String url, String name, long startPos, long endPos, int threadId) throws IOException {super();this.url = url;this.startPos = startPos;this.endPos = endPos;this.threadId = threadId;//分块下载写入文件内容this.itemFile = new SaveItemFile(name, startPos);}@Overridepublic void run() {while (endPos > startPos && !isDownloadOver) {try {URL url = new URL(this.url);HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 设置连接超时时间为10000msconn.setConnectTimeout(10000);// 设置读取数据超时时间为10000msconn.setReadTimeout(10000);setHeader(conn);String property = "bytes=" + startPos + "-";conn.setRequestProperty("RANGE", property);//输出log信息LogUtils.log("开始 " + threadId + ":" + property + endPos);//printHeader(conn);//获取文件输入流,读取文件内容InputStream is = conn.getInputStream();byte[] buff = new byte[BUFF_LENGTH];int length = -1;LogUtils.log("#start#Thread: " + threadId + ", startPos: " + startPos + ", endPos: " + endPos);while ((length = is.read(buff)) > 0 && startPos < endPos && !isDownloadOver) {//写入文件内容,返回最后写入的长度startPos += itemFile.write(buff, 0, length);}LogUtils.log("#over#Thread: " + threadId + ", startPos: " + startPos + ", endPos: " + endPos);LogUtils.log("Thread " + threadId + " is execute over!");this.isDownloadOver = true;} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (itemFile != null) {itemFile.close();}} catch (IOException e) {e.printStackTrace();}}}if (endPos < startPos && !isDownloadOver) {LogUtils.log("Thread " + threadId  + " startPos > endPos, not need download file !");this.isDownloadOver = true;}if (endPos == startPos && !isDownloadOver) {LogUtils.log("Thread " + threadId  + " startPos = endPos, not need download file !");this.isDownloadOver = true;}}/*** <b>function:</b> 打印下载文件头部信息* @author hoojo* @createDate 2011-9-22 下午05:44:35* @param conn HttpURLConnection*/public static void printHeader(URLConnection conn) {int i = 1;while (true) {String header = conn.getHeaderFieldKey(i);i++;if (header != null) {LogUtils.info(header + ":" + conn.getHeaderField(i));} else {break;}}}/*** <b>function:</b> 设置URLConnection的头部信息,伪装请求信息* @author hoojo* @createDate 2011-9-28 下午05:29:43* @param con*/public static void setHeader(URLConnection conn) {conn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3");conn.setRequestProperty("Accept-Language", "en-us,en;q=0.7,zh-cn;q=0.3");conn.setRequestProperty("Accept-Encoding", "utf-8");conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");conn.setRequestProperty("Keep-Alive", "300");conn.setRequestProperty("connnection", "keep-alive");conn.setRequestProperty("If-Modified-Since", "Fri, 02 Jan 2009 17:00:05 GMT");conn.setRequestProperty("If-None-Match", "\"1261d8-4290-df64d224\"");conn.setRequestProperty("Cache-conntrol", "max-age=0");conn.setRequestProperty("Referer", "http://www.baidu.com");}public boolean isDownloadOver() {return isDownloadOver;}public long getStartPos() {return startPos;}public long getEndPos() {return endPos;}
}

这个类主要是完成单个线程的文件下载,将通过URLConnection读取指定url的资源信息。然后用InputStream读取文件内容,然后调用调用SaveItemFile类,向本地写入当前要读取的块的内容。

四、分段多线程写入文件内容

[java] view plaincopyprint?
  1. package com.hoo.download;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.net.HttpURLConnection;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import com.hoo.entity.DownloadInfo;
  12. import com.hoo.util.LogUtils;
  13. /**
  14. * <b>function:</b> 分批量下载文件
  15. * @author hoojo
  16. * @createDate 2011-9-22 下午05:51:54
  17. * @file BatchDownloadFile.java
  18. * @package com.hoo.download
  19. * @project MultiThreadDownLoad
  20. * @blog http://blog.csdn.net/IBM_hoojo
  21. * @email hoojo_@126.com
  22. * @version 1.0
  23. */
  24. public class BatchDownloadFileimplements Runnable {
  25. //下载文件信息
  26. private DownloadInfo downloadInfo;
  27. //一组开始下载位置
  28. private long[] startPos;
  29. //一组结束下载位置
  30. private long[] endPos;
  31. //休眠时间
  32. private staticfinal int SLEEP_SECONDS =500;
  33. //子线程下载
  34. private DownloadFile[] fileItem;
  35. //文件长度
  36. private int length;
  37. //是否第一个文件
  38. private boolean first =true;
  39. //是否停止下载
  40. private boolean stop =false;
  41. //临时文件信息
  42. private File tempFile;
  43. public BatchDownloadFile(DownloadInfo downloadInfo) {
  44. this.downloadInfo = downloadInfo;
  45. String tempPath = this.downloadInfo.getFilePath() + File.separator + downloadInfo.getFileName() +".position";
  46. tempFile = new File(tempPath);
  47. //如果存在读入点位置的文件
  48. if (tempFile.exists()) {
  49. first = false;
  50. //就直接读取内容
  51. try {
  52. readPosInfo();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. } else {
  57. //数组的长度就要分成多少段的数量
  58. startPos = newlong[downloadInfo.getSplitter()];
  59. endPos = new long[downloadInfo.getSplitter()];
  60. }
  61. }
  62. @Override
  63. public void run() {
  64. //首次下载,获取下载文件长度
  65. if (first) {
  66. length = this.getFileSize();//获取文件长度
  67. if (length == -1) {
  68. LogUtils.log("file length is know!");
  69. stop = true;
  70. } else if (length == -2) {
  71. LogUtils.log("read file length is error!");
  72. stop = true;
  73. } else if (length > 0) {
  74. /**
  75. * eg
  76. * start: 1, 3, 5, 7, 9
  77. * end: 3, 5, 7, 9, length
  78. */
  79. for (int i =0, len = startPos.length; i < len; i++) {
  80. int size = i * (length / len);
  81. startPos[i] = size;
  82. //设置最后一个结束点的位置
  83. if (i == len - 1) {
  84. endPos[i] = length;
  85. } else {
  86. size = (i + 1) * (length / len);
  87. endPos[i] = size;
  88. }
  89. LogUtils.log("start-end Position[" + i +"]: " + startPos[i] + "-" + endPos[i]);
  90. }
  91. } else {
  92. LogUtils.log("get file length is error, download is stop!");
  93. stop = true;
  94. }
  95. }
  96. //子线程开始下载
  97. if (!stop) {
  98. //创建单线程下载对象数组
  99. fileItem = new DownloadFile[startPos.length];//startPos.length = downloadInfo.getSplitter()
  100. for (int i =0; i < startPos.length; i++) {
  101. try {
  102. //创建指定个数单线程下载对象,每个线程独立完成指定块内容的下载
  103. fileItem[i] = new DownloadFile(
  104. downloadInfo.getUrl(),
  105. this.downloadInfo.getFilePath() + File.separator + downloadInfo.getFileName(),
  106. startPos[i], endPos[i], i
  107. );
  108. fileItem[i].start();//启动线程,开始下载
  109. LogUtils.log("Thread: " + i +", startPos: " + startPos[i] + ", endPos: " + endPos[i]);
  110. } catch (IOException e) {
  111. e.printStackTrace();
  112. }
  113. }
  114. //循环写入下载文件长度信息
  115. while (!stop) {
  116. try {
  117. writePosInfo();
  118. LogUtils.log("downloading……");
  119. Thread.sleep(SLEEP_SECONDS);
  120. stop = true;
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. } catch (InterruptedException e) {
  124. e.printStackTrace();
  125. }
  126. for (int i =0; i < startPos.length; i++) {
  127. if (!fileItem[i].isDownloadOver()) {
  128. stop = false;
  129. break;
  130. }
  131. }
  132. }
  133. LogUtils.info("Download task is finished!");
  134. }
  135. }
  136. /**
  137. * 将写入点数据保存在临时文件中
  138. * @author hoojo
  139. * @createDate 2011-9-23 下午05:25:37
  140. * @throws IOException
  141. */
  142. private void writePosInfo()throws IOException {
  143. DataOutputStream dos = new DataOutputStream(new FileOutputStream(tempFile));
  144. dos.writeInt(startPos.length);
  145. for (int i =0; i < startPos.length; i++) {
  146. dos.writeLong(fileItem[i].getStartPos());
  147. dos.writeLong(fileItem[i].getEndPos());
  148. //LogUtils.info("[" + fileItem[i].getStartPos() + "#" + fileItem[i].getEndPos() + "]");
  149. }
  150. dos.close();
  151. }
  152. /**
  153. * <b>function:</b>读取写入点的位置信息
  154. * @author hoojo
  155. * @createDate 2011-9-23 下午05:30:29
  156. * @throws IOException
  157. */
  158. private void readPosInfo()throws IOException {
  159. DataInputStream dis = new DataInputStream(new FileInputStream(tempFile));
  160. int startPosLength = dis.readInt();
  161. startPos = new long[startPosLength];
  162. endPos = new long[startPosLength];
  163. for (int i =0; i < startPosLength; i++) {
  164. startPos[i] = dis.readLong();
  165. endPos[i] = dis.readLong();
  166. }
  167. dis.close();
  168. }
  169. /**
  170. * <b>function:</b> 获取下载文件的长度
  171. * @author hoojo
  172. * @createDate 2011-9-26 下午12:15:08
  173. * @return
  174. */
  175. private int getFileSize() {
  176. int fileLength = -1;
  177. try {
  178. URL url = new URL(this.downloadInfo.getUrl());
  179. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  180. DownloadFile.setHeader(conn);
  181. int stateCode = conn.getResponseCode();
  182. //判断http status是否为HTTP/1.1 206 Partial Content或者200 OK
  183. if (stateCode != HttpURLConnection.HTTP_OK && stateCode != HttpURLConnection.HTTP_PARTIAL) {
  184. LogUtils.log("Error Code: " + stateCode);
  185. return -2;
  186. } else if (stateCode >=400) {
  187. LogUtils.log("Error Code: " + stateCode);
  188. return -2;
  189. } else {
  190. //获取长度
  191. fileLength = conn.getContentLength();
  192. LogUtils.log("FileLength: " + fileLength);
  193. }
  194. //读取文件长度
  195. /*for (int i = 1; ; i++) {
  196. String header = conn.getHeaderFieldKey(i);
  197. if (header != null) {
  198. if ("Content-Length".equals(header)) {
  199. fileLength = Integer.parseInt(conn.getHeaderField(i));
  200. break;
  201. }
  202. } else {
  203. break;
  204. }
  205. }
  206. */
  207. DownloadFile.printHeader(conn);
  208. } catch (MalformedURLException e) {
  209. e.printStackTrace();
  210. } catch (IOException e) {
  211. e.printStackTrace();
  212. }
  213. return fileLength;
  214. }
  215. }
package com.hoo.download;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.hoo.entity.DownloadInfo;
import com.hoo.util.LogUtils;/*** <b>function:</b> 分批量下载文件* @author hoojo* @createDate 2011-9-22 下午05:51:54* @file BatchDownloadFile.java* @package com.hoo.download* @project MultiThreadDownLoad* @blog http://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com* @version 1.0*/
public class BatchDownloadFile implements Runnable {//下载文件信息 private DownloadInfo downloadInfo;//一组开始下载位置private long[] startPos;//一组结束下载位置private long[] endPos;//休眠时间private static final int SLEEP_SECONDS = 500;//子线程下载private DownloadFile[] fileItem;//文件长度private int length;//是否第一个文件private boolean first = true;//是否停止下载private boolean stop = false;//临时文件信息private File tempFile;public BatchDownloadFile(DownloadInfo downloadInfo) {this.downloadInfo = downloadInfo;String tempPath = this.downloadInfo.getFilePath() + File.separator + downloadInfo.getFileName() + ".position";tempFile = new File(tempPath);//如果存在读入点位置的文件if (tempFile.exists()) {first = false;//就直接读取内容try {readPosInfo();} catch (IOException e) {e.printStackTrace();}} else {//数组的长度就要分成多少段的数量startPos = new long[downloadInfo.getSplitter()];endPos = new long[downloadInfo.getSplitter()];}}@Overridepublic void run() {//首次下载,获取下载文件长度if (first) {length = this.getFileSize();//获取文件长度if (length == -1) {LogUtils.log("file length is know!");stop = true;} else if (length == -2) {LogUtils.log("read file length is error!");stop = true;} else if (length > 0) {/*** eg * start: 1, 3, 5, 7, 9* end: 3, 5, 7, 9, length*/for (int i = 0, len = startPos.length; i < len; i++) {int size = i * (length / len);startPos[i] = size;//设置最后一个结束点的位置if (i == len - 1) {endPos[i] = length;} else {size = (i + 1) * (length / len);endPos[i] = size;}LogUtils.log("start-end Position[" + i + "]: " + startPos[i] + "-" + endPos[i]);}} else {LogUtils.log("get file length is error, download is stop!");stop = true;}}//子线程开始下载if (!stop) {//创建单线程下载对象数组fileItem = new DownloadFile[startPos.length];//startPos.length = downloadInfo.getSplitter()for (int i = 0; i < startPos.length; i++) {try {//创建指定个数单线程下载对象,每个线程独立完成指定块内容的下载fileItem[i] = new DownloadFile(downloadInfo.getUrl(), this.downloadInfo.getFilePath() + File.separator + downloadInfo.getFileName(), startPos[i], endPos[i], i);fileItem[i].start();//启动线程,开始下载LogUtils.log("Thread: " + i + ", startPos: " + startPos[i] + ", endPos: " + endPos[i]);} catch (IOException e) {e.printStackTrace();}}//循环写入下载文件长度信息while (!stop) {try {writePosInfo();LogUtils.log("downloading……");Thread.sleep(SLEEP_SECONDS);stop = true;} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}for (int i = 0; i < startPos.length; i++) {if (!fileItem[i].isDownloadOver()) {stop = false;break;}}}LogUtils.info("Download task is finished!");}}/*** 将写入点数据保存在临时文件中* @author hoojo* @createDate 2011-9-23 下午05:25:37* @throws IOException*/private void writePosInfo() throws IOException {DataOutputStream dos = new DataOutputStream(new FileOutputStream(tempFile));dos.writeInt(startPos.length);for (int i = 0; i < startPos.length; i++) {dos.writeLong(fileItem[i].getStartPos());dos.writeLong(fileItem[i].getEndPos());//LogUtils.info("[" + fileItem[i].getStartPos() + "#" + fileItem[i].getEndPos() + "]");}dos.close();}/*** <b>function:</b>读取写入点的位置信息* @author hoojo* @createDate 2011-9-23 下午05:30:29* @throws IOException*/private void readPosInfo() throws IOException {DataInputStream dis = new DataInputStream(new FileInputStream(tempFile));int startPosLength = dis.readInt();startPos = new long[startPosLength];endPos = new long[startPosLength];for (int i = 0; i < startPosLength; i++) {startPos[i] = dis.readLong();endPos[i] = dis.readLong();}dis.close();}/*** <b>function:</b> 获取下载文件的长度* @author hoojo* @createDate 2011-9-26 下午12:15:08* @return*/private int getFileSize() {int fileLength = -1;try {URL url = new URL(this.downloadInfo.getUrl());HttpURLConnection conn = (HttpURLConnection) url.openConnection();DownloadFile.setHeader(conn);int stateCode = conn.getResponseCode();//判断http status是否为HTTP/1.1 206 Partial Content或者200 OKif (stateCode != HttpURLConnection.HTTP_OK && stateCode != HttpURLConnection.HTTP_PARTIAL) {LogUtils.log("Error Code: " + stateCode);return -2;} else if (stateCode >= 400) {LogUtils.log("Error Code: " + stateCode);return -2;} else {//获取长度fileLength = conn.getContentLength();LogUtils.log("FileLength: " + fileLength);}//读取文件长度/*for (int i = 1; ; i++) {String header = conn.getHeaderFieldKey(i);if (header != null) {if ("Content-Length".equals(header)) {fileLength = Integer.parseInt(conn.getHeaderField(i));break;}} else {break;}}*/DownloadFile.printHeader(conn);} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return fileLength;}
}

这个类主要是完成读取指定url资源的内容,获取该资源的长度。然后将该资源分成指定的块数,将每块的起始下载位置、结束下载位置,分别保存在一个数组中。每块都单独开辟一个独立线程开始下载。在开始下载之前,需要创建一个临时文件,写入当前下载线程的开始下载指针位置和结束下载指针位置。

五、工具类、测试类

日志工具类

[java] view plaincopyprint?
  1. package com.hoo.util;
  2. /**
  3. * <b>function:</b> 日志工具类
  4. * @author hoojo
  5. * @createDate 2011-9-21 下午05:21:27
  6. * @file LogUtils.java
  7. * @package com.hoo.util
  8. * @project MultiThreadDownLoad
  9. * @blog http://blog.csdn.net/IBM_hoojo
  10. * @email hoojo_@126.com
  11. * @version 1.0
  12. */
  13. public abstractclass LogUtils {
  14. public staticvoid log(Object message) {
  15. System.err.println(message);
  16. }
  17. public staticvoid log(String message) {
  18. System.err.println(message);
  19. }
  20. public staticvoid log(int message) {
  21. System.err.println(message);
  22. }
  23. public staticvoid info(Object message) {
  24. System.out.println(message);
  25. }
  26. public staticvoid info(String message) {
  27. System.out.println(message);
  28. }
  29. public staticvoid info(int message) {
  30. System.out.println(message);
  31. }
  32. }
package com.hoo.util;/*** <b>function:</b> 日志工具类* @author hoojo* @createDate 2011-9-21 下午05:21:27* @file LogUtils.java* @package com.hoo.util* @project MultiThreadDownLoad* @blog http://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com* @version 1.0*/
public abstract class LogUtils {public static void log(Object message) {System.err.println(message);}public static void log(String message) {System.err.println(message);}public static void log(int message) {System.err.println(message);}public static void info(Object message) {System.out.println(message);}public static void info(String message) {System.out.println(message);}public static void info(int message) {System.out.println(message);}
}

下载工具类

[java] view plaincopyprint?
  1. package com.hoo.util;
  2. import com.hoo.download.BatchDownloadFile;
  3. import com.hoo.entity.DownloadInfo;
  4. /**
  5. * <b>function:</b> 分块多线程下载工具类
  6. * @author hoojo
  7. * @createDate 2011-9-28 下午05:22:18
  8. * @file DownloadUtils.java
  9. * @package com.hoo.util
  10. * @project MultiThreadDownLoad
  11. * @blog http://blog.csdn.net/IBM_hoojo
  12. * @email hoojo_@126.com
  13. * @version 1.0
  14. */
  15. public abstractclass DownloadUtils {
  16. public staticvoid download(String url) {
  17. DownloadInfo bean = new DownloadInfo(url);
  18. LogUtils.info(bean);
  19. BatchDownloadFile down = new BatchDownloadFile(bean);
  20. new Thread(down).start();
  21. }
  22. public staticvoid download(String url, int threadNum) {
  23. DownloadInfo bean = new DownloadInfo(url, threadNum);
  24. LogUtils.info(bean);
  25. BatchDownloadFile down = new BatchDownloadFile(bean);
  26. new Thread(down).start();
  27. }
  28. public staticvoid download(String url, String fileName, String filePath,int threadNum) {
  29. DownloadInfo bean = new DownloadInfo(url, fileName, filePath, threadNum);
  30. LogUtils.info(bean);
  31. BatchDownloadFile down = new BatchDownloadFile(bean);
  32. new Thread(down).start();
  33. }
  34. }
package com.hoo.util;import com.hoo.download.BatchDownloadFile;
import com.hoo.entity.DownloadInfo;/*** <b>function:</b> 分块多线程下载工具类* @author hoojo* @createDate 2011-9-28 下午05:22:18* @file DownloadUtils.java* @package com.hoo.util* @project MultiThreadDownLoad* @blog http://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com* @version 1.0*/
public abstract class DownloadUtils {public static void download(String url) {DownloadInfo bean = new DownloadInfo(url);LogUtils.info(bean);BatchDownloadFile down = new BatchDownloadFile(bean);new Thread(down).start();}public static void download(String url, int threadNum) {DownloadInfo bean = new DownloadInfo(url, threadNum);LogUtils.info(bean);BatchDownloadFile down = new BatchDownloadFile(bean);new Thread(down).start();}public static void download(String url, String fileName, String filePath, int threadNum) {DownloadInfo bean = new DownloadInfo(url, fileName, filePath, threadNum);LogUtils.info(bean);BatchDownloadFile down = new BatchDownloadFile(bean);new Thread(down).start();}
}

下载测试类

[java] view plaincopyprint?
  1. package com.hoo.test;
  2. import com.hoo.util.DownloadUtils;
  3. /**
  4. * <b>function:</b> 下载测试
  5. * @author hoojo
  6. * @createDate 2011-9-23 下午05:49:46
  7. * @file TestDownloadMain.java
  8. * @package com.hoo.download
  9. * @project MultiThreadDownLoad
  10. * @blog http://blog.csdn.net/IBM_hoojo
  11. * @email hoojo_@126.com
  12. * @version 1.0
  13. */
  14. public class TestDownloadMain {
  15. public staticvoid main(String[] args) {
  16. /*DownloadInfo bean = new DownloadInfo("http://i7.meishichina.com/Health/UploadFiles/201109/2011092116224363.jpg");
  17. System.out.println(bean);
  18. BatchDownloadFile down = new BatchDownloadFile(bean);
  19. new Thread(down).start();*/
  20. //DownloadUtils.download("http://i7.meishichina.com/Health/UploadFiles/201109/2011092116224363.jpg");
  21. DownloadUtils.download("http://mp3.baidu.com/j?j=2&url=http%3A%2F%2Fzhangmenshiting2.baidu.com%2Fdata%2Fmusic%2F1669425%2F%25E9%2599%25B7%25E5%2585%25A5%25E7%2588%25B1%25E9%2587%258C%25E9%259D%25A2.mp3%3Fxcode%3D2ff36fb70737c816553396c56deab3f1","aa.mp3", "c:/temp",5);
  22. }
  23. }
package com.hoo.test;import com.hoo.util.DownloadUtils;/*** <b>function:</b> 下载测试* @author hoojo* @createDate 2011-9-23 下午05:49:46* @file TestDownloadMain.java* @package com.hoo.download* @project MultiThreadDownLoad* @blog http://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com* @version 1.0*/
public class TestDownloadMain {public static void main(String[] args) {/*DownloadInfo bean = new DownloadInfo("http://i7.meishichina.com/Health/UploadFiles/201109/2011092116224363.jpg");System.out.println(bean);BatchDownloadFile down = new BatchDownloadFile(bean);new Thread(down).start();*///DownloadUtils.download("http://i7.meishichina.com/Health/UploadFiles/201109/2011092116224363.jpg");DownloadUtils.download("http://mp3.baidu.com/j?j=2&url=http%3A%2F%2Fzhangmenshiting2.baidu.com%2Fdata%2Fmusic%2F1669425%2F%25E9%2599%25B7%25E5%2585%25A5%25E7%2588%25B1%25E9%2587%258C%25E9%259D%25A2.mp3%3Fxcode%3D2ff36fb70737c816553396c56deab3f1", "aa.mp3", "c:/temp", 5);}
}

多线程下载主要在第三部和第四部,其他的地方还是很好理解。源码中提供相应的注释了,便于理解。

本文转载于:http://blog.csdn.net/ibm_hoojo/article/details/6838222

转载于:https://www.cnblogs.com/ouyangpeng/archive/2013/04/04/8538416.html

Java 多线程断点下载文件_详解相关推荐

  1. 使用python下载文件_详解使用Python下载文件的几种方法

    在使用Python进行数据抓取的时候,有时候需要保持文件或图片等,在Python中可以有多种方式实现.今天就一起来学习下. urllib.request 主要使用的是urlretrieve方法,该方法 ...

  2. 基于php下载文件的详解

    基于php下载文件的详解 本篇文章是对php下载文件进行了详细的分析介绍,需要的朋友参考下 php下载文件,比如txt文件. 出现的效果就是,弹出浏览器自带的下载框,出现另存为操作.有时候会出现内存溢 ...

  3. java多线程中的join方法详解

    java多线程中的join方法详解 方法Join是干啥用的? 简单回答,同步,如何同步? 怎么实现的? 下面将逐个回答. 自从接触Java多线程,一直对Join理解不了.JDK是这样说的:join p ...

  4. Java多线程断点下载

    多线程下载已经提高了下载的效率,但是当一些特殊情况发生的时候,我们需要对程序进行处理,这样效率会更高.比如,断电断网等造成下载中断,那么我们下一次又要重新开始下载,这样效率底下,所以我们可以考虑使用断 ...

  5. python多线程下载大文件_Python threading多线程断点下载文件的方法

    这是玩蛇网一篇关于Python多线程下载文件方法的代码实例.文中应用到的python模块和方法有httplib.Python urllib2.Python threading多线程模块.python ...

  6. java从url下载文件_从URL下载Java文件

    java从url下载文件 Today we will learn how to download a file from URL in java. We can use java.net.URL op ...

  7. java线程下载文件_Java多线程下载文件实例详解

    本文实例为大家分享了Java多线程下载文件的具体代码,供大家参考,具体内容如下 import java.io.File; import java.io.InputStream; import java ...

  8. java调用项目中的文件_详解eclipse项目中.classpath文件的使用

    1 前言 在使用eclipse或者myeclipse进行java项目开发的时候,每个project(工程)下面都会有一个.classpath文件,那么这个文件究竟有什么作用? 2 作用 .classp ...

  9. java访问本地文件_详解Java读取本地文件并显示在JSP文件中

    详解Java读取本地文件并显示在JSP文件中 当我们初学IMG标签时,我们知道通过设置img标签的src属性,能够在页面中显示想要展示的图片.其中src的值,可以是磁盘目录上的绝对,也可以是项目下的相 ...

最新文章

  1. Cocos 全局变量的使用
  2. esp32 camera_利用Phyphox和ESP32蓝牙制作欧姆表测电阻
  3. pandas中align函数的使用示例
  4. Gartner2018新兴技术成熟度曲线:人机界线日益模糊!
  5. SuperSocket 1.5 Documentation译文 2 ----- 实现你的AppServer和AppSession
  6. 服务器重装之后连接不上及解决措施
  7. 【spring-boot】启用数据缓存功能
  8. window.location.reload()会掉参数吗_iPhone手机电量不够用,你真的会用苹果吗?设置好这几个就OK了!...
  9. MP3技术白皮书-音频格式全解析
  10. 03-06 APP-UI自动化测试-等待方式
  11. 车道线检测综述及近期新工作
  12. 【零基础学Java】—自定义异常(四十八)
  13. android 嵌套分组拖动_Android NestedScrolling嵌套滑动机制
  14. 3Sum Closest
  15. 字典爆破php,密码字典 渗透测试字典 爆破字典
  16. Django REST Framework——3. 序列化器(Serializer)
  17. NXP S32K146 CAN通讯 TJA1043(二)
  18. flutter TabBar设置圆角背景
  19. mysql数据库连接池锁_数据库连接池deadlock
  20. PS颜色模式及修图工具

热门文章

  1. UVA11549计算器谜题
  2. hdu1529 差分约束(好题)
  3. 【Linux 内核 内存管理】Linux 内核内存布局 ③ ( Linux 内核 动态分配内存 系统接口函数 | 统计输出 vmalloc 分配的内存 )
  4. 【Windows 逆向】使用 CE 工具挖掘关键数据内存真实地址 ( 查找子弹数据的动态地址 | 查找子弹数据的静态地址 | 静态地址分析 | 完整流程 ) ★
  5. 【Android 插件化】VirtualApp 源码分析 ( 启动应用源码分析 | HomePresenterImpl 启动应用方法 | VirtualCore 启动插件应用最终方法 )
  6. 【Flutter】Flutter 项目中使用 Flutter 插件 ( Flutter 插件管理平台 | 搜索 Flutter 插件 | 安装 Flutter 插件 | 使用 Flutter 插件 )
  7. 模板方法及策略设计模式实践
  8. bzoj 1863 二分+dp check
  9. 查看Eclipse32位还是64位以及Eclipse的编译版本号,查看JDK是32位还是64位
  10. android100 自定义内容提供者