This commit is contained in:
parent
824063b9dc
commit
33378e840c
|
@ -54,13 +54,16 @@ public class AliyahPanBackup {
|
|||
// 3. 创建备份文件
|
||||
String zipFilePath = createBackupZip();
|
||||
|
||||
// 4. 上传文件
|
||||
boolean uploadSuccess = uploadToAlipan(zipFilePath);
|
||||
if (uploadSuccess) {
|
||||
logger.info("备份文件已成功上传到阿里云盘目录: {}", TARGET_FOLDER);
|
||||
} else {
|
||||
logger.error("上传到阿里云盘失败");
|
||||
System.exit(1);
|
||||
// 假设 uploadToAlipan 方法返回一个布尔值,表示上传是否成功
|
||||
boolean uploadSuccess = false;
|
||||
|
||||
while (!uploadSuccess) {
|
||||
uploadSuccess = uploadToAlipan(zipFilePath);
|
||||
if (uploadSuccess) {
|
||||
logger.info("备份文件已成功上传到阿里云盘目录: {}", TARGET_FOLDER);
|
||||
} else {
|
||||
logger.error("上传到阿里云盘失败,重试上传");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -285,22 +288,17 @@ public class AliyahPanBackup {
|
|||
|
||||
// 上传文件分片
|
||||
for (int i = 0; i < receivedPartInfoList.length(); i++) {
|
||||
|
||||
JSONObject partInfo = receivedPartInfoList.getJSONObject(i);
|
||||
String uploadUrl = partInfo.getString("upload_url");
|
||||
int partNumber = partInfo.getInt("part_number");
|
||||
|
||||
|
||||
// 处理URL中的特殊字符
|
||||
if (uploadUrl.contains(" ")) {
|
||||
uploadUrl = uploadUrl.replace(" ", "%20");
|
||||
}
|
||||
|
||||
// 计算分片位置和实际大小
|
||||
long pos = (partNumber - 1) * partSize;
|
||||
long actualSize = partNumber == partCount ? fileSize - pos : partSize;
|
||||
byte[] partContent = new byte[(int) actualSize];
|
||||
|
||||
// 读取分片内容
|
||||
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
|
||||
randomAccessFile.seek(pos);
|
||||
|
@ -309,7 +307,6 @@ public class AliyahPanBackup {
|
|||
logger.error("读取文件分片失败", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 上传分片
|
||||
RequestBody body = RequestBody.create(partContent, null); // 不指定MediaType
|
||||
Request request = new Request.Builder()
|
||||
|
@ -317,20 +314,13 @@ public class AliyahPanBackup {
|
|||
.put(body)
|
||||
.removeHeader("Content-Type") // 移除Content-Type以避免签名问题
|
||||
.build();
|
||||
|
||||
try (Response response = new OkHttpClient().newCall(request).execute()) {
|
||||
if (!response.isSuccessful()) {
|
||||
logger.error("上传分片失败,partNumber: {}, 响应: {}", partNumber, response.body().string());
|
||||
// 等待一段时间再重试
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
new OkHttpClient().newCall(request).execute();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
logger.info("上传分片成功,进度: {}/{}", partNumber, partCount);
|
||||
Thread.sleep(500);
|
||||
Thread.sleep(50);
|
||||
} catch (IOException e) {
|
||||
logger.error("上传分片失败", e);
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue