歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java多線程下載源碼

Java多線程下載源碼

日期:2017/3/1 10:55:26   编辑:Linux編程
Java多線程下載源碼,僅作參考之用
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.RandomAccessFile;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. public class MyThreadDownloader
  9. {
  10. public static void main(String args[])
  11. {
  12. // 下載連接
  13. String downloadURL = "http://localhost:8080/cangjingkong.avi";
  14. // 下載開啟線程數
  15. int threadSize = 3;
  16. try
  17. {
  18. new MyThreadDownloader().download(downloadURL, threadSize);
  19. }
  20. catch (Exception e)
  21. {
  22. e.printStackTrace();
  23. }
  24. }
  25. private void download(String downloadURL, int threadSize) throws Exception
  26. {
  27. String fileName = getFileName(downloadURL);
  28. URL url = new URL(downloadURL);
  29. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  30. // 設置超時
  31. connection.setConnectTimeout(5000);
  32. // 設置請求方式
  33. connection.setRequestMethod("GET");
  34. // 獲取文件長度
  35. int fileLength = connection.getContentLength();
  36. // 保存文件
  37. File localFile = new File(fileName);
  38. // 臨時文件不存內存,直接進行數據讀寫"rwd"
  39. RandomAccessFile randomAccessFile = new RandomAccessFile(localFile,"rwd");
  40. // 直接創建與服務器獲取長度相等的文件
  41. randomAccessFile.setLength(fileLength);
  42. // 文件操作over,關閉
  43. randomAccessFile.close();
  44. // 計算每條線程負責下載的數據量
  45. int downblock = fileLength % threadSize == 0 ? fileLength / threadSize
  46. : fileLength / threadSize + 1;
  47. // 分配每條線程下載任務
  48. for (int threadID = 0; threadID < threadSize; threadID++)
  49. {
  50. //開啟多線程下載任務
  51. new DownloadThread(threadID, url, localFile, downblock).start();
  52. }
  53. }
  54. /**
  55. * @param downloadURL
  56. * 下載連接
  57. * @return 返回下載文件名
  58. */
  59. private String getFileName(String downloadURL)
  60. {
  61. return downloadURL.substring(downloadURL.lastIndexOf("/") + 1);
  62. }
  63. public final class DownloadThread extends Thread
  64. {
  65. private int threadID; // 當前下載線程ID
  66. private URL url; // 下載連接
  67. private File localFile; // 目標文件
  68. private int downblock; // 分段下載長度
  69. private int startPosition; // 開始下載位置
  70. private int endPosition; // 結束下載位置
  71. public DownloadThread(int threadID, URL url, File localFile,
  72. int downblock)
  73. {
  74. this.threadID = threadID;
  75. this.url = url;
  76. this.localFile = localFile;
  77. this.downblock = downblock;
  78. }
  79. public void run()
  80. {
  81. startPosition = threadID * downblock;
  82. endPosition = (threadID + 1) * downblock;
  83. try
  84. {
  85. RandomAccessFile randomAccessFile = new RandomAccessFile(localFile,"rwd");
  86. randomAccessFile.seek(startPosition);
  87. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  88. connection.setReadTimeout(5000);
  89. connection.setRequestMethod("GET");
  90. connection.setRequestProperty("Range", "bytes="+startPosition+"-"+endPosition);
  91. InputStream inputStream = connection.getInputStream();
  92. byte[] buffer = new byte[1024];
  93. int len = 0;
  94. while ((len = inputStream.read(buffer)) != -1)
  95. {
  96. randomAccessFile.write(buffer, 0, len);
  97. }
  98. inputStream.close();
  99. randomAccessFile.close();
  100. System.out.println("第"+(threadID+1)+"條線程已經下載完成");
  101. }
  102. catch (FileNotFoundException e)
  103. {
  104. e.printStackTrace();
  105. }
  106. catch (IOException e)
  107. {
  108. e.printStackTrace();
  109. }
  110. }
  111. }
  112. }
未做任何處理
Copyright © Linux教程網 All Rights Reserved