国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java多線程實現(xiàn)文件下載功能

2019-11-26 13:14:17
字體:
供稿:網(wǎng)友

多線程下載文件的思路:

1.首先獲取到文件的總大小
獲取文件大小的方式是通過網(wǎng)絡(luò)讀取,getContentLength()即可獲取到文件的大小,使用RandomAccessFile()支持隨機訪問

2.根據(jù)所準備的線程數(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)建隨機存儲文件     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);    //請求部分數(shù)據(jù)的響應(yīng)碼是206    if(conn.getResponseCode() == 206){     //獲取一部分數(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)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 固镇县| 手游| 沁源县| 崇仁县| 浪卡子县| 泾源县| 大城县| 深泽县| 大方县| 木兰县| 雷波县| 双辽市| 通城县| 哈密市| 新平| 大城县| 鞍山市| 荣昌县| 六枝特区| 江华| 保康县| 永康市| 南开区| 富顺县| 锦州市| 高要市| 康保县| 德保县| 达日县| 马鞍山市| 竹北市| 马龙县| 神木县| 白水县| 梅河口市| 巴楚县| 张家港市| 南投市| 保德县| 仙桃市| 盐亭县|