parent
ccc2e26bae
commit
1b51d00f09
@ -0,0 +1,214 @@
|
|||||||
|
package com.dsideal.YunXiaoTools.Utils;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
import java.nio.file.*;
|
||||||
|
|
||||||
|
public class MultiThreadDownloader {
|
||||||
|
private final String fileUrl;
|
||||||
|
private final String saveDir;
|
||||||
|
private final int threadCount;
|
||||||
|
private final long[] progress;
|
||||||
|
private volatile boolean isPaused = false;
|
||||||
|
private volatile boolean isCanceled = false;
|
||||||
|
|
||||||
|
public MultiThreadDownloader(String fileUrl, String saveDir, int threadCount) {
|
||||||
|
this.fileUrl = fileUrl;
|
||||||
|
this.saveDir = saveDir;
|
||||||
|
this.threadCount = threadCount;
|
||||||
|
this.progress = new long[threadCount];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开始下载
|
||||||
|
*/
|
||||||
|
public void download() throws Exception {
|
||||||
|
// 获取文件大小
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) new URL(fileUrl).openConnection();
|
||||||
|
conn.setRequestMethod("GET");
|
||||||
|
long fileSize = conn.getContentLengthLong();
|
||||||
|
String fileName = getFileName(conn);
|
||||||
|
conn.disconnect();
|
||||||
|
|
||||||
|
System.out.println("文件大小: " + formatFileSize(fileSize));
|
||||||
|
|
||||||
|
// 创建临时文件
|
||||||
|
String tempDir = saveDir + File.separator + fileName + ".downloading";
|
||||||
|
Files.createDirectories(Paths.get(tempDir));
|
||||||
|
|
||||||
|
// 计算每个线程下载的块大小
|
||||||
|
long blockSize = fileSize / threadCount;
|
||||||
|
CountDownLatch latch = new CountDownLatch(threadCount);
|
||||||
|
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
|
||||||
|
|
||||||
|
// 启动下载线程
|
||||||
|
for (int i = 0; i < threadCount; i++) {
|
||||||
|
long startPos = i * blockSize;
|
||||||
|
long endPos = (i == threadCount - 1) ? fileSize - 1 : (i + 1) * blockSize - 1;
|
||||||
|
String tempFile = tempDir + File.separator + "part_" + i;
|
||||||
|
|
||||||
|
executor.execute(new DownloadTask(i, startPos, endPos, tempFile, latch));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 启动进度显示线程
|
||||||
|
ScheduledExecutorService progressExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
progressExecutor.scheduleAtFixedRate(() -> {
|
||||||
|
if (!isPaused && !isCanceled) {
|
||||||
|
showProgress(fileSize);
|
||||||
|
}
|
||||||
|
}, 1, 1, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
// 等待所有线程完成
|
||||||
|
latch.await();
|
||||||
|
executor.shutdown();
|
||||||
|
progressExecutor.shutdown();
|
||||||
|
|
||||||
|
if (!isCanceled) {
|
||||||
|
// 合并文件
|
||||||
|
mergeFiles(tempDir, saveDir + File.separator + fileName, threadCount);
|
||||||
|
// 删除临时文件
|
||||||
|
deleteDirectory(tempDir);
|
||||||
|
System.out.println("\n下载完成: " + saveDir + File.separator + fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DownloadTask implements Runnable {
|
||||||
|
private final int threadId;
|
||||||
|
private final long startPos;
|
||||||
|
private final long endPos;
|
||||||
|
private final String tempFile;
|
||||||
|
private final CountDownLatch latch;
|
||||||
|
|
||||||
|
public DownloadTask(int threadId, long startPos, long endPos,
|
||||||
|
String tempFile, CountDownLatch latch) {
|
||||||
|
this.threadId = threadId;
|
||||||
|
this.startPos = startPos;
|
||||||
|
this.endPos = endPos;
|
||||||
|
this.tempFile = tempFile;
|
||||||
|
this.latch = latch;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
// 检查断点续传
|
||||||
|
long downloadedSize = 0;
|
||||||
|
File temp = new File(tempFile);
|
||||||
|
if (temp.exists()) {
|
||||||
|
downloadedSize = temp.length();
|
||||||
|
progress[threadId] = downloadedSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (downloadedSize < (endPos - startPos + 1)) {
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) new URL(fileUrl).openConnection();
|
||||||
|
conn.setRequestMethod("GET");
|
||||||
|
conn.setRequestProperty("Range",
|
||||||
|
"bytes=" + (startPos + downloadedSize) + "-" + endPos);
|
||||||
|
|
||||||
|
try (InputStream in = conn.getInputStream();
|
||||||
|
RandomAccessFile out = new RandomAccessFile(tempFile, "rw")) {
|
||||||
|
|
||||||
|
out.seek(downloadedSize);
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
|
int len;
|
||||||
|
|
||||||
|
while ((len = in.read(buffer)) != -1 && !isCanceled) {
|
||||||
|
if (!isPaused) {
|
||||||
|
out.write(buffer, 0, len);
|
||||||
|
progress[threadId] += len;
|
||||||
|
} else {
|
||||||
|
Thread.sleep(1000); // 暂停时休眠
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.disconnect();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 显示下载进度
|
||||||
|
*/
|
||||||
|
private void showProgress(long fileSize) {
|
||||||
|
long downloaded = 0;
|
||||||
|
for (long p : progress) {
|
||||||
|
downloaded += p;
|
||||||
|
}
|
||||||
|
|
||||||
|
double progress = (double) downloaded / fileSize * 100;
|
||||||
|
String speed = formatFileSize(downloaded / 1024) + "/s";
|
||||||
|
|
||||||
|
System.out.printf("\r下载进度: %.2f%% [%s]", progress, speed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 合并文件
|
||||||
|
*/
|
||||||
|
private void mergeFiles(String tempDir, String targetFile, int parts) throws IOException {
|
||||||
|
try (RandomAccessFile target = new RandomAccessFile(targetFile, "rw")) {
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
|
for (int i = 0; i < parts; i++) {
|
||||||
|
String partFile = tempDir + File.separator + "part_" + i;
|
||||||
|
try (RandomAccessFile source = new RandomAccessFile(partFile, "r")) {
|
||||||
|
int len;
|
||||||
|
while ((len = source.read(buffer)) != -1) {
|
||||||
|
target.write(buffer, 0, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 工具方法
|
||||||
|
private String getFileName(HttpURLConnection conn) {
|
||||||
|
String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
|
||||||
|
String disposition = conn.getHeaderField("Content-Disposition");
|
||||||
|
if (disposition != null) {
|
||||||
|
int index = disposition.indexOf("filename=");
|
||||||
|
if (index > 0) {
|
||||||
|
fileName = disposition.substring(index + 9).replace("\"", "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String formatFileSize(long size) {
|
||||||
|
if (size < 1024) return size + " B";
|
||||||
|
if (size < 1024 * 1024) return String.format("%.2f KB", size / 1024.0);
|
||||||
|
if (size < 1024 * 1024 * 1024) return String.format("%.2f MB", size / (1024.0 * 1024));
|
||||||
|
return String.format("%.2f GB", size / (1024.0 * 1024 * 1024));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteDirectory(String dir) {
|
||||||
|
Path directory = Paths.get(dir);
|
||||||
|
try {
|
||||||
|
Files.walk(directory)
|
||||||
|
.sorted(java.util.Comparator.reverseOrder())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.forEach(File::delete);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 控制方法
|
||||||
|
public void pause() {
|
||||||
|
isPaused = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resume() {
|
||||||
|
isPaused = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancel() {
|
||||||
|
isCanceled = true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue