Linux教程網
Java多線程下載源碼,僅作參考之用
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.net.HttpURLConnection;
- import java.net.URL;
-
- public class MyThreadDownloader
- {
- public static void main(String args[])
- {
- // 下載連接
- String downloadURL = "http://localhost:8080/cangjingkong.avi";
- // 下載開啟線程數
- int threadSize = 3;
- try
- {
- new MyThreadDownloader().download(downloadURL, threadSize);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
-
- private void download(String downloadURL, int threadSize) throws Exception
- {
- String fileName = getFileName(downloadURL);
-
- URL url = new URL(downloadURL);
-
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- // 設置超時
- connection.setConnectTimeout(5000);
- // 設置請求方式
- connection.setRequestMethod("GET");
- // 獲取文件長度
- int fileLength = connection.getContentLength();
- // 保存文件
- File localFile = new File(fileName);
- // 臨時文件不存內存,直接進行數據讀寫"rwd"
- RandomAccessFile randomAccessFile = new RandomAccessFile(localFile,"rwd");
- // 直接創建與服務器獲取長度相等的文件
- randomAccessFile.setLength(fileLength);
- // 文件操作over,關閉
- randomAccessFile.close();
-
- // 計算每條線程負責下載的數據量
- int downblock = fileLength % threadSize == 0 ? fileLength / threadSize
- : fileLength / threadSize + 1;
- // 分配每條線程下載任務
- for (int threadID = 0; threadID < threadSize; threadID++)
- {
- //開啟多線程下載任務
- new DownloadThread(threadID, url, localFile, downblock).start();
- }
- }
-
-
- /**
- * @param downloadURL
- * 下載連接
- * @return 返回下載文件名
- */
- private String getFileName(String downloadURL)
- {
- return downloadURL.substring(downloadURL.lastIndexOf("/") + 1);
- }
-
- public final class DownloadThread extends Thread
- {
- private int threadID; // 當前下載線程ID
- private URL url; // 下載連接
- private File localFile; // 目標文件
- private int downblock; // 分段下載長度
- private int startPosition; // 開始下載位置
- private int endPosition; // 結束下載位置
-
- public DownloadThread(int threadID, URL url, File localFile,
- int downblock)
- {
- this.threadID = threadID;
- this.url = url;
- this.localFile = localFile;
- this.downblock = downblock;
- }
-
- public void run()
- {
- startPosition = threadID * downblock;
- endPosition = (threadID + 1) * downblock;
- try
- {
- RandomAccessFile randomAccessFile = new RandomAccessFile(localFile,"rwd");
- randomAccessFile.seek(startPosition);
-
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setReadTimeout(5000);
- connection.setRequestMethod("GET");
- connection.setRequestProperty("Range", "bytes="+startPosition+"-"+endPosition);
-
- InputStream inputStream = connection.getInputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while ((len = inputStream.read(buffer)) != -1)
- {
- randomAccessFile.write(buffer, 0, len);
- }
-
- inputStream.close();
- randomAccessFile.close();
-
- System.out.println("第"+(threadID+1)+"條線程已經下載完成");
-
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
未做任何處理
Copyright ©
Linux教程網 All Rights Reserved