java大文件断点续传实现代码
  9m65el8SCpbP 2023年11月24日 26 0

以下是一个基本的Java大文件断点续传实现代码,使用RandomAccessFile类。

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class FileDownload {
    private static final int BUFFER_SIZE = 4096;
    public static void main(String[] args) {
        String fileURL = "http://example.com/file.zip";
        String saveDir = "C:/downloads/";
        String fileName = "file.zip";
        int numThreads = 4;
        try {
            URL url = new URL(fileURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                int fileSize = conn.getContentLength();
                System.out.println("File size: " + fileSize);
                File file = new File(saveDir + fileName);
                RandomAccessFile raf = new RandomAccessFile(file, "rw");
                raf.setLength(fileSize);
                int chunkSize = fileSize / numThreads;
                for (int i = 0; i < numThreads - 1; i++) {
                    new DownloadThread(i * chunkSize, (i + 1) * chunkSize - 1, fileURL, saveDir + fileName).start();
                }
                new DownloadThread((numThreads - 1) * chunkSize, fileSize - 1, fileURL, saveDir + fileName).start();
                conn.disconnect();
            } else {
                System.out.println("Error: Server returned HTTP response code " + responseCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static class DownloadThread extends Thread {
        private int startByte;
        private int endByte;
        private String fileURL;
        private String saveFilePath;
        public DownloadThread(int startByte, int endByte, String fileURL, String saveFilePath) {
            this.startByte = startByte;
            this.endByte = endByte;
            this.fileURL = fileURL;
            this.saveFilePath = saveFilePath;
        }
        @Override
        public void run() {
            try {
                URL url = new URL(fileURL);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestProperty("Range", "bytes=" + startByte + "-" + endByte);
                byte[] buffer = new byte[BUFFER_SIZE];
                RandomAccessFile raf = new RandomAccessFile(saveFilePath, "rw");
                raf.seek(startByte);
                int bytesRead;
                while ((bytesRead = conn.getInputStream().read(buffer)) != -1) {
                    raf.write(buffer, 0, bytesRead);
                }
                conn.disconnect();
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

参考文章:http://blog.ncmem.com/wordpress/2023/10/27/java大文件断点续传实现代码/


 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月24日 0

暂无评论

推荐阅读
  xaeiTka4h8LY   2024年05月17日   52   0   0 数据库JavaSQL
9m65el8SCpbP