Java / Android 基於Http的多線程下載的實現

來源:互聯網
上載者:User

轉載請標明出處:http://blog.csdn.net/lmj623565791/article/details/26994463

有個朋友需要個多線程現在的例子,就幫忙實現了,在此分享下~

先說下原理,原理明白了,其實很簡單:

a、對於網路上的一個資源,首先發送一個請求,從返回的Content-Length中回去需要下載檔案的大小,然後根據檔案大小建立一個檔案。

this.fileSize = conn.getContentLength();// 根據響應擷取檔案大小File dir = new File(dirStr);this.localFile = new File(dir, filename);RandomAccessFile raf = new RandomAccessFile(this.localFile, "rw");raf.setLength(fileSize);raf.close();

b、根據線程數和檔案大小,為每個線程分配下載的位元組區間,然後每個線程向伺服器發送請求,擷取這段位元組區間的檔案內容。

conn.setRequestProperty("Range", "bytes=" + startPos + "-"+ endPos);// 設定擷取實體資料的範圍

c、利用RandomAccessFile的seek方法,多線程同時往一個檔案中寫入位元組。

raf.seek(startPos);while ((len = is.read(buf)) != -1){raf.write(buf, 0, len);}
分析完了原理就很簡單了,我封裝了一個類,利用這個類的執行個體進行下載,所需參數:下載資源的URI, 本地檔案路徑,線程的數量。

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;}@Overridepublic 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();}}}}

createFileByUrl方法,就是我們上述的原理的步驟1,得到檔案大小和建立本地檔案。我在程式使用了一個內部類DownloadThread繼承Thread,專門負責下載。download()方法,根據線程數量和檔案大小計算每個線程需要下載的位元組區間,然後開啟線程去下載。

伺服器端:我就扔了幾個檔案在Tomcat根目錄做實驗,下面是測試代碼:

package com.zhy.mutilthread_download;import java.io.IOException;public class Test{public static void main(String[] args){try{new MultipartThreadDownloador("http://localhost:8080/nexus.zip","f:/backup/nexus", "nexus.zip", 2).download();} catch (IOException e){e.printStackTrace();}}}

輸出結果:

需要下載的檔案大小為 :31143237 , 儲存位置為: f:/backup/nexus/nexus.zipThread-1開始下載...Thread-2開始下載...Thread-3開始下載...Thread-4開始下載...Thread-4完成下載  : 23357430 -- 31143237Thread-2完成下載  : 7785810 -- 15571619Thread-1完成下載  : 0 -- 7785809Thread-3完成下載  : 15571620 -- 23357429

:



ok,多線程下載介紹完畢,如果代碼設計不合理,以及方法使用錯誤,歡迎各位留言,,,



源碼點擊下載



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.