java多線程實(shí)現(xiàn)文件下載功能
多線程下載文件的思路:
1.首先獲取到文件的總大小
獲取文件大小的方式是通過網(wǎng)絡(luò)讀取,getContentLength()即可獲取到文件的大小,使用RandomAccessFile()支持隨機(jī)訪問
2.根據(jù)所準(zhǔn)備的線程數(shù)據(jù),計(jì)算每一個(gè)線程需要下載的文件的大小

上圖顯示下載400M的電影分4個(gè)線程下載,每一個(gè)線程分別下載各自數(shù)據(jù)段中的數(shù)據(jù),第一個(gè)線程下載0-100M,第二個(gè)下載100M-200M之間的數(shù)據(jù),依次類推。因此下載過程中需要記住的是的開始位置段和結(jié)束位置段,其實(shí)只需要開始位置就可以了,結(jié)束為止可以根據(jù)開始位置加上下載的大小來推斷獲取。
3.獲取到大小數(shù)據(jù)以后,開始用線程循環(huán)讀取每一個(gè)區(qū)間的數(shù)據(jù)
這個(gè)里面需要注意的是,要更新數(shù)據(jù)的寫入位置seek(startIndex),逐段填滿,不然會(huì)出現(xiàn)覆蓋以前的數(shù)據(jù)。
package com.ldw.multilthreaddownload;
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class Multidownload {
static int ThreadCount = 3; //線程的個(gè)數(shù)
static String path = "http://192.168.0.102:8080/QQ.exe"; //確定下載地址
public static void main(String[] args) {
// TODO Auto-generated method stub
//發(fā)送get請求,請求這個(gè)地址的資源
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
if(conn.getResponseCode() == 200){
//獲取到請求資源文件的長度
int length = conn.getContentLength();
File file = new File("QQ.exe");
//創(chuàng)建隨機(jī)存儲(chǔ)文件
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//設(shè)置臨時(shí)文件的大小
raf.setLength(length);
//關(guān)閉raf
raf.close();
//計(jì)算出每一個(gè)線程下載多少字節(jié)
int size = length / Multidownload.ThreadCount;
for(int i = 0; i < Multidownload.ThreadCount; i ++){
//startIndex,endIndex分別代表線程的開始和結(jié)束位置
int startIndex = i * size;
int endIndex = (i + 1) * size - 1;
if(i == ThreadCount - 1){
//如果是最后一個(gè)線程,那么結(jié)束位置寫死
endIndex = length -1;
}
System.out.println("線程" + i + "的下載區(qū)間是" + startIndex + "到" + endIndex);
new DownLoadThread(startIndex, endIndex, i).start(); //創(chuàng)建線程下載數(shù)據(jù)
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class DownLoadThread extends Thread{
int startIndex;
int endIndex;
int threadId;
public DownLoadThread(int startIndex, int endIndex, int threadId) {
super();
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadId = threadId;
}
@Override
public void run(){
//使用http請求下載安裝包文件
URL url;
try {
url = new URL(Multidownload.path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
//設(shè)置請求數(shù)據(jù)的區(qū)間
conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
//請求部分?jǐn)?shù)據(jù)的響應(yīng)碼是206
if(conn.getResponseCode() == 206){
//獲取一部分?jǐn)?shù)據(jù)來讀取
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
int total = 0;
//拿到臨時(shí)文件的引用
File file = new File("QQ.exe");
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//更新文件的寫入位置,startIndex
raf.seek(startIndex);
while((len = is.read(b)) != -1 ){
//每次讀取流里面的數(shù)據(jù),同步吧數(shù)據(jù)寫入臨時(shí)文件
raf.write(b, 0, len);
total += len;
System.out.println("線程" + threadId + "下載了" + total);
}
System.out.println("線程" + threadId + "下載過程結(jié)束===========================");
raf.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)單例模式的五種方式總結(jié)
這篇文章主要介紹了如何實(shí)現(xiàn)一個(gè)單例模式,包括構(gòu)造器私有化、提供靜態(tài)私有變量和公共獲取實(shí)例接口,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01
Springboot項(xiàng)目的服務(wù)器部署與發(fā)布方式
本文記錄了將Springboot項(xiàng)目部署到服務(wù)器并發(fā)布的過程,包括在IDEA中打包、選擇服務(wù)器、連接服務(wù)器、安裝環(huán)境、上傳jar包、配置環(huán)境變量以及運(yùn)行項(xiàng)目等步驟2025-03-03
Spring Security添加二次認(rèn)證的項(xiàng)目實(shí)踐
在用戶自動(dòng)登錄后,可以通過對密碼進(jìn)行二次校驗(yàn)進(jìn)而確保用戶的真實(shí)性,本文就來介紹一下Spring Security添加二次認(rèn)證的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
IDEA報(bào)錯(cuò):Unable to save settings Failed to save settings
這篇文章主要介紹了IDEA報(bào)錯(cuò):Unable to save settings Failed to save settings的相關(guān)知識(shí),本文給大家分享問題原因及解決方案,需要的朋友可以參考下2020-09-09
Java如何通過反射將map轉(zhuǎn)換為實(shí)體對象
在Java開發(fā)中,常需要將XML配置數(shù)據(jù)轉(zhuǎn)為Map,并最終映射到實(shí)體對象上,通過單例模式管理XML轉(zhuǎn)換后的Map,并利用Java反射機(jī)制,通過屬性名稱匹配將Map的值賦給實(shí)體對象的對應(yīng)屬性,這種方法忽略了數(shù)據(jù)類型轉(zhuǎn)換,適用于數(shù)據(jù)類型一致的簡單場景,需要類型轉(zhuǎn)換時(shí)2024-09-09

