1. 程式人生 > >Http的多執行緒下載的實現

Http的多執行緒下載的實現

a、對於網路上的一個資源,首先發送一個請求,從返回的Content-Length中回去需要下載檔案的大小,然後根據檔案大小建立一個檔案。
b、根據執行緒數和檔案大小,為每個執行緒分配下載的位元組區間,然後每個執行緒向伺服器傳送請求,獲取這段位元組區間的檔案內容。
c、利用RandomAccessFile的seek方法,多執行緒同時往一個檔案中寫入位元組。

【HTTP之Range】

 1、什麼是Range?

  當用戶在聽一首歌的時候,如果聽到一半(網路下載了一半),網路斷掉了,使用者需要繼續聽的時候,檔案伺服器不支援斷點的話,則使用者需要重新下載這個檔案。而Range支援的話,客戶端應該記錄了之前已經讀取的檔案範圍,網路恢復之後,則向伺服器傳送讀取剩餘Range的請求,服務端只需要傳送客戶端請求的那部分內容,而不用整個檔案傳送回客戶端,以此節省網路頻寬。

 2、HTTP1.1規範的Range是怎樣一個約定呢?

  如果Server支援Range,首先就要告訴客戶端,咱支援Range,之後客戶端才可能發起帶Range的請求。這裡套用唐僧的一句話,你不說我怎麼知道呢。response.setHeader(‘Accept-Ranges’, ‘bytes’);

  Server通過請求頭中的Range: bytes=0-xxx來判斷是否是做Range請求,如果這個值存在而且有效,則只發回請求的那部分檔案內容,響應的狀態碼變成206,表示Partial Content,並設定Content-Range。如果無效,則返回416狀態碼,表明Request Range Not Satisfiable(

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17 )。如果不包含Range的請求頭,則繼續通過常規的方式響應。

package com.zhy.mutilthread_download;  

import java.io.File;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.RandomAccessFile;  
import java.net.HttpURLConnection;  
import java.net.URL;  

public
class MultipartThreadDownloador { /** * 需要下載資源的地址 */ private String urlStr; /** * 下載的檔案 */ private File localFile; /** * 需要下載檔案的存放的本地資料夾路徑 */ private String dirStr; /** * 儲存到本地的檔名 */ private String filename; /** * 開啟的執行緒數量 */ private int threadCount; /** * 下載檔案的大小 */ private long fileSize; public MultipartThreadDownloador(String urlStr, String dirStr, String filename, int threadCount) { this.urlStr = urlStr; this.dirStr = dirStr; this.filename = filename; this.threadCount = threadCount; } public void download() throws IOException { createFileByUrl(); /** * 計算每個執行緒需要下載的資料長度 */ long block = fileSize % threadCount == 0 ? fileSize / threadCount : fileSize / threadCount + 1; for (int i = 0; i < threadCount; i++) { long start = i * block; long end = start + block >= fileSize ? fileSize : start + block - 1; new DownloadThread(new URL(urlStr), localFile, start, end).start(); } } /** * 根據資源的URL獲取資源的大小,以及在本地建立檔案 */ public void createFileByUrl() throws IOException { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(15 * 1000); conn.setRequestMethod("GET"); conn.setRequestProperty( "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Referer", urlStr); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.connect(); if (conn.getResponseCode() == 200) { this.fileSize = conn.getContentLength();// 根據響應獲取檔案大小 if (fileSize <= 0) throw new RuntimeException( "the file that you download has a wrong size ... "); File dir = new File(dirStr); if (!dir.exists()) dir.mkdirs(); this.localFile = new File(dir, filename); RandomAccessFile raf = new RandomAccessFile(this.localFile, "rw"); raf.setLength(fileSize); raf.close(); System.out.println("需要下載的檔案大小為 :" + this.fileSize + " , 儲存位置為: " + dirStr + "/" + filename); } else { throw new RuntimeException("url that you conneted has error ..."); } } private class DownloadThread extends Thread { /** * 下載檔案的URI */ private URL url; /** * 存的本地路徑 */ private File localFile; /** * 是否結束 */ private boolean isFinish; /** * 開始的位置 */ private Long startPos; /** * 結束位置 */ private Long endPos; public DownloadThread(URL url, File savefile, Long startPos, Long endPos) { this.url = url; this.localFile = savefile; this.startPos = startPos; this.endPos = endPos; } @Override public void run() { System.out.println(Thread.currentThread().getName() + "開始下載..."); try { HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setConnectTimeout(15 * 1000); conn.setRequestMethod("GET"); conn.setRequestProperty( "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Referer", url.toString()); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);// 設定獲取實體資料的範圍 conn.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.connect(); /** * 代表伺服器已經成功處理了部分GET請求 */ if (conn.getResponseCode() == 206) { InputStream is = conn.getInputStream(); int len = 0; byte[] buf = new byte[1024]; RandomAccessFile raf = new RandomAccessFile(localFile, "rwd"); raf.seek(startPos); while ((len = is.read(buf)) != -1) { raf.write(buf, 0, len); } raf.close(); is.close(); System.out.println(Thread.currentThread().getName() + "完成下載 : " + startPos + " -- " + endPos); this.isFinish = true; } else { throw new RuntimeException( "url that you conneted has error ..."); } } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { try { new MultipartThreadDownloador("http://img.firefoxchina.cn/2016/09/5/201609141103140.jpg", "D:\\KwDownload", "dd.jpg", 2).download(); } catch (IOException e) { e.printStackTrace(); } } }