解决上传超过一小时失败的问题,支持超大文件上传(不超过30G)

This commit is contained in:
zhouxin 2021-06-02 21:30:59 +08:00
parent 553d456dc3
commit 0016c3e759
2 changed files with 64 additions and 1 deletions

View File

@ -0,0 +1,42 @@
package com.github.zxbu.webdavteambition.model;
import java.util.List;
public class RefreshUploadUrlRequest {
private String drive_id;
private List<UploadPreRequest.PartInfo> part_info_list;
private String file_id;
private String upload_id;
public String getDrive_id() {
return drive_id;
}
public void setDrive_id(String drive_id) {
this.drive_id = drive_id;
}
public List<UploadPreRequest.PartInfo> getPart_info_list() {
return part_info_list;
}
public void setPart_info_list(List<UploadPreRequest.PartInfo> part_info_list) {
this.part_info_list = part_info_list;
}
public String getFile_id() {
return file_id;
}
public void setFile_id(String file_id) {
this.file_id = file_id;
}
public String getUpload_id() {
return upload_id;
}
public void setUpload_id(String upload_id) {
this.upload_id = upload_id;
}
}

View File

@ -9,6 +9,7 @@ import com.github.zxbu.webdavteambition.model.result.TFileListResult;
import com.github.zxbu.webdavteambition.model.result.UploadPreResult;
import com.github.zxbu.webdavteambition.util.JsonUtil;
import net.sf.webdav.exceptions.WebdavException;
import okhttp3.HttpUrl;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -146,13 +147,32 @@ public class AliYunDriverClientService {
byte[] buffer = new byte[chunkSize];
for (int i = 0; i < partInfoList.size(); i++) {
UploadPreRequest.PartInfo partInfo = partInfoList.get(i);
String uploadUrl = partInfo.getUpload_url();
long expires = Long.parseLong(Objects.requireNonNull(Objects.requireNonNull(HttpUrl.parse(uploadUrl)).queryParameter("x-oss-expires")));
if (System.currentTimeMillis() / 1000 >= expires) {
// 已过期重新置换UploadUrl
RefreshUploadUrlRequest refreshUploadUrlRequest = new RefreshUploadUrlRequest();
refreshUploadUrlRequest.setDrive_id(client.getDriveId());
refreshUploadUrlRequest.setUpload_id(uploadPreResult.getUpload_id());
refreshUploadUrlRequest.setFile_id(uploadPreResult.getFile_id());
refreshUploadUrlRequest.setPart_info_list(part_info_list);
String refreshJson = client.post("/file/get_upload_url", refreshUploadUrlRequest);
UploadPreResult refreshResult = JsonUtil.readValue(refreshJson, UploadPreResult.class);
for (int j = i; j < partInfoList.size(); j++) {
UploadPreRequest.PartInfo oldInfo = partInfoList.get(j);
UploadPreRequest.PartInfo newInfo = refreshResult.getPart_info_list().stream().filter(p -> p.getPart_number().equals(oldInfo.getPart_number())).findAny().orElseThrow(NullPointerException::new);
oldInfo.setUpload_url(newInfo.getUpload_url());
}
}
try {
int read = IOUtils.read(inputStream, buffer, 0, buffer.length);
if (read == -1) {
LOGGER.info("文件上传结束。文件名:{},当前进度:{}/{}", path, (i + 1), partInfoList.size());
return;
}
client.upload(partInfo.getUpload_url(), buffer, 0, read);
client.upload(uploadUrl, buffer, 0, read);
virtualTFileService.updateLength(parent.getFile_id(), uploadPreResult.getFile_id(), buffer.length);
LOGGER.info("文件正在上传。文件名:{},当前进度:{}/{}", path, (i + 1), partInfoList.size());
} catch (IOException e) {
@ -175,6 +195,7 @@ public class AliYunDriverClientService {
clearCache();
}
public void rename(String sourcePath, String newName) {
sourcePath = normalizingPath(sourcePath);
TFile tFile = getTFileByPath(sourcePath);