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è)子線程分別去下載對應(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());
}
}
}
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java 實(shí)現(xiàn)截取字符串并按字節(jié)分別輸出實(shí)例代碼
這篇文章主要介紹了java 實(shí)現(xiàn)截取字符串并按字節(jié)分別輸出實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
Mybatis注解實(shí)現(xiàn)多數(shù)據(jù)源讀寫分離詳解
這篇文章主要給大家介紹了關(guān)于Mybatis注解實(shí)現(xiàn)多數(shù)據(jù)源讀寫分離的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Java spring webmvc如何實(shí)現(xiàn)控制反轉(zhuǎn)
這篇文章主要介紹了Java spring webmvc如何實(shí)現(xiàn)控制反轉(zhuǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Java實(shí)現(xiàn)自定義中文排序的方法機(jī)注意事項(xiàng)
在Java中,中文排序通常涉及到使用Collator類來處理字符串的比較,確保根據(jù)漢字的拼音順序進(jìn)行排序,本文給大家介紹了Java實(shí)現(xiàn)自定義中文排序的方法機(jī)注意事項(xiàng),并有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-10-10

