Java實現(xiàn)多線程文件下載的代碼示例
Java實現(xiàn)多線程文件下載思路:
1、基本思路是將文件分段切割、分段傳輸、分段保存。
2、分段切割用到HttpUrlConnection對象的setRequestProperty("Range", "bytes=" + start + "-" + end)方法。
3、分段傳輸用到HttpUrlConnection對象的getInputStream()方法。
4、分段保存用到RandomAccessFile的seek(int start)方法。
5、創(chuàng)建指定長度的線程池,循環(huán)創(chuàng)建線程,執(zhí)行下載操作。
首先,我們要先寫一個方法,方法的參數(shù)包含URL地址,保存的文件地址,切割后的文件開始位置和結(jié)束位置,這樣我們就能把分段文件下載到本地。并且這個方法要是run方法,這樣我們啟動線程時就直接執(zhí)行該方法。
public class DownloadWithRange implements Runnable { private String urlLocation; private String filePath; private long start; private long end; DownloadWithRange(String urlLocation, String filePath, long start, long end) { this.urlLocation = urlLocation; this.filePath = filePath; this.start = start; this.end = end; } @Override public void run() { try { HttpURLConnection conn = getHttp(); conn.setRequestProperty("Range", "bytes=" + start + "-" + end); File file = new File(filePath); RandomAccessFile out = null; if (file != null) { out = new RandomAccessFile(file, "rwd"); } out.seek(start); InputStream in = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; while ((len = in.read(b)) != -1) { out.write(b, 0, len); } in.close(); out.close(); } catch (Exception e) { e.getMessage(); } } public HttpURLConnection getHttp() throws IOException { URL url = null; if (urlLocation != null) { url = new URL(urlLocation); } HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); return conn; } }
然后我們創(chuàng)建線程池,線程池的長度可以自定義,然后循環(huán)創(chuàng)建線程來執(zhí)行請求,每條線程的請求開始位置和結(jié)束位置都不同,本地存儲的文件開始位置和請求開始位置相同,這樣就可以實現(xiàn)多線程下載了。
public class DownloadFileWithThreadPool { public void getFileWithThreadPool(String urlLocation,String filePath, int poolLength) throws IOException { Executor threadPool = Executors.newFixedThreadPool(poolLength); long len = getContentLength(urlLocation); for(int i=0;i<poolLength;i++) { long start=i*len/poolLength; long end = (i+1)*len/poolLength-1; if(i==poolLength-1) { end =len; } DownloadWithRange download=new DownloadWithRange(urlLocation, filePath, start, end); threadPool.execute(download); } } public static long getContentLength(String urlLocation) throws IOException { URL url = null; if (urlLocation != null) { url = new URL(urlLocation); } HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); long len = conn.getContentLength(); return len; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Spring中singleton?bean如何同時服務(wù)多個請求
這篇文章主要介紹了詳解Spring中singleton?bean如何同時服務(wù)多個請求2023-02-02淺談synchronized加鎖this和class的區(qū)別
synchronized 是 Java 語言中處理并發(fā)問題的一種常用手段,本文主要介紹了synchronized加鎖this和class的區(qū)別,具有一定的參考價值,感興趣的可以了解一下2021-11-11SpringBoot actuator 健康檢查不通過的解決方案
這篇文章主要介紹了SpringBoot actuator 健康檢查不通過的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07詳解SpringMVC組件之HandlerMapping(一)
這篇文章主要介紹了詳解SpringMVC組件之HandlerMapping(一),HandlerMapping組件是Spring?MVC核心組件,用來根據(jù)請求的request查找對應(yīng)的Handler,在Spring?MVC中,有各式各樣的Web請求,每個請求都需要一個對應(yīng)的Handler來處理,需要的朋友可以參考下2023-08-08Spring集成MyBatis和PageHelper分頁插件整合過程詳解
Spring?整合?MyBatis?是將?MyBatis?數(shù)據(jù)訪問框架與?Spring?框架進行集成,以實現(xiàn)更便捷的開發(fā)和管理,在集成過程中,Spring?提供了許多特性和功能,如依賴注入、聲明式事務(wù)管理、AOP?等,這篇文章主要介紹了Spring集成MyBatis和PageHelper分頁插件整合,需要的朋友可以參考下2023-08-08MyBatis-Plus實現(xiàn)條件查詢的三種格式例舉詳解
本文主要介紹了MyBatis-Plus三中條件查詢格式的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08