最近閑的比較蛋疼,原本軟件計劃招20個人的,現在14個人的團隊都是一半在打醬油,這跟全國經濟形勢有關,我們也沒太大辦法,所以最近是啥都看看。昨天晚上看了一個多線程斷點下載,今天就用Java實現了一遍。
基本思路:
1、通過HttpURLConnection獲取網絡資源,得到資源大小等一些信息,
2、在本地創建一個和通過網絡獲取的資源同樣大小的文件(目的是為了存放下載的資源)
3、分配每個線程下載文件的開始位置和結束位置(下載時記錄每個線程下載的起始位置,方便停止后能繼續下載)
4、開啟線程去下載。
下面開始實現
private static int threadCount = 3;//開啟3個線程
private static int blockSize = 0;//每個線程下載的大小
private static int runningTrheadCount = 0;//當前運行的線程數
private static String path = "http://sw.bos.baidu.com/sw-search-sp/software/09d9bc67eab07/QQ_8.7.19075.0_setup.exe";
private static String filename ="QQ_8.7.19075.0_setup.exe";
/**
* @param args
*/
public static void main(String[] args) {
try{
//1.請求url地址獲取服務端資源的大小
URL url = new URL(path);
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
openConnection.setRequestMethod("GET");
openConnection.setConnectTimeout(5*1000);
int code = openConnection.getResponseCode();
if(code == 200){
//獲取資源的大小
int filelength = openConnection.getContentLength();
if(filelength==-1){
filelength=1024*1024*60;
}
System.out.println("filelength="+filelength);
//2.在本地創建一個與服務端資源同樣大小的一個文件(占位)
RandomAccessFile randomAccessFile = new RandomAccessFile(filename, "rw");
randomAccessFile.setLength(filelength);//設置隨機訪問文件的大小
//3.要分配每個線程下載文件的開始位置和結束位置。
blockSize = filelength/threadCount;//計算出每個線程理論下載大小
for(int threadId =0 ;threadId < threadCount;threadId++){
int startIndex = threadId * blockSize;//計算每個線程下載的開始位置
int endIndex = (threadId+1)*blockSize -1;//計算每個線程下載的結束位置
//如果是最后一個線程,結束位置需要單獨計算
if(threadId == threadCount-1){
endIndex = filelength -1;
}
//4.開啟線程去執行下載
new DownloadThread(threadId, startIndex, endIndex).start();
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
上面代碼就是按照基本思路來寫的 首先通過請求url獲取網絡的資源,并設置為get請求,超時時間為5S 正常連接后獲取資源的大小,這里要注意一下,在百度里面輸入”qq下載“ 點立即下載
得到的下載連接為https://www.baidu.com/link?url=V4gOxFgauy-GHJGTah_Os4obVwMcRqSdtCT3zL6Lxyzg5tMfIMqK7OW_WIr8cUasrc-h9fDypXCPa2EYE8e1242fYJshFLA1BYdPKIu2KWy&wd=&eqid=b7133a01000210550000000658008fce
用chrome帶的抓包工具發現
會有三個連接而我們用直接點擊下載得到的連接其中沒有Content-Length這一項,因此當調用openConnection.getContentLength();時返回值為-1, 看網上都說設置setRequestProperty(“Accept-Encoding”, “identity”); 就可以了,可是試了下并沒什么卵用 我不是搞網絡的出身,所以搞了個簡單的方法,用下面的地址獲取資源。就是抓的包最下面一個的URL地址
http://sw.bos.baidu.com/sw-search-sp/software/84b5fcf50a3de/QQ_8.7.19083.0_setup.exe
現在可以正確的獲取網絡資源的大小了。
繼續 創建一個RandomAccessFile類型的文件,之所以要用這個類創建文件,是為了為后面的斷點續傳做準備的,看它名字也知道它 它可以隨機的開始寫入文件的位置,這個類功能非常強大,這里只用了它的randomAccessFile.seek(lastPostion)方法。
接著 就該分配線程 并確定每個線程的開始和結束位置了 這個沒什么難的,就是確定用幾個線程去下載,每個線程下載多少,隨你怎么分配了,只要他們連起來還是資源的大小,并且是連續的就行。
接下來就是開啟線程去下載了。
public static class DownloadThread extends Thread{
private int threadId;
private int startIndex;
private int endIndex;
private int lastPostion;
public DownloadThread(int threadId,int startIndex,int endIndex){
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
synchronized (DownloadThread.class) {
runningTrheadCount = runningTrheadCount +1;//開啟一線程,線程數加1
}
//分段請求網絡連接,分段保存文件到本地
try{
URL url = new URL(path);
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
openConnection.setRequestMethod("GET");
openConnection.setConnectTimeout(5*1000);
System.out.println("理論上下載: 線程:"+threadId+",開始位置:"+startIndex+";結束位置:"+endIndex);
//讀取上次下載結束的位置,本次從這個位置開始直接下載。
File file2 = new File(threadId+".txt");
if(file2.exists()){
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file2)));
String lastPostion_str = bufferedReader.readLine();
lastPostion = Integer.parseInt(lastPostion_str);//讀取文件獲取上次下載的位置
//設置分段下載的頭信息。 Range:做分段數據請求用的。
openConnection.setRequestProperty("Range", "bytes="+lastPostion+"-"+endIndex);//bytes:0-500:請求服務器資源中0-500之間的字節信息 501-1000:
System.out.println("實際下載1: 線程:"+threadId+",開始位置:"+lastPostion+";結束位置:"+endIndex);
bufferedReader.close();
}else{
lastPostion = startIndex;
//設置分段下載的頭信息。 Range:做分段數據請求用的。
openConnection.setRequestProperty("Range", "bytes="+lastPostion+"-"+endIndex);//bytes:0-500:請求服務器資源中0-500之間的字節信息 501-1000:
System.out.println("實際下載2: 線程:"+threadId+",開始位置:"+lastPostion+";結束位置:"+endIndex);
}
System.out.println("getResponseCode"+openConnection.getResponseCode() );
if(openConnection.getResponseCode() == 206){//200:請求全部資源成功, 206代表部分資源請求成功
InputStream inputStream = openConnection.getInputStream();
//請求成功將流寫入本地文件中,已經創建的占位那個文件中
RandomAccessFile randomAccessFile = new RandomAccessFile(filename, "rw");
randomAccessFile.seek(lastPostion);//設置隨機文件從哪個位置開始寫。
//將流中的數據寫入文件
byte[] buffer = new byte[1024];
int length = -1;
int total = 0;//記錄本次線程下載的總大小
while((length= inputStream.read(buffer)) !=-1){
randomAccessFile.write(buffer, 0, length);
total = total+ length;
//去保存當前線程下載的位置,保存到文件中
int currentThreadPostion = lastPostion + total;//計算出當前線程本次下載的位置
//創建隨機文件保存當前線程下載的位置
File file = new File(threadId+".txt");
RandomAccessFile accessfile = new RandomAccessFile(file, "rwd");
accessfile.write(String.valueOf(currentThreadPostion).getBytes());
accessfile.close();
}
//關閉相關的流信息
inputStream.close();
randomAccessFile.close();
System.out.println("線程:"+threadId+",下載完畢");
//當所有線程下載結束,刪除存放下載位置的文件。
synchronized (DownloadThread.class) {
runningTrheadCount = runningTrheadCount -1;//標志著一個線程下載結束。
if(runningTrheadCount == 0 ){
System.out.println("所有線程下載完成");
for(int i =0 ;i< threadCount;i++){
File file = new File(i+".txt");
System.out.println(file.getAbsolutePath());
file.delete();
}
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
super.run();
}
}
上面代碼主要做了4 件事
1、設置分段下載的頭信息;
2、分段下載網絡資源
3、當中斷時把當前各個線程當前下載的位置分別保存到一個臨時文件中
4、下載完成后把臨時文件刪除 上面代碼中都給出了詳細的注釋
其中有一點要注意
openConnection.setRequestProperty("Range", "bytes="+lastPostion+"-"+endIndex);
如果"bytes=格式不對的話會導致設置不成功,返回的將不是部分資源的返回碼
另一個要說明的就是randomAccessFile.seek(startThread);是設置各個線程下載的開始位置
現在的開源項目xutils也可以實現多線程斷點下載 不過還是附上demo吧
https://github.com/solary2014/Muchdownload
http://download.csdn.net/detail/asd1031/9654085