成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

在Java項(xiàng)目中如何利用多線程實(shí)現(xiàn)文件下載功能

這篇文章將為大家詳細(xì)講解有關(guān)在Java項(xiàng)目中如何利用多線程實(shí)現(xiàn)文件下載功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。

揚(yáng)中網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),揚(yáng)中網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為揚(yáng)中上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個(gè)售后服務(wù)好的揚(yáng)中做網(wǎng)站的公司定做!

具體內(nèi)容如下

import java.io.File; 
import java.io.InputStream; 
import java.io.RandomAccessFile; 
import java.net.HttpURLConnection; 
import java.net.URL; 
 
public class MulThreadDownload { 
  public static void main(String[] args) throws Exception { 
    String path = "http://192.168.1.100:8080/Hello/Big.exe"; 
    new MulThreadDownload().download(path, 3); 
  } 
 
  /** 
   * 下載文件 
   * 
   * @param path 
   *      網(wǎng)絡(luò)文件路徑 
   * @param threadSize 
   *      線程數(shù) 
   * @throws Exception 
   */ 
  private void download(String path, int threadSize) throws Exception { 
    URL url = new URL(path); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
    connection.setRequestMethod("GET"); 
    connection.setConnectTimeout(5000); 
    if (connection.getResponseCode() == 200) { 
      int length = connection.getContentLength();// 獲取網(wǎng)絡(luò)文件長度 
      File file = new File(getFileName(path)); 
      // 在本地生成一個(gè)長度與網(wǎng)絡(luò)文件相同的文件 
      RandomAccessFile accessFile = new RandomAccessFile(file, "rwd"); 
      accessFile.setLength(length); 
      accessFile.close(); 
 
      // 計(jì)算每條線程負(fù)責(zé)下載的數(shù)據(jù)量 
      int block = length % threadSize == 0 ? length / threadSize : length 
          / threadSize + 1; 
      for (int threadId = 0; threadId < threadSize; threadId++) { 
        new DownloadThread(threadId, block, url, file).start(); 
      } 
    } else { 
      System.out.println("download fail"); 
    } 
  } 
 
  private class DownloadThread extends Thread { 
 
    private int threadId; 
    private int block; 
    private URL url; 
    private File file; 
 
    public DownloadThread(int threadId, int block, URL url, File file) { 
      this.threadId = threadId; 
      this.block = block; 
      this.url = url; 
      this.file = file; 
    } 
 
    @Override 
    public void run() { 
      int start = threadId * block; // 計(jì)算該線程從網(wǎng)絡(luò)文件什么位置開始下載 
      int end = (threadId + 1) * block - 1; // 計(jì)算下載到網(wǎng)絡(luò)文件什么位置結(jié)束 
      try { 
        RandomAccessFile accessFile = new RandomAccessFile(file, "rwd"); 
        accessFile.seek(start); //從start開始 
 
        HttpURLConnection connection = (HttpURLConnection) url 
            .openConnection(); 
        connection.setRequestMethod("GET"); 
        connection.setConnectTimeout(5000); 
        //設(shè)置獲取資源數(shù)據(jù)的范圍,從start到end 
        connection.setRequestProperty("Range", "bytes=" + start + "-" 
            + end); 
        //注意多線程下載狀態(tài)碼是 206 不是200 
        if (connection.getResponseCode() == 206) { 
          InputStream inputStream = connection.getInputStream(); 
          byte[] buffer = new byte[1024]; 
          int len = 0; 
          while ((len = inputStream.read(buffer)) != -1) { 
            accessFile.write(buffer, 0, len); 
          } 
          accessFile.close(); 
          inputStream.close(); 
        } 
        System.out.println("第" + (threadId + 1) + "條線程已經(jīng)下載完成"); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
    } 
  } 
 
  /** 
   * 獲取文件名稱 
   * 
   * @param path 
   *      網(wǎng)絡(luò)文件路徑 
   * @return 
   */ 
  private String getFileName(String path) { 
    return path.substring(path.lastIndexOf("/") + 1); 
  } 
} 

關(guān)于在Java項(xiàng)目中如何利用多線程實(shí)現(xiàn)文件下載功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

本文名稱:在Java項(xiàng)目中如何利用多線程實(shí)現(xiàn)文件下載功能
網(wǎng)址分享:http://jinyejixie.com/article12/iippgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作移動(dòng)網(wǎng)站建設(shè)響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)
平顶山市| 久治县| 涟源市| 新宁县| 南乐县| 衡山县| 南乐县| 正阳县| 长岛县| 江西省| 辽源市| 吕梁市| 乌恰县| 荣成市| 将乐县| 彩票| 沁水县| 离岛区| 峨眉山市| 晋江市| 金昌市| 常熟市| 永平县| 尚志市| 通许县| 大荔县| 河北省| 济源市| 赤水市| 松潘县| 山东| 甘南县| 泌阳县| 石阡县| 谢通门县| 富宁县| 陆川县| 桂东县| 获嘉县| 庆安县| 寻甸|