add(req-2080):断点续传加文件路径

This commit is contained in:
胡朝飞 2024-01-16 17:32:41 +08:00
parent eaacc80cc2
commit caec375c8d
8 changed files with 48 additions and 10 deletions

View File

@ -205,12 +205,14 @@ public class WebFileController {
}
String filename = file.getOriginalFilename();
String filePath = fileService.getFilePath(file);
BizException.error(filename.length() < FILE_NAME_MAX_LENGTH, FILE_NAME_TOO_LONG);
ServerFileUploadDto fileUploadDto = ServerFileUploadDto.builder()
.appCode(appCode)
.bizScene(bizScene)
.fileName(filename)
.fileContent(file.getBytes())
.filePath(filePath)
.build();
ServerFileUploadResponse response = fileService.uploadObs(fileUploadDto, liteSaasContext,
FileUploadTypeEnum.CHECK_POINT.getCode());

View File

@ -11,7 +11,7 @@ import java.io.InputStream;
public interface HuaWeiCloudService {
String uploadFile(String bucketName, String fileName, String appCode, InputStream srcStream);
String checkPointUploadFile(String bucketName, String fileName, String appCode, InputStream srcStream);
String checkPointUploadFile(String bucketName, String fileName, String appCode, InputStream srcStream, String filePath);
String multipartUpload(String bucketName, String fileName, String appCode, InputStream srcStream);
}

View File

@ -82,7 +82,7 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
* @return fileUrl
*/
@Override
public String checkPointUploadFile(String bucketName, String fileName, String appCode, InputStream srcStream) {
public String checkPointUploadFile(String bucketName, String fileName, String appCode, InputStream srcStream, String filePath) {
ObsClient obsClient = huaWeiCloudObsClient.getClient();
// 创建文件夹
@ -91,7 +91,7 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
if (APPCODE_CMS.equals(appCode)) {
// 断点续传
CompleteMultipartUploadResult completeMultipartUploadResult =
this.uploadFile(obsClient, bucketName, objectKey, fileName, null);
this.uploadFile(obsClient, bucketName, objectKey, fileName, null, filePath);
return completeMultipartUploadResult.getObjectUrl();
} else {
// 创建上传文件的元信息通过文件云信息设置HTTP Header
@ -100,7 +100,7 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
metadata.setContentDisposition("attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
metadata.setContentEncoding("utf-8");
CompleteMultipartUploadResult completeMultipartUploadResult =
this.uploadFile(obsClient, bucketName, objectKey, fileName, metadata);
this.uploadFile(obsClient, bucketName, objectKey, fileName, metadata, filePath);
return completeMultipartUploadResult.getObjectUrl();
}
} catch (ObsException exception) {
@ -197,10 +197,10 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
// 断点续传
private CompleteMultipartUploadResult uploadFile(ObsClient obsClient, String bucketName, String objectKey,
String fileName, ObjectMetadata metadata) {
String fileName, ObjectMetadata metadata, String filePath) {
UploadFileRequest request = new UploadFileRequest(bucketName, objectKey, fileName);
// 设置待上传的本地文件fileName为待上传的本地文件路径需要指定到具体带文件后缀的文件名
request.setUploadFile(objectKey);
request.setUploadFile(filePath);
// 设置分段上传时的最大并发数
request.setTaskNum(5);
// 设置分段大小为10MB

View File

@ -34,7 +34,7 @@ public interface FileManager {
* @return
*/
String uploadByStream(String bulkName, String keyPath, String fileName, String appCode, byte[] fileContent,
Integer channelType, Integer fileUploadType);
Integer channelType, Integer fileUploadType, String filePath);
/**
* 下载文件

View File

@ -20,6 +20,7 @@ public class ServerFileUploadDto {
private String bizScene;
private String fileName;
private byte[] fileContent;
private String filePath;
@Override
public String toString() {

View File

@ -53,7 +53,7 @@ public class FileManagerImpl implements FileManager {
* @param keyPath key的前置路径
*/
public String uploadByStream(String bulkName, String keyPath, String fileName, String appCode, byte[] fileContent,
Integer channelType, Integer fileUploadType) {
Integer channelType, Integer fileUploadType, String filePath) {
InputStream inputStream = new ByteArrayInputStream(fileContent);
@ -71,7 +71,7 @@ public class FileManagerImpl implements FileManager {
if (fileUploadType.equals(FileUploadTypeEnum.DEFAULT_UPLOAD.getCode())) {
return huaWeiCloudService.uploadFile(bulkName, fileName, appCode, inputStream);
} else if (fileUploadType.equals(FileUploadTypeEnum.CHECK_POINT.getCode())) {
return huaWeiCloudService.checkPointUploadFile(bulkName, fileName, appCode, inputStream);
return huaWeiCloudService.checkPointUploadFile(bulkName, fileName, appCode, inputStream, filePath);
} else {
return huaWeiCloudService.multipartUpload(bulkName, fileName, appCode, inputStream);
}

View File

@ -3,6 +3,7 @@ package cn.axzo.oss.service.api;
import cn.axzo.framework.auth.domain.ContextInfo;
import cn.axzo.oss.manager.api.dto.request.*;
import cn.axzo.oss.manager.api.dto.response.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
@ -48,4 +49,6 @@ public interface FileService {
ServerFileUploadResponse uploadObs(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext,
Integer fileUploadType);
String getFilePath(MultipartFile file);
}

View File

@ -29,8 +29,12 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@ -580,7 +584,7 @@ public class FileServiceImpl implements FileService {
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileConform);
// 上传文件
String fileUrl = fileManager.uploadByStream(fileUploadConfig.getBucketName(), tgtFileKey, dto.getFileName(),
dto.getAppCode(), dto.getFileContent(), channelType, fileUploadType);
dto.getAppCode(), dto.getFileContent(), channelType, fileUploadType, dto.getFilePath());
// 保存失败
if (Utility.isBlank(fileUrl)) {
@ -628,4 +632,32 @@ public class FileServiceImpl implements FileService {
return setResponse(ossFile);
}
@Override
public String getFilePath(MultipartFile file) {
// 创建输入流
InputStream input = null;
try {
input = file.getInputStream();
// 指定目标文件路径及文件名
String targetDir = "target/";
String fileName = file.getOriginalFilename();
String filePath = targetDir + fileName;
// 创建输出流并写入数据
OutputStream output = new FileOutputStream(filePath);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// 关闭流
input.close();
output.close();
return filePath;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}