Java多線程下載文件實(shí)現(xiàn)案例詳解
原理解析:
利用RandomAccessFile在本地創(chuàng)建一個(gè)隨機(jī)訪問文件,文件大小和服務(wù)器要下載的文件大小相同。 根據(jù)線程的數(shù)量(假設(shè)有三個(gè)線程),服務(wù)器的文件三等分,并把我們?cè)诒镜貏?chuàng)建的文件同樣三等分,每個(gè)線程下載自己負(fù)責(zé)的部分,到相應(yīng)的位置即可。
示例圖:

代碼如下
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)成下載的開始位置
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)文章
Spring Boot項(xiàng)目中定制攔截器的方法詳解
這篇文章主要介紹了Spring Boot項(xiàng)目中定制攔截器的方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
Spring Web零xml配置原理以及父子容器關(guān)系詳解
這篇文章主要介紹了Spring Web零xml配置原理以及父子容器關(guān)系詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Spring Validation和Hibernate Validator結(jié)合國(guó)際化代碼實(shí)例
這篇文章主要介紹了Spring Validation和Hibernate Validator結(jié)合國(guó)際化代碼實(shí)例,我們需要對(duì)請(qǐng)求參數(shù)進(jìn)行非空、長(zhǎng)度、正確性進(jìn)行校驗(yàn), 本文主要講解Spring Validation 和 Hibernate Validator, 同時(shí)整合i18n(國(guó)際化)實(shí)現(xiàn)參數(shù)校驗(yàn)自動(dòng),需要的朋友可以參考下2023-10-10
java使用XSSFWorkbook實(shí)現(xiàn)讀寫Excel
這篇文章主要為大家詳細(xì)介紹了java如何通過使用XSSFWorkbook實(shí)現(xiàn)讀寫Excel功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
詳解SpringBoot讀取resource目錄下properties文件的常見方式
這篇文章主要介紹了SpringBoot讀取resource目錄下properties文件的常見方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Javaweb請(qǐng)求轉(zhuǎn)發(fā)及重定向?qū)崿F(xiàn)詳解
這篇文章主要介紹了Javaweb請(qǐng)求轉(zhuǎn)發(fā)及重定向?qū)崿F(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Spring使用@Autowired注解靜態(tài)實(shí)例對(duì)象方式
這篇文章主要介紹了Spring使用@Autowired注解靜態(tài)實(shí)例對(duì)象方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot集成Validation參數(shù)校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了SpringBoot集成Validation參數(shù)校驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

