-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiDownload.java
More file actions
49 lines (43 loc) · 1.79 KB
/
Copy pathMultiDownload.java
File metadata and controls
49 lines (43 loc) · 1.79 KB
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
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MultiDownload implements Runnable {
private final URL resourceUrl;
private final long rangeStart, rangeEnd;
private final int chunkIndex;
private final String downloadDirectory;
public MultiDownload(URL resourceUrl, long rangeStart, long rangeEnd, int chunkIndex, String downloadDirectory) {
this.resourceUrl = resourceUrl;
this.rangeStart = rangeStart;
this.rangeEnd = rangeEnd;
this.chunkIndex = chunkIndex;
this.downloadDirectory = downloadDirectory;
}
@Override
public void run() {
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) resourceUrl.openConnection();
connection.setInstanceFollowRedirects(true);
String byteRange = "bytes=" + rangeStart + "-" + rangeEnd;
connection.setRequestProperty("Range", byteRange);
connection.connect();
try (InputStream inputStream = connection.getInputStream(); FileOutputStream fileOutput = new FileOutputStream(downloadDirectory + "/part" + chunkIndex)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fileOutput.write(buffer, 0, bytesRead);
}
System.out.println("Chunk " + chunkIndex + " downloaded.");
}
} catch (IOException e) {
System.err.println("Error downloading chunk " + chunkIndex + ": " + e.getMessage());
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}