激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java多線程下載文件實例詳解

Java多線程下載文件實例詳解

2020-09-06 15:07歐陽鵬 Java教程

這篇文章主要為大家詳細(xì)介紹了Java多線程下載文件的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java多線程下載文件的具體代碼,供大家參考,具體內(nèi)容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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));
      // 在本地生成一個長度與網(wǎng)絡(luò)文件相同的文件
      RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
      accessFile.setLength(length);
      accessFile.close();
 
      // 計算每條線程負(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; // 計算該線程從網(wǎng)絡(luò)文件什么位置開始下載
      int end = (threadId + 1) * block - 1; // 計算下載到網(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本黄色免费播放 | 国产精品久久久久久久久久久久久久久 | 欧美成人免费一区二区三区 | 草妞视频 | 国产精品视频久久久 | 曰批全过程40分钟免费视频多人 | 日韩app| 99视频有精品视频高清 | 国产午夜精品一区二区三区视频 | www亚洲免费 | 最新影院| 久久精品国产久精国产 | 草碰人人| 黄色片网站在线免费观看 | 欧美老逼 | 日韩毛片毛片久久精品 | 亚洲影视在线观看 | 一区二区三区国产视频 | 久久免费观看一级毛片 | 在线 日本 制服 中文 欧美 | 日本一区二区精品视频 | 黄视频免费在线 | 亚洲第一视频 | 成人啪啪18免费网站 | 日本一区二区在线 | av电影手机在线看 | 国产精品片一区二区三区 | 秋霞a级毛片在线看 | 免费在线性爱视频 | 中国妞xxxhd露脸偷拍视频 | 99爱视频在线观看 | 天天碰天天操 | xnxx 日本免费 | 国产噜噜噜噜噜久久久久久久久 | 亚洲欧美日韩一区二区三区在线观看 | av久草 | 欧美成人一区二区三区电影 | 欧美精品一区二区中文字幕 | 最新中文字幕在线视频 | 久久久久久久.comav | 国产精品视频一区二区三区四区国 |