Java實(shí)現(xiàn)多線程斷點(diǎn)下載
更新時(shí)間:2018年03月14日 17:18:34 投稿:lijiao
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多線程斷點(diǎn)下載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
JAVA多線程斷點(diǎn)下載原理如圖:

代碼如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class MutileThreadDownload {
/**
* 線程的數(shù)量
*/
private static int threadCount = 3;
/**
* 每個(gè)下載區(qū)塊的大小
*/
private static long blocksize;
/**
* 正在運(yùn)行的線程的數(shù)量
*/
private static int runningThreadCount;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 服務(wù)器文件的路徑
String path = "http://192.168.1.100:8080/ff.exe";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
long size = conn.getContentLength();// 得到服務(wù)端返回的文件的大小
System.out.println("服務(wù)器文件的大?。? + size);
blocksize = size / threadCount;
// 1.首先在本地創(chuàng)建一個(gè)大小跟服務(wù)器一模一樣的空白文件。
File file = new File("temp.exe");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.setLength(size);
// 2.開啟若干個(gè)子線程分別去下載對(duì)應(yīng)的資源。
runningThreadCount = threadCount;
for (int i = 1; i <= threadCount; i++) {
long startIndex = (i - 1) * blocksize;
long endIndex = i * blocksize - 1;
if (i == threadCount) {
// 最后一個(gè)線程
endIndex = size - 1;
}
System.out.println("開啟線程:" + i + "下載的位置:" + startIndex + "~"
+ endIndex);
new DownloadThread(path, i, startIndex, endIndex).start();
}
}
conn.disconnect();
}
private static class DownloadThread extends Thread {
private int threadId;
private long startIndex;
private long endIndex;
private String path;
public DownloadThread(String path, int threadId, long startIndex,
long endIndex) {
this.path = path;
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
try {
// 當(dāng)前線程下載的總大小
int total = 0;
File positionFile = new File(threadId + ".txt");
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
// 接著從上一次的位置繼續(xù)下載數(shù)據(jù)
if (positionFile.exists() && positionFile.length() > 0) {// 判斷是否有記錄
FileInputStream fis = new FileInputStream(positionFile);
BufferedReader br = new BufferedReader(
new InputStreamReader(fis));
// 獲取當(dāng)前線程上次下載的總大小是多少
String lasttotalstr = br.readLine();
int lastTotal = Integer.valueOf(lasttotalstr);
System.out.println("上次線程" + threadId + "下載的總大?。?
+ lastTotal);
startIndex += lastTotal;
total += lastTotal;// 加上上次下載的總大小。
fis.close();
}
conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
+ endIndex);
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
System.out.println("code=" + code);
InputStream is = conn.getInputStream();
File file = new File("temp.exe");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// 指定文件開始寫的位置。
raf.seek(startIndex);
System.out.println("第" + threadId + "個(gè)線程:寫文件的開始位置:"
+ String.valueOf(startIndex));
int len = 0;
byte[] buffer = new byte[512];
while ((len = is.read(buffer)) != -1) {
RandomAccessFile rf = new RandomAccessFile(positionFile,
"rwd");
raf.write(buffer, 0, len);
total += len;
rf.write(String.valueOf(total).getBytes());
rf.close();
}
is.close();
raf.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 只有所有的線程都下載完畢后 才可以刪除記錄文件。
synchronized (MutileThreadDownload.class) {
System.out.println("線程" + threadId + "下載完畢了");
runningThreadCount--;
if (runningThreadCount < 1) {
System.out.println("所有的線程都工作完畢了。刪除臨時(shí)記錄的文件");
for (int i = 1; i <= threadCount; i++) {
File f = new File(i + ".txt");
System.out.println(f.delete());
}
}
}
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java后端實(shí)現(xiàn)生成驗(yàn)證碼圖片的示例代碼
驗(yàn)證碼是一種用于驗(yàn)證用戶身份或確保用戶操作安全的技術(shù)手段,通常以圖形、聲音或文字的形式出現(xiàn),本文主要介紹了如何通過java實(shí)現(xiàn)生成驗(yàn)證碼圖片,需要的可以參考下2023-12-12
java循環(huán)刪除List元素報(bào)錯(cuò)的原因分析與解決
大家在工作中應(yīng)該都會(huì)遇到從List集合中刪除某一個(gè)或多個(gè)元素的業(yè)務(wù)場(chǎng)景,相信大家都會(huì)避開在循環(huán)里面刪除元素,使用其他方式處理,這是為什么呢,下面小編就來(lái)和大家詳細(xì)聊聊2023-11-11
Springboot?maven項(xiàng)目配置文件覆蓋問題的處理
這篇文章主要介紹了Springboot?maven項(xiàng)目配置文件覆蓋問題的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
MyBatis 動(dòng)態(tài)拼接Sql字符串的問題
MyBatis的動(dòng)態(tài)SQL,解決了SQL字符串拼接的痛苦。下文分步驟給大家詳細(xì)介紹了MyBatis 動(dòng)態(tài)拼接Sql字符串的問題,非常不錯(cuò),感興趣的朋友一起看下吧2016-08-08
eclipse創(chuàng)建多層包(多級(jí)包)全過程
這篇文章主要介紹了eclipse創(chuàng)建多層包(多級(jí)包)全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

