java 大文件断点续传

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;

public class DownloadManager {

    private static final int BUFFER_SIZE = 4096;

    private String downloadUrl;
    private String savePath;
    private int numThreads;
    private long fileSize;
    private ConcurrentHashMap<Integer, Long> progressMap;
    private ExecutorService executorService;

    public DownloadManager(String downloadUrl, String savePath, int numThreads) {
        this.downloadUrl = downloadUrl;
        this.savePath = savePath;
        this.numThreads = numThreads;
        this.progressMap = new ConcurrentHashMap<>();
        this.executorService = Executors.newFixedThreadPool(numThreads);
    }

    public void download() throws Exception {
        URL url = new URL(downloadUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("HEAD");
        fileSize = conn.getContentLengthLong();
        conn.disconnect();

        if (fileSize == -1) {
            throw new Exception("Invalid URL or file not found");
        }

        File file = new File(savePath);
        if (file.exists() && file.isFile()) {
            long localFileSize = file.length();
            if (localFileSize == fileSize) {
                System.out.println("文件已下载完成。");
                return;
            } else if (localFileSize < fileSize) {
                System.out.println("文件已存在,从上次下载的地方恢复下载 ...");
            }
        }

        long blockSize = fileSize / numThreads;

        for (int i = 0; i < numThreads; i++) {
            long startPos = i * blockSize;
            long endPos = (i == numThreads - 1) ? fileSize - 1 : startPos + blockSize - 1;
            progressMap.put(i, startPos);

            DownloadThread thread = new DownloadThread(i, startPos, endPos, url, savePath);
            executorService.execute(thread);
        }

        executorService.shutdown();

        while (!executorService.isTerminated()) {
            Thread.sleep(100);
        }

        RandomAccessFile raf = new RandomAccessFile(savePath, "rw");
        for (int i = 0; i < numThreads; i++) {
            FileInputStream fis = new FileInputStream(getTempFilePath(i));
            byte[] buffer = new byte[BUFFER_SIZE];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                raf.seek(progressMap.get(i));
                raf.write(buffer, 0, len);
                progressMap.put(i, progressMap.get(i) + len);
            }
            fis.close();
            new File(getTempFilePath(i)).delete();
        }
        raf.close();

        System.out.println("文件下载完成。");
    }

    private String getTempFilePath(int threadId) {
        return savePath + ".thread" + threadId;
    }

    private class DownloadThread implements Runnable {

        private int threadId;
        private long startPos;
        private long endPos;
        private URL url;
        private String savePath;

        public DownloadThread(int threadId, long startPos, long endPos, URL url, String savePath) {
            this.threadId = threadId;
            this.startPos = startPos;
            this.endPos = endPos;
            this.url = url;
            this.savePath = savePath;
        }

        @Override
        public void run() {
            try {
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos);
                InputStream inputStream = conn.getInputStream();
                RandomAccessFile raf = new RandomAccessFile(getTempFilePath(threadId), "rw");
                byte[] buffer = new byte[BUFFER_SIZE];
                int len;
                while ((len = inputStream.read(buffer)) != -1) {
                    raf.write(buffer, 0, len);
                    progressMap.put(threadId, progressMap.get(threadId) + len);
                }
                inputStream.close();
                raf.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
 

使用示例:

public static void main(String[] args) {
    try {
        DownloadManager downloadManager = new DownloadManager(
                "http://example.com/file.zip", // 下载链接
                "C:/Downloads/file.zip", // 本地存储路径
                10 // 线程数
        );
        downloadManager.download();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 

在上面的示例中,下载链接为 http://example.com/file.zip,本地存储路径为 C:/Downloads/file.zip,线程数为 10。程序将会自动计算并分配每个线程下载的部分,在下载过程中使用断点续传技术,自动在本地生成对应下载线程的临时文件,下载完成后合并临时文件到下载目标文件。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉浮yu大海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值