欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java多線程下載文件實(shí)例詳解

 更新時(shí)間:2017年04月05日 11:19:32   作者:歐陽鵬  
這篇文章主要為大家詳細(xì)介紹了Java多線程下載文件的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java多線程下載文件的具體代碼,供大家參考,具體內(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); 
  } 
} 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解SpringCloud服務(wù)認(rèn)證(JWT)

    詳解SpringCloud服務(wù)認(rèn)證(JWT)

    本篇文章主要介紹了SpringCloud服務(wù)認(rèn)證(JWT),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Java信號(hào)量Semaphore原理及代碼實(shí)例

    Java信號(hào)量Semaphore原理及代碼實(shí)例

    這篇文章主要介紹了Java信號(hào)量Semaphore原理及代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 新手初學(xué)Java繼承、封裝與多態(tài)

    新手初學(xué)Java繼承、封裝與多態(tài)

    封裝、繼承、多態(tài)三大特征是java中比較常用的,務(wù)必要掌握,下面給大家介紹Java封裝、繼承、多態(tài)三大特征的理解,有不清楚的朋友可以一起學(xué)習(xí)下
    2021-07-07
  • SWT(JFace) FTP客戶端實(shí)現(xiàn)

    SWT(JFace) FTP客戶端實(shí)現(xiàn)

    SWT(JFace)小制作:FTP客戶端實(shí)現(xiàn)
    2009-06-06
  • 詳解springboot集成mybatis xml方式

    詳解springboot集成mybatis xml方式

    這篇文章主要介紹了詳解springboot集成mybatis xml方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • Java中ThreadLocal共享變量的使用

    Java中ThreadLocal共享變量的使用

    java.lang.ThreadLocal該類提供了線程局部變量,用于在當(dāng)前線程中共享數(shù)據(jù),本文主要介紹了Java中ThreadLocal共享變量的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Java?輪詢鎖使用時(shí)遇到問題解決方案

    Java?輪詢鎖使用時(shí)遇到問題解決方案

    這篇文章主要介紹了Java?輪詢鎖使用時(shí)遇到問題解決方案,當(dāng)我們遇到死鎖之后,除了可以手動(dòng)重啟程序解決之外,還可以考慮使用順序鎖和輪詢鎖,但是過程也會(huì)遇到一些問題,接下來我們一起進(jìn)入下面文章了解解決方案,需要的小伙伴可以參考一下
    2022-05-05
  • SpringBoot通過RedisTemplate執(zhí)行Lua腳本的方法步驟

    SpringBoot通過RedisTemplate執(zhí)行Lua腳本的方法步驟

    這篇文章主要介紹了SpringBoot通過RedisTemplate執(zhí)行Lua腳本的方法步驟,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java數(shù)組(Array)最全匯總(上篇)

    Java數(shù)組(Array)最全匯總(上篇)

    這篇文章主要介紹了Java數(shù)組(Array)最全匯總(上篇),本文章內(nèi)容詳細(xì),通過案例可以更好的理解數(shù)組的相關(guān)知識(shí),本模塊分為了三部分,本次為上篇,需要的朋友可以參考下
    2023-01-01
  • Java  mysql數(shù)據(jù)庫并進(jìn)行內(nèi)容查詢實(shí)例代碼

    Java mysql數(shù)據(jù)庫并進(jìn)行內(nèi)容查詢實(shí)例代碼

    這篇文章主要介紹了Java mysql數(shù)據(jù)庫并進(jìn)行內(nèi)容查詢實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-11-11

最新評(píng)論