我正在制作一个下载管理器,我希望多个线程下载不同的文件段,以便一次写入不同位置的文件。只是为了每个人的澄清,我不希望文件被锁定,因为它会杀死不同线程一次写入的目的。我正在使用 Apache HttpClient 库和 FileChannel transferFrom()。当前代码仅下载第一个段,而忽略其他段。
代码说明: startDownload 方法创建一个新文件并检查链接是否支持部分内容,如果支持则为每个段启动线程,否则单个线程将下载整个文件。getFileName 是从 URI 中提取文件名的函数。Download 方法包含实际使用 HttpClient 下载文件然后使用 transferFrom 写入文件的代码。
public void startDownload() {
Thread thread = new Thread(() -> {
try {
String downloadDirectory = "/home/muhammad/";
URI uri = new URI("http://94.23.204.158/JDownloader.zip");
int segments = 2;
// Create a HttpClient for checking file for segmentation.
CloseableHttpClient Checkingclient = HttpClients.createDefault();
// get request for checking size of file.
HttpGet checkingGet = new HttpGet(uri);
CloseableHttpResponse checkingResponse = Checkingclient.execute(checkingGet);
long sizeofFile = checkingResponse.getEntity().getContentLength();
// Create a new file in downloadDirectory with name extracted from uri.
File file = new File(downloadDirectory + getFileName(uri));
if (!file.exists()) {
file.createNewFile();
}
// set range header for checking server support for partial content.
checkingGet.setHeader("Range", "bytes=" + 0 + "-" + 1);
checkingResponse = Checkingclient.execute(checkingGet);
// Check if response code is 206 (partial content response code).
if (checkingResponse.getStatusLine().getStatusCode() == 206) {
//find size of each segment.
final long sizeOfEachSegment = sizeofFile / segments;
//Download each segment independently.
for (int i = 0; i < segments; i++) {
Download(i * sizeOfEachSegment, (i + 1) * sizeOfEachSegment, sizeOfEachSegment, file, uri);
}
// Thread used for last few Bytes and EOF.
Download(sizeOfEachSegment * segments, sizeofFile, Long.MAX_VALUE, file, uri);
} else {
System.err.println("server dont support partial content");
System.out.println(checkingResponse.getStatusLine().getStatusCode());
// Download complete file using single thread.
Download(0, 0, Long.MAX_VALUE, file, uri);
}
} catch (IOException | URISyntaxException ex) {
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
}
});
thread.start();
}
public void Download(long start, long end, long sizeOfEachSegment, File file, URI uri) {
Thread thread = new Thread(() -> {
try {
FileChannel fileChannel = new FileOutputStream(file).getChannel();
CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(uri);
// Range header for defining which segment of file we want to receive.
if (end != 0) {
String byteRange = start + "-" + end;
get.setHeader("Range", "bytes=" + byteRange);
}
CloseableHttpResponse response = client.execute(get);
ReadableByteChannel inputChannel = Channels.newChannel(response.getEntity().getContent());
fileChannel.transferFrom(inputChannel, start, sizeOfEachSegment);
response.close();
client.close();
fileChannel.close();
} catch (IOException | IllegalStateException exception) {
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, exception);
}
});
thread.start();
}
对现有代码的一些修复可以使多个线程同时写入同一个文件而无需等待会很好,但如果他们可以完成上述任务,我也有兴趣研究其他更有效的技术。如果在任何情况下不等待就写入文件是不可能的,那么任何其他有效的解决方案都更受欢迎。提前致谢 :)