文件分块上传
上一篇文章(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());
}
}
}