今天就跟大家聊聊有關(guān)Java中如何實(shí)現(xiàn)下載多線程文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)建站是一家集成都網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)站頁(yè)面設(shè)計(jì)、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)網(wǎng)站制作公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設(shè)服務(wù)。追求良好的瀏覽體驗(yàn),以探求精品塑造與理念升華,設(shè)計(jì)最適合用戶的網(wǎng)站頁(yè)面。 合作只是第一步,服務(wù)才是根本,我們始終堅(jiān)持講誠(chéng)信,負(fù)責(zé)任的原則,為您進(jìn)行細(xì)心、貼心、認(rèn)真的服務(wù),與眾多客戶在蓬勃發(fā)展的市場(chǎng)環(huán)境中,互促共生。
Java實(shí)現(xiàn)多線程文件下載思路:
1、基本思路是將文件分段切割、分段傳輸、分段保存。
2、分段切割用到HttpUrlConnection對(duì)象的setRequestProperty("Range", "bytes=" + start + "-" + end)方法。
3、分段傳輸用到HttpUrlConnection對(duì)象的getInputStream()方法。
4、分段保存用到RandomAccessFile的seek(int start)方法。
5、創(chuàng)建指定長(zhǎng)度的線程池,循環(huán)創(chuàng)建線程,執(zhí)行下載操作。
首先,我們要先寫一個(gè)方法,方法的參數(shù)包含URL地址,保存的文件地址,切割后的文件開始位置和結(jié)束位置,這樣我們就能把分段文件下載到本地。并且這個(gè)方法要是run方法,這樣我們啟動(dòng)線程時(shí)就直接執(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)建線程池,線程池的長(zhǎng)度可以自定義,然后循環(huán)創(chuàng)建線程來(lái)執(zhí)行請(qǐng)求,每條線程的請(qǐng)求開始位置和結(jié)束位置都不同,本地存儲(chǔ)的文件開始位置和請(qǐng)求開始位置相同,這樣就可以實(shí)現(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)容,你們對(duì)Java中如何實(shí)現(xiàn)下載多線程文件有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享文章:Java中如何實(shí)現(xiàn)下載多線程文件
本文URL:http://www.chinadenli.net/article32/jigspc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、面包屑導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)、定制開發(fā)、網(wǎng)站收錄、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)