java多線程下載文件原理解析
原理解析:利用RandomAccessFile在本地創(chuàng)建一個(gè)隨機(jī)訪問(wèn)文件,文件大小和服務(wù)器要下載的文件大小相同。根據(jù)線程的數(shù)量(假設(shè)有三個(gè)線程),服務(wù)器的文件三等分,并把我們?cè)诒镜貏?chuàng)建的文件同樣三等分,每個(gè)線程下載自己負(fù)責(zé)的部分,到相應(yīng)的位置即可。
示例圖:
示例demo
import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class MutilDownload { private static String path = "http://192.168.80.85:8080/test.doc"; private static final int threadCount = 3; public static void main(String[] args) { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); int responseCode = conn.getResponseCode(); if (responseCode == 200) { int contentLength = conn.getContentLength(); System.out.println("length" + contentLength); RandomAccessFile rafAccessFile = new RandomAccessFile("test.doc", "rw"); rafAccessFile.setLength(contentLength); int blockSize = contentLength / threadCount; for (int i = 0; i < threadCount; i++) { int startIndex = i * blockSize; //每個(gè)現(xiàn)成下載的開(kāi)始位置 int endIndex = (i + 1) * blockSize - 1;// 每個(gè)線程的結(jié)束位置 if (i == threadCount - 1) { //最后一個(gè)線程 endIndex = contentLength - 1; } new DownloadThread(startIndex, endIndex, i).start(); } } } catch (Exception e) { } } private static class DownloadThread extends Thread { private int startIndex; private int endIndex; private int threadId; public DownloadThread(int startIndex, int endIndex, int threadId) { this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; } @Override public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); //固定寫法,請(qǐng)求部分資源 int responseCode = conn.getResponseCode(); // 206表示請(qǐng)求部分資源 if (responseCode == 206) { RandomAccessFile rafAccessFile = new RandomAccessFile("test.doc", "rw"); rafAccessFile.seek(startIndex); InputStream is = conn.getInputStream(); int len = -1; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { rafAccessFile.write(buffer, 0, len); } rafAccessFile.close(); System.out.println("線程" + threadId + "下載完成"); } } catch (Exception e) { } } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一個(gè)簡(jiǎn)陋的java圖書管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)陋的java圖書管理系統(tǒng),簡(jiǎn)單的實(shí)現(xiàn)功能測(cè)試,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07詳解Java List的擴(kuò)容機(jī)制原理及應(yīng)用
在Java中,List是一種非常常用的數(shù)據(jù)結(jié)構(gòu),用于存儲(chǔ)有序的元素集合,本文將分析Java List的擴(kuò)容機(jī)制原理,并通過(guò)示例代碼和測(cè)試代碼來(lái)加強(qiáng)闡述內(nèi)容,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08SpringBoot打印啟動(dòng)時(shí)異常堆棧信息詳解
在本篇文章里小編給大家整理的是關(guān)于SpringBoot打印啟動(dòng)時(shí)異常堆棧信息,有需要的朋友們可以學(xué)習(xí)下。2019-11-11Java?的訪問(wèn)修飾符public,protected,private(封裝、繼承)
這篇文章主要介紹了Java?的訪問(wèn)修飾符public,protected,private(封裝、繼承),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09SSH框架網(wǎng)上商城項(xiàng)目第24戰(zhàn)之Struts2中處理多個(gè)Model請(qǐng)求的方法
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第24戰(zhàn)之Struts2中處理多個(gè)Model請(qǐng)求的方法,感興趣的小伙伴們可以參考一下2016-06-06spring?@Conditional的使用與擴(kuò)展源碼分析
這篇文章主要介紹了spring?@Conditional的使用與擴(kuò)展,這里需要注意如果Condition返回的是false,那么spirng就不會(huì)對(duì)方法或類進(jìn)行解析,具體源碼分析跟隨小編一起看看吧2022-03-03如何在Java中創(chuàng)建線程通信的四種方式你知道嗎
開(kāi)發(fā)中不免會(huì)遇到需要所有子線程執(zhí)行完畢通知主線程處理某些邏輯的場(chǎng)景。或者是線程 A 在執(zhí)行到某個(gè)條件通知線程 B 執(zhí)行某個(gè)操作。下面我們來(lái)一起學(xué)習(xí)如何解決吧2021-09-09SpringBoot是如何實(shí)現(xiàn)自動(dòng)配置的你知道嗎
這篇文章主要介紹了詳解SpringBoot自動(dòng)配置原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-08-08