java多線程實現(xiàn)文件下載功能
多線程下載文件的思路:
1.首先獲取到文件的總大小
獲取文件大小的方式是通過網(wǎng)絡(luò)讀取,getContentLength()即可獲取到文件的大小,使用RandomAccessFile()支持隨機(jī)訪問
2.根據(jù)所準(zhǔn)備的線程數(shù)據(jù),計算每一個線程需要下載的文件的大小
上圖顯示下載400M的電影分4個線程下載,每一個線程分別下載各自數(shù)據(jù)段中的數(shù)據(jù),第一個線程下載0-100M,第二個下載100M-200M之間的數(shù)據(jù),依次類推。因此下載過程中需要記住的是的開始位置段和結(jié)束位置段,其實只需要開始位置就可以了,結(jié)束為止可以根據(jù)開始位置加上下載的大小來推斷獲取。
3.獲取到大小數(shù)據(jù)以后,開始用線程循環(huán)讀取每一個區(qū)間的數(shù)據(jù)
這個里面需要注意的是,要更新數(shù)據(jù)的寫入位置seek(startIndex),逐段填滿,不然會出現(xiàn)覆蓋以前的數(shù)據(jù)。
package com.ldw.multilthreaddownload; import java.io.File; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class Multidownload { static int ThreadCount = 3; //線程的個數(shù) static String path = "http://192.168.0.102:8080/QQ.exe"; //確定下載地址 public static void main(String[] args) { // TODO Auto-generated method stub //發(fā)送get請求,請求這個地址的資源 try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); if(conn.getResponseCode() == 200){ //獲取到請求資源文件的長度 int length = conn.getContentLength(); File file = new File("QQ.exe"); //創(chuàng)建隨機(jī)存儲文件 RandomAccessFile raf = new RandomAccessFile(file, "rwd"); //設(shè)置臨時文件的大小 raf.setLength(length); //關(guān)閉raf raf.close(); //計算出每一個線程下載多少字節(jié) int size = length / Multidownload.ThreadCount; for(int i = 0; i < Multidownload.ThreadCount; i ++){ //startIndex,endIndex分別代表線程的開始和結(jié)束位置 int startIndex = i * size; int endIndex = (i + 1) * size - 1; if(i == ThreadCount - 1){ //如果是最后一個線程,那么結(jié)束位置寫死 endIndex = length -1; } System.out.println("線程" + i + "的下載區(qū)間是" + startIndex + "到" + endIndex); new DownLoadThread(startIndex, endIndex, i).start(); //創(chuàng)建線程下載數(shù)據(jù) } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class DownLoadThread extends Thread{ int startIndex; int endIndex; int threadId; public DownLoadThread(int startIndex, int endIndex, int threadId) { super(); this.startIndex = startIndex; this.endIndex = endIndex; this.threadId = threadId; } @Override public void run(){ //使用http請求下載安裝包文件 URL url; try { url = new URL(Multidownload.path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); //設(shè)置請求數(shù)據(jù)的區(qū)間 conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); //請求部分?jǐn)?shù)據(jù)的響應(yīng)碼是206 if(conn.getResponseCode() == 206){ //獲取一部分?jǐn)?shù)據(jù)來讀取 InputStream is = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; int total = 0; //拿到臨時文件的引用 File file = new File("QQ.exe"); RandomAccessFile raf = new RandomAccessFile(file, "rwd"); //更新文件的寫入位置,startIndex raf.seek(startIndex); while((len = is.read(b)) != -1 ){ //每次讀取流里面的數(shù)據(jù),同步吧數(shù)據(jù)寫入臨時文件 raf.write(b, 0, len); total += len; System.out.println("線程" + threadId + "下載了" + total); } System.out.println("線程" + threadId + "下載過程結(jié)束==========================="); raf.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談基于SpringBoot實現(xiàn)一個簡單的權(quán)限控制注解
這篇文章主要介紹了基于SpringBoot實現(xiàn)一個簡單的權(quán)限控制注解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Maven編譯遇到Process terminated問題(四種情況全部解決)
這篇文章主要介紹了Maven編譯遇到Process terminated問題(四種情況全部解決),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07IDEA自定義setter和getter格式的設(shè)置方法
這篇文章主要介紹了IDEA自定義setter和getter格式的設(shè)置方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-12-12springboot集成mybatisplus的詳細(xì)步驟
MyBatis-Plus (opens new window)(簡稱 MP)是一個 MyBatis (opens new window)的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生,這篇文章主要介紹了springboot四步集成mybatisplus,需要的朋友可以參考下2022-10-10