知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
多線程斷點(diǎn)下載原理(java代碼實(shí)例演示)
發(fā)表時(shí)間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):39
其實(shí)多線程斷點(diǎn)下載道理,很簡(jiǎn)單的,那么我們就來先懂得下,若何實(shí)現(xiàn)多線程的斷點(diǎn)下載,起首:你必須明白第一點(diǎn),那么就是,什么是多線程下載,該常識(shí)點(diǎn)可以查看本博客上一篇文┞仿,Android之多線程下載道理,斷點(diǎn)下載呢,其實(shí)就是在這個(gè)的基本之上添加了一些器械,那么添加了什么器械了,如今來做一個(gè)具體的懂得。
1.鄙人載的過程中,邊下載,變用一個(gè)文件來記錄下載的地位,也就是下載了若干的數(shù)據(jù)
1.創(chuàng)建文件
2.記錄下載若干癟據(jù)
3.存儲(chǔ)數(shù)據(jù)
2.第二次下載的時(shí)刻,就去攫取文件中是否存稀有據(jù),攫取前次下載的地位,作為此次開端下載的地位
1.創(chuàng)建文件對(duì)象
2.考驗(yàn)是否有次文件和文件瑯綾擎是否稀有據(jù)
3.攫取數(shù)據(jù),將數(shù)據(jù)拿給此次的開端地位,也就是大年夜這個(gè)數(shù)據(jù)這里開端下載
3.文件下載完成之后,將記錄的文件刪除,必定要下載完成之后,在將文件刪除,不然會(huì)彪炳一些異常,比如,此次文件沒了,就要從新開端下載等等
4.以上說了這些是不是稍微明白了些,那么下面來看看真正的實(shí)踐吧
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;
/**
* 文件下載器
*
* @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;
}
/**
* 去辦事器端下載文件
*
* @param path
* 辦事器地址
*/
public void downFile(String path) {
// 去辦事器端獲取文件的長(zhǎng)度,在本地創(chuàng)建一個(gè)跟辦事器一樣大年夜小的文件
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.獲取辦事器端文件的長(zhǎng)度
int fileLength = connection.getContentLength();
// 2.本地創(chuàng)建一個(gè)跟辦事器一樣大年夜小的文件
RandomAccessFile raf = new RandomAccessFile("setup.exe", "rwd");
raf.setLength(fileLength);
raf.close();
// 3.假設(shè)三個(gè)線程下載
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 假設(shè)下載
System.out.println("假設(shè)線程:" + threadId + ",下載:" + startIndex
+ "--->" + endIndex);
// 4.開端下載
new DownLoadThread(threadId, startIndex, endIndex, path)
.start();
}
System.out.println("文件總長(zhǎng)度為:" + fileLength);
} else {
System.out.println("請(qǐng)求掉?。?quot;);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下載文件的線程
*
* @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
* 線程下載開端地位
* @param endIndex
* 線程下載停止地位
* @param path
* 線程下載停止文件放置地址
*/
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.考驗(yàn)是否有存的記錄 -------------------------------------------------------------------------------------------------
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ǐng)求辦事器下載部分的文件,制訂開端的地位,和停止地位
connection.setRequestProperty("Range", "bytes=" + startIndex
+ "-" + endIndex);
// log 真實(shí)下載
System.out.println("真實(shí)線程:" + threadId + ",下載:" + startIndex
+ "--->" + endIndex);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
// 3.大年夜辦事器獲取的全部數(shù)據(jù),返回:200,大年夜辦事器獲取部分?jǐn)?shù)據(jù),返回:206
int code = connection.getResponseCode();
System.out.println("code = " + code);
InputStream is = connection.getInputStream();
RandomAccessFile raf = new RandomAccessFile("setup.exe", "rwd");
raf.seek(startIndex); // 隨機(jī)寫文件的時(shí)刻,大年夜什么時(shí)刻開端
int len = 0;
int total = 0; // 記錄下載若干 -----------------------------------------
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.存數(shù)據(jù):(真正下載到開端的地位)下載的+開端的----------------------------------------
info.close();
}
is.close();
raf.close();
System.out.println("線程:" + threadId + ",下載完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.notice必定要文件都下載完畢之后再將記錄文件刪除
runningThread--;
if (runningThread == 0) {
for (int i = 1; i <= threadCount; i++) {
File file = new File(i + ".txt");
file.delete();
}
System.out.println("文件下載完畢,刪除記錄文件"); ---------------------------------------------------------------------
}
}
}
}
}
相關(guān)案例查看更多
相關(guān)閱讀
- 專業(yè)網(wǎng)站建設(shè)公司
- 昆明小程序公司
- 云南網(wǎng)站優(yōu)化公司
- 開發(fā)制作小程序
- 云南網(wǎng)站建設(shè)首頁(yè)
- 網(wǎng)站建設(shè)首選
- 分銷系統(tǒng)
- 網(wǎng)站建設(shè)高手
- 云南網(wǎng)站建設(shè)選
- 怎么做網(wǎng)站
- 網(wǎng)站優(yōu)化哪家好
- 小程序被騙
- 大理小程序開發(fā)
- 網(wǎng)絡(luò)公司報(bào)價(jià)
- 云南微信小程序開發(fā)
- 云南小程序制作
- 云南網(wǎng)站建設(shè)高手
- 網(wǎng)站建設(shè)首頁(yè)
- 云南軟件開發(fā)
- 小程序開發(fā)平臺(tái)前十名
- 云南etc小程序
- 昆明網(wǎng)站開發(fā)
- APP
- 網(wǎng)站上首頁(yè)
- 云南建設(shè)廳網(wǎng)站
- 大理網(wǎng)站建設(shè)公司
- 微信小程序
- painter
- 服務(wù)器
- 搜索引擎優(yōu)化