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

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

揚中網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),揚中網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為揚中上千多家提供企業(yè)網(wǎng)站建設(shè)服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個售后服務好的揚中做網(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)絡文件路徑 
   * @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)絡文件長度 
      File file = new File(getFileName(path)); 
      // 在本地生成一個長度與網(wǎng)絡文件相同的文件 
      RandomAccessFile accessFile = new RandomAccessFile(file, "rwd"); 
      accessFile.setLength(length); 
      accessFile.close(); 
 
      // 計算每條線程負責下載的數(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; // 計算該線程從網(wǎng)絡文件什么位置開始下載 
      int end = (threadId + 1) * block - 1; // 計算下載到網(wǎng)絡文件什么位置結(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)絡文件路徑 
   * @return 
   */ 
  private String getFileName(String path) { 
    return path.substring(path.lastIndexOf("/") + 1); 
  } 
} 

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

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

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

廣告

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

成都定制網(wǎng)站網(wǎng)頁設(shè)計