今天就跟大家聊聊有關Java中如何實現(xiàn)下載多線程文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)建站是一家集成都網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)站頁面設計、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)網(wǎng)站制作公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設服務。追求良好的瀏覽體驗,以探求精品塑造與理念升華,設計最適合用戶的網(wǎng)站頁面。 合作只是第一步,服務才是根本,我們始終堅持講誠信,負責任的原則,為您進行細心、貼心、認真的服務,與眾多客戶在蓬勃發(fā)展的市場環(huá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地址,保存的文件地址,切割后的文件開始位置和結束位置,這樣我們就能把分段文件下載到本地。并且這個方法要是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í)行請求,每條線程的請求開始位置和結束位置都不同,本地存儲的文件開始位置和請求開始位置相同,這樣就可以實現(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;
}看完上述內容,你們對Java中如何實現(xiàn)下載多線程文件有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享文章:Java中如何實現(xiàn)下載多線程文件
本文URL:http://chinadenli.net/article32/jigspc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號、面包屑導航、外貿網(wǎng)站建設、定制開發(fā)、網(wǎng)站收錄、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)