多線程斷點下(xià)載原理(java代碼實例演示)
發表時間:2020-10-19
發布人:葵宇科技
浏覽次數:27
其實多線程斷點下(xià)載道理,很簡單的,那麼我們就來先懂得下(xià),若何實現多線程的斷點下(xià)載,起首:你(nǐ)必須明白第一點,那麼就是,什麼是多線程下(xià)載,該常識點可(kě)以查看本博客上一篇文(wén)┞仿,Android之多線程下(xià)載道理,斷點下(xià)載呢(ne),其實就是在這個(gè)的基本之上添加了一些器(qì)械,那麼添加了什麼器(qì)械了,如(rú)今來做一個(gè)具體的懂得。
1.鄙人載的過程中(zhōng),邊下(xià)載,變用一個(gè)文(wén)件來記錄下(xià)載的地位,也就是下(xià)載了若幹的數據
1.創建文(wén)件
2.記錄下(xià)載若幹癟據
3.存儲數據
2.第二次下(xià)載的時刻,就去攫取文(wén)件中(zhōng)是否存稀有據,攫取前次下(xià)載的地位,作為此次開端下(xià)載的地位
1.創建文(wén)件對象
2.考驗是否有次文(wén)件和(hé)文(wén)件琅绫擎是否稀有據
3.攫取數據,将數據拿給此次的開端地位,也就是大年夜這個(gè)數據這裡開端下(xià)載
3.文(wén)件下(xià)載完成之後,将記錄的文(wén)件删除,必定要下(xià)載完成之後,在将文(wén)件删除,不然會彪炳一些異常,比如(rú),此次文(wén)件沒了,就要從新開端下(xià)載等等
4.以上說了這些是不是稍微明白了些,那麼下(xià)面來看看真正的實踐吧
package com.zengtao.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 文(wén)件下(xià)載器(qì)
*
* @author Administrator zengtao
*
*/
public class DemoLoader {
private static DemoLoader loader = new DemoLoader();
private static int threadCount = 3;
private static int runningThread = 3;
private DemoLoader() {
}
public static DemoLoader getInstance() {
return loader;
}
/**
* 去辦事器(qì)端下(xià)載文(wén)件
*
* @param path
* 辦事器(qì)地址
*/
public void downFile(String path) {
// 去辦事器(qì)端獲取文(wén)件的長度,在本地創建一個(gè)跟辦事器(qì)一樣大年夜小的文(wén)件
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
int code = connection.getResponseCode();
if (code == 200) {
// 1.獲取辦事器(qì)端文(wén)件的長度
int fileLength = connection.getContentLength();
// 2.本地創建一個(gè)跟辦事器(qì)一樣大年夜小的文(wén)件
RandomAccessFile raf = new RandomAccessFile("setup.exe", "rwd");
raf.setLength(fileLength);
raf.close();
// 3.假設三個(gè)線程下(xià)載
int blockSize = fileLength / threadCount;
for (int threadId = 0; threadId < threadCount; threadId++) {
int startIndex = (threadId - 1) * blockSize;
int endIndex = threadId * blockSize - 1;
if (threadId == threadCount) {
endIndex = fileLength;
}
// log 假設下(xià)載
System.out.println("假設線程:" + threadId + ",下(xià)載:" + startIndex
+ "--->" + endIndex);
// 4.開端下(xià)載
new DownLoadThread(threadId, startIndex, endIndex, path)
.start();
}
System.out.println("文(wén)件總長度為:" + fileLength);
} else {
System.out.println("請求掉敗!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下(xià)載文(wén)件的線程
*
* @author Administrator zengtao
*
*/
public class DownLoadThread extends Thread {
private int threadId;
private int startIndex;
private int endIndex;
private String path;
/**
*
* @param threadId
* 線程id
* @param startIndex
* 線程下(xià)載開端地位
* @param endIndex
* 線程下(xià)載停止地位
* @param path
* 線程下(xià)載停止文(wén)件放置地址
*/
public DownLoadThread(int threadId, int startIndex, int endIndex,
String path) {
super();
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.path = path;
}
@Override
public void run() {
super.run();
try {
// 1.考驗是否有存的記錄 -------------------------------------------------------------------------------------------------
File file = new File(threadId + ".txt");
if (file.exists() && file.length() > 0) {
FileInputStream fis = new FileInputStream(file);
byte[] temp = new byte[1024];
int leng = fis.read(temp);
String loadLength = new String(temp, 0, leng);
int load = Integer.parseInt(loadLength);
startIndex = load;
fis.close();
}
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
// 2.請求辦事器(qì)下(xià)載部分的文(wén)件,制訂開端的地位,和(hé)停止地位
connection.setRequestProperty("Range", "bytes=" + startIndex
+ "-" + endIndex);
// log 真實下(xià)載
System.out.println("真實線程:" + threadId + ",下(xià)載:" + startIndex
+ "--->" + endIndex);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
// 3.大年夜辦事器(qì)獲取的全部數據,返回:200,大年夜辦事器(qì)獲取部分數據,返回:206
int code = connection.getResponseCode();
System.out.println("code = " + code);
InputStream is = connection.getInputStream();
RandomAccessFile raf = new RandomAccessFile("setup.exe", "rwd");
raf.seek(startIndex); // 随機寫文(wén)件的時刻,大年夜什麼時刻開端
int len = 0;
int total = 0; // 記錄下(xià)載若幹 -----------------------------------------
byte[] buff = new byte[1024];
while ((len = is.read(buff)) != -1) {
RandomAccessFile info = new RandomAccessFile(threadId
+ ".txt", "rwd");
raf.write(buff, 0, len);
total += len;
info.write(("" + startIndex + total).getBytes()); // 4.存數據:(真正下(xià)載到開端的地位)下(xià)載的+開端的----------------------------------------
info.close();
}
is.close();
raf.close();
System.out.println("線程:" + threadId + ",下(xià)載完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.notice必定要文(wén)件都下(xià)載完畢之後再将記錄文(wén)件删除
runningThread--;
if (runningThread == 0) {
for (int i = 1; i <= threadCount; i++) {
File file = new File(i + ".txt");
file.delete();
}
System.out.println("文(wén)件下(xià)載完畢,删除記錄文(wén)件"); ---------------------------------------------------------------------
}
}
}
}
}