一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - Java實現(xiàn)多線程斷點下載

Java實現(xiàn)多線程斷點下載

2021-04-12 09:25Java之家 Java教程

這篇文章主要為大家詳細介紹了Java實現(xiàn)多線程斷點下載的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

java多線程斷點下載原理如圖:

Java實現(xiàn)多線程斷點下載

代碼如下:

?
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.randomaccessfile;
import java.net.httpurlconnection;
import java.net.url;
 
public class mutilethreaddownload {
  /**
   * 線程的數(shù)量
   */
  private static int threadcount = 3;
 
  /**
   * 每個下載區(qū)塊的大小
   */
  private static long blocksize;
 
  /**
   * 正在運行的線程的數(shù)量
   */
  private static int runningthreadcount;
 
  /**
   * @param args
   * @throws exception
   */
  public static void main(string[] args) throws exception {
    // 服務器文件的路徑
    string path = "http://192.168.1.100:8080/ff.exe";
    url url = new url(path);
    httpurlconnection conn = (httpurlconnection) url.openconnection();
    conn.setrequestmethod("get");
    conn.setconnecttimeout(5000);
    int code = conn.getresponsecode();
    if (code == 200) {
      long size = conn.getcontentlength();// 得到服務端返回的文件的大小
      system.out.println("服務器文件的大小:" + size);
      blocksize = size / threadcount;
      // 1.首先在本地創(chuàng)建一個大小跟服務器一模一樣的空白文件。
      file file = new file("temp.exe");
      randomaccessfile raf = new randomaccessfile(file, "rw");
      raf.setlength(size);
      // 2.開啟若干個子線程分別去下載對應的資源。
      runningthreadcount = threadcount;
      for (int i = 1; i <= threadcount; i++) {
        long startindex = (i - 1) * blocksize;
        long endindex = i * blocksize - 1;
        if (i == threadcount) {
          // 最后一個線程
          endindex = size - 1;
        }
        system.out.println("開啟線程:" + i + "下載的位置:" + startindex + "~"
            + endindex);
        new downloadthread(path, i, startindex, endindex).start();
      }
    }
    conn.disconnect();
  }
 
  private static class downloadthread extends thread {
    private int threadid;
    private long startindex;
    private long endindex;
    private string path;
 
    public downloadthread(string path, int threadid, long startindex,
        long endindex) {
      this.path = path;
      this.threadid = threadid;
      this.startindex = startindex;
      this.endindex = endindex;
    }
 
    @override
    public void run() {
      try {
        // 當前線程下載的總大小
        int total = 0;
        file positionfile = new file(threadid + ".txt");
        url url = new url(path);
        httpurlconnection conn = (httpurlconnection) url
            .openconnection();
        conn.setrequestmethod("get");
        // 接著從上一次的位置繼續(xù)下載數(shù)據(jù)
        if (positionfile.exists() && positionfile.length() > 0) {// 判斷是否有記錄
          fileinputstream fis = new fileinputstream(positionfile);
          bufferedreader br = new bufferedreader(
              new inputstreamreader(fis));
          // 獲取當前線程上次下載的總大小是多少
          string lasttotalstr = br.readline();
          int lasttotal = integer.valueof(lasttotalstr);
          system.out.println("上次線程" + threadid + "下載的總大小:"
              + lasttotal);
          startindex += lasttotal;
          total += lasttotal;// 加上上次下載的總大小。
          fis.close();
        }
 
        conn.setrequestproperty("range", "bytes=" + startindex + "-"
            + endindex);
        conn.setconnecttimeout(5000);
        int code = conn.getresponsecode();
        system.out.println("code=" + code);
        inputstream is = conn.getinputstream();
        file file = new file("temp.exe");
        randomaccessfile raf = new randomaccessfile(file, "rw");
        // 指定文件開始寫的位置。
        raf.seek(startindex);
        system.out.println("第" + threadid + "個線程:寫文件的開始位置:"
            + string.valueof(startindex));
        int len = 0;
        byte[] buffer = new byte[512];
        while ((len = is.read(buffer)) != -1) {
          randomaccessfile rf = new randomaccessfile(positionfile,
              "rwd");
          raf.write(buffer, 0, len);
          total += len;
          rf.write(string.valueof(total).getbytes());
          rf.close();
        }
        is.close();
        raf.close();
 
      } catch (exception e) {
        e.printstacktrace();
      } finally {
        // 只有所有的線程都下載完畢后 才可以刪除記錄文件。
        synchronized (mutilethreaddownload.class) {
          system.out.println("線程" + threadid + "下載完畢了");
          runningthreadcount--;
          if (runningthreadcount < 1) {
            system.out.println("所有的線程都工作完畢了。刪除臨時記錄的文件");
            for (int i = 1; i <= threadcount; i++) {
              file f = new file(i + ".txt");
              system.out.println(f.delete());
            }
          }
        }
 
      }
    }
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天干天天日天天射天天操毛片 | 国产不卡视频一区二区在线观看 | 日韩网站在线 | 女人张开腿让男人桶爽 | 香蕉久久一区二区三区啪啪 | 亚洲另类激情 | 日本欧美不卡一区二区三区在线 | 四虎永久免费地址 | 欧美日韩国产另类一区二区三区 | 国产情侣自拍网 | 欧洲老妇人70 | 国产成人精品午夜免费 | 亚洲精品国产在线观看 | 亚洲丰满模特裸做爰 | 国产精品欧美亚洲韩国日本99 | 99av涩导航 | 久久久久久久久a免费 | 日本连裤袜xxxxx在线视频 | 欧洲破处 | 天天做天天爱天天操 | 国产精品一区二区国产 | 天天久久影视色香综合网 | 王晶三级作品 | 成品人视频w免费观看w | 国产色图片| 天天综合网网欲色 | 免费国产高清精品一区在线 | 欧美黑人成人免费全部 | 亚洲七七久久综合桃花 | 国产高清视频免费最新在线 | 久久99精品涩AV毛片观看 | 色播影院性播影院私人影院 | 2019男人天堂 | 国产日韩免费视频 | 成品人视频w免费观看w | 久久中文字幕乱码免费 | 国产色在线观看 | 91混血大战上海双胞胎 | 色狠狠色狠狠综合天天 | 小妇人电影免费完整观看2021 | 国产在线观看色 |