亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Java多線程文件分片下載實現的示例代碼

瀏覽:70日期:2022-09-04 18:54:27

多線程下載介紹

多線程下載技術是很常見的一種下載方案,這種方式充分利用了多線程的優勢,在同一時間段內通過多個線程發起下載請求,將需要下載的數據分割成多個部分,每一個線程只負責下載其中一個部分,然后將下載后的數據組裝成完整的數據文件,這樣便大大加快了下載效率。常見的下載器,迅雷,QQ旋風等都采用了這種技術。

分片下載

所謂分片下載就是要利用多線程的優勢,將要下載的文件一塊一塊的分配到各個線程中去下載,這樣就極大的提高了下載速度。

技術難點

并不能說是什么難點,只能說沒接觸過不知道罷了。

1、如何請求才能拿到數據的特定部分,而非全部?

可以在HTTP請求頭中加入Range來標識數據的請求范圍/區間,從HTTP/1.1開始可用。

基本用法:

Range: bytes=10-:取第10個字節及后所有數據。

Range: bytes=40-100:取第40個字節到第100個字節之間的數據。

這樣我們就能拿到特定部分的數據了,斷點續傳也可以用這個來實現。

PS:0為開始點。

2、分片后某線程下載時如何寫出?

思路1:等所有下載完成后進行統一匯總整理然后再一次性寫出。

這簡直是最笨的思路了,如果文件過大全部拉到內存中,豈不涼涼。

思路2:下載采用多線程,寫出時采取數據前后順序排隊寫出。

也就是說多線程下載,單線程輸出,某種程度解決了內存占用問題,不過效率基本不理想。

思路3:要說還是API香,老大哥Java給我們提供了一個類叫做RandomAccessFile。

這個類可以進行隨機文件讀寫,其中有一個seek函數,可以將指針指向任意位置,然后進行讀寫。什么意思呢,舉個栗子:假如我們開了30個線程,首先第一個下載完成的是線程X,它下載的數據范圍是4000-9000,那么這時我們調用seek函數將指針撥動到4000,然后調用它的write函數將byte寫出,這時4000之前都是NULL,4000之后就是我們插入的數據。這樣就可以實現多線程下載和本地寫入了。

具體實現

一個分片下載類,我們需要創建多個對象來進行下載。

public class UnitDownloader implements Runnable { private int from; private int to; private File target; private String uri; private int id; public UnitDownloader(int from, int to, File target, String uri, int id) { this.from = from; this.to = to; this.target = target; this.uri = uri; this.id = id; } public int getFrom() { return from; } public int getTo() { return to; } @Override public void run() { //download and save data try { HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection(); connection.setRequestProperty('Range', 'bytes=' + from + '-' + to); connection.connect(); int totalSize = connection.getContentLength(); InputStream inputStream = connection.getInputStream(); RandomAccessFile randomAccessFile = new RandomAccessFile(target, 'rw'); randomAccessFile.seek(from); byte[] buffer = new byte[1024 * 1024]; int readCount = inputStream.read(buffer, 0, buffer.length); while (readCount > 0) {totalSize -= readCount;System.out.println('分片:' + this.id + '的剩余:' + totalSize);randomAccessFile.write(buffer, 0, readCount);readCount = inputStream.read(buffer, 0, buffer.length); } inputStream.close(); randomAccessFile.close(); } catch (IOException e) { e.printStackTrace(); } }}

分片下載管理器,主要就是拿到內容的總大小,將其分配給每一個UnitDownloader。這里的threadCount函數可以再考慮優化一下。

public class MultipleThreadDownloadManager implements Runnable { private String uri; private File target; public MultipleThreadDownloadManager(String uri, File target) { this.target = target; this.uri = uri; if (target.exists() == false) { try {target.createNewFile(); } catch (IOException e) {e.printStackTrace(); } } } /** * 開始下載 */ public void start() { new Thread(this).start(); } /** * 根據文件總大小計算線程數量 * * @param totalSize * @return */ public int threadCount(int totalSize) { if (totalSize < 30 * 2014 * 1024) { return 1; } return 30; } @Override public void run() { //獲取文件總大小 int totalSize = 0; try { HttpURLConnection connection = (HttpURLConnection) new URL(uri).openConnection(); connection.connect(); int contentLength = connection.getContentLength(); totalSize = contentLength; } catch (IOException e) { e.printStackTrace(); } //將文件分片并分開下載 int threadCount = threadCount(totalSize); int perThreadSize = totalSize / threadCount;//每一個線程分到的任務下載量 int id = 0; int from = 0, to = 0; while (totalSize > 0) { id++; //計算分片 if (totalSize < perThreadSize) {from = 0;to = totalSize; } else {from = totalSize;to = from + perThreadSize; } //開始下載 UnitDownloader downloader = new UnitDownloader(from, to, target, uri, id); new Thread(downloader).start(); } }}

參考文獻

1、https://emacsist.github.io/2015/12/29/http-%E5%8D%8F%E8%AE%AE%E4%B8%AD%E7%9A%84range%E8%AF%B7%E6%B1%82%E5%A4%B4%E4%BE%8B%E5%AD%90/

2、https://blog.csdn.net/lyt_7cs1dn9/article/details/75105266

到此這篇關于Java多線程文件分片下載實現的示例代碼的文章就介紹到這了,更多相關Java多線程分片下載內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 亚洲免费看片 | 一级毛片短视频 | 1024国产在线 | 美女批日起爽在线观看 | 免费国产99久久久香蕉 | 亚洲免费中字慕日产2021 | 极品主播的慰在线播放 | 91精品国产视频 | 国产大乳喷奶水在线看 | 黄色免费看片 | 国产三级观看久久 | 日本毛片在线看 | 性视频一级 | 欧美日韩一区二区三区免费不卡 | 亚洲第一免费视频 | 国产精品久久久久久久 | 美女国产网站 | 婷婷六月久久综合丁香乐透 | 精品国产视频在线观看 | 最新欧美精品一区二区三区不卡 | 亚洲一区二区三区四区 | 成人久久18免费网址 | 亚洲欧美在线综合一区二区三区 | 国产精品福利无圣光一区二区 | 久久久精品在线观看 | 亚洲欧美日韩中文字幕一区二区三区 | 日韩黄色在线视频 | 国产成人精品日本亚洲语音1 | 久草福利在线播放 | 欧美成年免费a级 | 亚洲精品在线网 | 久久久久一区二区三区 | 精品黄色录像 | 69交性视频| 国产精品久久久久久久 | 久久精品视频播放 | 国产精品视频无圣光一区 | 欧洲成人在线视频 | 老司机一级毛片 | 一级α一级α片免费观看网站 | 天天草影院|