斷點(diǎn)續(xù)傳原理,其實(shí)是利用了臨時(shí)文件保存了下載進(jìn)度,當(dāng)再次下載的時(shí)候,先讀取臨時(shí)文件里面的進(jìn)度,再下載
public class MultiThreadDown {
static int ThreadCount = 4;
static String path = "http://192.168.1.103:8080/android/pdf.exe";
static int finishedThread = 0;
public static void main(String[] args) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if (conn.getResponseCode() == 200) {
// 1.先獲取請(qǐng)求資源的大小
int length = conn.getContentLength();
File file = new File("pdf.exe");
// 生成臨時(shí)文件
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
// 設(shè)置臨時(shí)文件的大小
raf.setLength(length);
raf.close();
// 計(jì)算每個(gè)線程應(yīng)該要下載多少個(gè)字節(jié)
int size = length / ThreadCount;
for (int i = 0; i < ThreadCount; i++) {
// 計(jì)算線程下載的開(kāi)始位置和結(jié)束位置
int startIndex = i * size;
int endIndex = (i + 1) * size - 1;
// 如果是最后一個(gè)線程,那么結(jié)束位置寫(xiě)死
if (i == (ThreadCount - 1)) {
endIndex = length - 1;
}
new DownThread(startIndex, endIndex, i).start();
}
}
}
}
class DownThread extends Thread {
int startIndex;
int endIndex;
int threadId;
public DownThread(int startIndex, int endIndex, int threadId) {
super();
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadId = threadId;
}
@Override
public void run() {
// 再次發(fā)送HTTP請(qǐng)求,下載源文件
try {
// 生成一個(gè)專門(mén)用來(lái)記錄下載進(jìn)度的臨時(shí)文件
File progressFile = new File(threadId + ".txt");
if (progressFile.exists()) {
FileInputStream fis = new FileInputStream(progressFile);
BufferedReader br = new BufferedReader(new InputStreamReader(
fis));
// 從進(jìn)度臨時(shí)文件中讀取出上一次下載的總進(jìn)度,然后與原本的開(kāi)始位置相加,得到新的開(kāi)始位置
startIndex += Integer.parseInt(br.readLine());
fis.close();
}
URL url = new URL(MultiThreadDown.path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
System.out.println("線程threadId" + threadId + "下載區(qū)間:" + startIndex
+ "-" + endIndex);
// 設(shè)置請(qǐng)求的數(shù)據(jù)的區(qū)間
conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
+ endIndex);
// 請(qǐng)求部分?jǐn)?shù)據(jù),響應(yīng)碼為206
if (conn.getResponseCode() == 206) {
// 此時(shí)只有 1/threadcount數(shù)據(jù)
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
long total = 0;
File file = new File("pdf.exe");
// 這樣可以保證數(shù)據(jù)同步寫(xiě)入硬盤(pán)中,防止停電等原因da
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
// 把文件的寫(xiě)入位置移動(dòng)至startIndex
raf.seek(startIndex);
while ((len = is.read(b)) != -1) {
// 每次讀取流里的數(shù)據(jù)寫(xiě)入臨時(shí)文件
raf.write(b, 0, len);
total += len;
RandomAccessFile progressRaf = new RandomAccessFile(
progressFile, "rwd");
progressRaf.write((total + "").getBytes());
progressRaf.close();
}
raf.close();
progressFile.delete(); // 下載完成后,將臨時(shí)文件刪除
++MultiThreadDown.finishedThread;
synchronized (MultiThreadDown.path) {
if (MultiThreadDown.finishedThread == MultiThreadDown.ThreadCount) {
for (int i = 0; i < MultiThreadDown.ThreadCount; i++) {
File f = new File(i + ".txt");
f.delete();
}
MultiThreadDown.finishedThread = 0;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}