forked from wrayzheng/java-multithread-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpDownloader.java
249 lines (224 loc) · 7.05 KB
/
HttpDownloader.java
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* ----------------------------------------------------
* Copyright (c) 2018, Wray Zheng. All Rights Reserved.
* Distributed under the BSD License.
* ----------------------------------------------------
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author: Wray Zheng
* @date: 2018-04-23
* @description: A multi-threaded downloader
*/
public class HttpDownloader {
private boolean resumable;
private URL url;
private File localFile;
private int[] endPoint;
private Object waiting = new Object();
private AtomicInteger downloadedBytes = new AtomicInteger(0);
private AtomicInteger aliveThreads = new AtomicInteger(0);
private boolean multithreaded = true;
private int fileSize = 0;
private int THREAD_NUM = 5;
private int TIME_OUT = 5000;
private final int MIN_SIZE = 2 << 20;
public static void main(String[] args) throws IOException {
String url = "http://mirrors.163.com/debian/ls-lR.gz";
new HttpDownloader(url, "D:/ls-lR.gz", 5, 5000).get();
}
public HttpDownloader(String Url, String localPath) throws MalformedURLException {
this.url = new URL(Url);
this.localFile = new File(localPath);
}
public HttpDownloader(String Url, String localPath,
int threadNum, int timeout) throws MalformedURLException {
this(Url, localPath);
this.THREAD_NUM = threadNum;
this.TIME_OUT = timeout;
}
//开始下载文件
public void get() throws IOException {
long startTime = System.currentTimeMillis();
resumable = supportResumeDownload();
if (!resumable || THREAD_NUM == 1|| fileSize < MIN_SIZE) multithreaded = false;
if (!multithreaded) {
new DownloadThread(0, 0, fileSize - 1).start();;
}
else {
endPoint = new int[THREAD_NUM + 1];
int block = fileSize / THREAD_NUM;
for (int i = 0; i < THREAD_NUM; i++) {
endPoint[i] = block * i;
}
endPoint[THREAD_NUM] = fileSize;
for (int i = 0; i < THREAD_NUM; i++) {
new DownloadThread(i, endPoint[i], endPoint[i + 1] - 1).start();
}
}
startDownloadMonitor();
//等待 downloadMonitor 通知下载完成
try {
synchronized(waiting) {
waiting.wait();
}
} catch (InterruptedException e) {
System.err.println("Download interrupted.");
}
cleanTempFile();
long timeElapsed = System.currentTimeMillis() - startTime;
System.out.println("* File successfully downloaded.");
System.out.println(String.format("* Time used: %.3f s, Average speed: %d KB/s",
timeElapsed / 1000.0, downloadedBytes.get() / timeElapsed));
}
//检测目标文件是否支持断点续传,以决定是否开启多线程下载文件的不同部分
public boolean supportResumeDownload() throws IOException {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Range", "bytes=0-");
int resCode;
while (true) {
try {
con.connect();
fileSize = con.getContentLength();
resCode = con.getResponseCode();
con.disconnect();
break;
} catch (ConnectException e) {
System.out.println("Retry to connect due to connection problem.");
}
}
if (resCode == 206) {
System.out.println("* Support resume download");
return true;
} else {
System.out.println("* Doesn't support resume download");
return false;
}
}
//监测下载速度及下载状态,下载完成时通知主线程
public void startDownloadMonitor() {
Thread downloadMonitor = new Thread(() -> {
int prev = 0;
int curr = 0;
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
curr = downloadedBytes.get();
System.out.println(String.format("Speed: %d KB/s, Downloaded: %d KB (%.2f%%), Threads: %d",
(curr - prev) >> 10, curr >> 10, curr / (float) fileSize * 100, aliveThreads.get()));
prev = curr;
if (aliveThreads.get() == 0) {
synchronized (waiting) {
waiting.notifyAll();
}
}
}
});
downloadMonitor.setDaemon(true);
downloadMonitor.start();
}
//对临时文件进行合并或重命名
public void cleanTempFile() throws IOException {
if (multithreaded) {
merge();
System.out.println("* Temp file merged.");
} else {
Files.move(Paths.get(localFile.getAbsolutePath() + ".0.tmp"),
Paths.get(localFile.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
}
}
//合并多线程下载产生的多个临时文件
public void merge() {
try (OutputStream out = new FileOutputStream(localFile)) {
byte[] buffer = new byte[1024];
int size;
for (int i = 0; i < THREAD_NUM; i++) {
String tmpFile = localFile.getAbsolutePath() + "." + i + ".tmp";
InputStream in = new FileInputStream(tmpFile);
while ((size = in.read(buffer)) != -1) {
out.write(buffer, 0, size);
}
in.close();
Files.delete(Paths.get(tmpFile));
}
} catch (IOException e) {
e.printStackTrace();
}
}
//一个下载线程负责下载文件的某一部分,如果失败则自动重试,直到下载完成
class DownloadThread extends Thread {
private int id;
private int start;
private int end;
private OutputStream out;
public DownloadThread(int id, int start, int end) {
this.id = id;
this.start = start;
this.end = end;
aliveThreads.incrementAndGet();
}
//保证文件的该部分数据下载完成
@Override
public void run() {
boolean success = false;
while (true) {
success = download();
if (success) {
System.out.println("* Downloaded part " + (id + 1));
break;
} else {
System.out.println("Retry to download part " + (id + 1));
}
}
aliveThreads.decrementAndGet();
}
//下载文件指定范围的部分
public boolean download() {
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Range", String.format("bytes=%d-%d", start, end));
con.setConnectTimeout(TIME_OUT);
con.setReadTimeout(TIME_OUT);
con.connect();
int partSize = con.getHeaderFieldInt("Content-Length", -1);
if (partSize != end - start + 1) return false;
if (out == null) out = new FileOutputStream(localFile.getAbsolutePath() + "." + id + ".tmp");
try (InputStream in = con.getInputStream()) {
byte[] buffer = new byte[1024];
int size;
while (start <= end && (size = in.read(buffer)) > 0) {
start += size;
downloadedBytes.addAndGet(size);
out.write(buffer, 0, size);
out.flush();
}
con.disconnect();
if (start <= end) return false;
else out.close();
}
} catch(SocketTimeoutException e) {
System.out.println("Part " + (id + 1) + " Reading timeout.");
return false;
} catch (IOException e) {
System.out.println("Part " + (id + 1) + " encountered error.");
return false;
}
return true;
}
}
}