文件分块上传

上一篇文章(Java 线程实现暂停、中止)讲了理论,接下来写一个简单的demo,可以实现暂停和恢复上传。
核心就是分块上传,可以把代码里面的数据都放到数据库里即可

import java.io.*;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicLong;

public class FileUploader {

    private static final int BUFFER_SIZE = 1 * 2; // buffer size
    private static final String UPLOAD_STATUS_FILE = "C:\\upload_status.txt"; // Status file to track progress

    private static volatile boolean suspend = false;
    public void uploadFile(String filePath, String serverUrl) throws IOException {
        File file = new File(filePath);
        long fileSize = file.length();

        // Create a temporary status file to track the upload progress
        File statusFile = new File(UPLOAD_STATUS_FILE);
        long uploadedBytes = 0;
        if (statusFile.exists()) {
            try (BufferedReader reader = new BufferedReader(new FileReader(statusFile))) {
                uploadedBytes = Long.parseLong(reader.readLine());
            }
        }
        int i =0;
        // Open the file and start uploading from the last uploaded position
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] buffer = new byte[BUFFER_SIZE];
            AtomicLong currentUploadedBytes = new AtomicLong(uploadedBytes);

            while (currentUploadedBytes.get() < fileSize) {
                while (suspend) {

                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                i++;
                System.out.println("第" +i +"次分块上传");
                int bytesRead = fis.read(buffer);
                if (bytesRead == -1) {
                    break;
                }

                // Upload the chunk to the server
                uploadChunk(serverUrl, buffer, bytesRead, currentUploadedBytes);

                // Update the status file
                updateStatusFile(currentUploadedBytes.get());
            }
        }

        // Remove the status file once the upload is complete
        statusFile.delete();
    }


    private void uploadChunk(String filePath, byte[] buffer, int bytesRead, AtomicLong currentUploadedBytes) throws IOException {
        // Write the chunk to the specified file path
        try (FileOutputStream fos = new FileOutputStream(filePath, true); // Append mode
             BufferedOutputStream bos = new BufferedOutputStream(fos)) {
            bos.write(buffer, 0, bytesRead);
            currentUploadedBytes.addAndGet(bytesRead);
            System.out.println("Uploading chunk of size " + bytesRead + " bytes");
        }
    }
    private void updateStatusFile(long uploadedBytes) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(UPLOAD_STATUS_FILE))) {
            writer.write(Long.toString(uploadedBytes));
        }
    }
    public static void main(String[] args) {
        String filePath = "C:\\test.png";
        String serverUrl = "C:\\to.png";

        new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNextBoolean()) {
                boolean b = scanner.nextBoolean();
                suspend = b;
                System.out.println("suspend is " + suspend);
            }
        }).start();
        try {
            FileUploader uploader = new FileUploader();
            uploader.uploadFile(filePath, serverUrl);
            System.out.println("Upload completed successfully.");
        } catch (IOException e) {
            System.err.println("Failed to upload file: " + e.getMessage());
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值