日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多线程分段下载文件

發布時間:2025/3/11 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多线程分段下载文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原理

先獲取文件大小,然后分段分配任務給線程下載

在開始多線程下載前得先得知下載文件的大小,如果在之前的流程中并沒有告知文件大小則可以使用HTTP請求方法 HEAD,這個請求方法類似于 GET 請求,只不過返回的響應中沒有具體的內容,用于獲取報頭,在頭部中可以找到字段content-length就是文件大小了。

得知文件長度后應分割需要下載的起止位置以便之后使用。
有具體位置后就可以啟動多線程發送網絡請求,在網絡請求中使用HTTP協議的頭部標志Range,這個標志的使用方法是Range:bytes=start-end。

實現

import java.io.*; import java.net.HttpURLConnection; import java.net.URL;/*** Created with IntelliJ IDEA.** @Auther: zlf* @Date: 2021/09/14/1:01* @Description:*/ public class MutiThreadDownload {private int threadCount = 5; // 下載線程數private long blocksize; // 每線程下載區塊大小private int runningThreadCount; // 正運行線程數public int getThreadCount() {return threadCount;}public void setThreadCount(int threadCount) {this.threadCount = threadCount;}public long getBlocksize() {return blocksize;}public void setBlocksize(long blocksize) {this.blocksize = blocksize;}public int getRunningThreadCount() {return runningThreadCount;}public void setRunningThreadCount(int runningThreadCount) {this.runningThreadCount = runningThreadCount;}/*** @Description: 多線程分段下載* @Param: [url, filePath]* @return: void* @Author: zlf* @Date: 2021/9/14*/public void download(String url, String filePath) throws IOException {HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();int code = conn.getResponseCode();if (code == 200) {long size = conn.getContentLength();// 文件大小blocksize = size / threadCount;// 1.創建RandomAccessFile文件(可以隨機訪問)RandomAccessFile raf = new RandomAccessFile(new File(filePath), "rw");raf.setLength(size);// 2.多線程下載對應區塊runningThreadCount = threadCount;for (int i = 1; i <= threadCount; i++) {long startIndex = (i - 1) * blocksize;long endIndex = i * blocksize - 1;if (i == threadCount) {endIndex = size - 1;}new DownloadThread(i, url, filePath,startIndex, endIndex).start();}}conn.disconnect();}/**** @Description: 分段下載的線程* @Param: * @return: * @Author: zlf* @Date: 2021/9/14*/private class DownloadThread extends Thread {private int id;private String url;private long startIndex;private long endIndex;private String filePath;public DownloadThread(int id, String url, String filePath, long startIndex, long endIndex) {this.id = id;this.url = url;this.startIndex = startIndex;this.filePath = filePath;this.endIndex = endIndex;}@Overridepublic void run() {try {HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();conn.setRequestMethod("GET");// 1.讀取文件斷點位置curIndexint curIndex = 0;File indexFile = new File("indexFile_"+id);if (indexFile.exists() && indexFile.length() > 0) {FileInputStream fis = new FileInputStream(indexFile);BufferedReader br = new BufferedReader(new InputStreamReader(fis));curIndex = Integer.valueOf(br.readLine());startIndex += curIndex;fis.close();}// 2.從startIndex位置下載文件, 并從startIndex位置寫入raf文件conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);InputStream is = conn.getInputStream();RandomAccessFile raf = new RandomAccessFile(new File(filePath), "rw");raf.seek(startIndex);int len = 0;byte[] buffer = new byte[1024*1024];while ((len = is.read(buffer)) != -1) {raf.write(buffer, 0, len);// 更新斷點位置RandomAccessFile indexRaf = new RandomAccessFile(indexFile, "rwd");curIndex += len;indexRaf.write(String.valueOf(curIndex).getBytes());indexRaf.close();}is.close();raf.close();} catch (Exception e) {e.printStackTrace();} finally {// 3.所有線程下載完,刪除斷點位置文件indexFilesynchronized (MutiThreadDownload.class){if (--runningThreadCount == 0) {for (int i = 1; i <= threadCount; i++) new File("indexFile_"+i).delete();}}}}}public static void main(String[] args) throws IOException {MutiThreadDownload mutiThreadDownload = new MutiThreadDownload();mutiThreadDownload.setRunningThreadCount(5);mutiThreadDownload.download("http://img.netbian.com/file/2020/0904/de2f77ed1090735b441ba5e4c2b460ca.jpg","D:/a.jpg");}}

總結

以上是生活随笔為你收集整理的多线程分段下载文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。