add(req-2080):断点续传加文件路径
This commit is contained in:
parent
eaacc80cc2
commit
caec375c8d
@ -205,12 +205,14 @@ public class WebFileController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String filename = file.getOriginalFilename();
|
String filename = file.getOriginalFilename();
|
||||||
|
String filePath = fileService.getFilePath(file);
|
||||||
BizException.error(filename.length() < FILE_NAME_MAX_LENGTH, FILE_NAME_TOO_LONG);
|
BizException.error(filename.length() < FILE_NAME_MAX_LENGTH, FILE_NAME_TOO_LONG);
|
||||||
ServerFileUploadDto fileUploadDto = ServerFileUploadDto.builder()
|
ServerFileUploadDto fileUploadDto = ServerFileUploadDto.builder()
|
||||||
.appCode(appCode)
|
.appCode(appCode)
|
||||||
.bizScene(bizScene)
|
.bizScene(bizScene)
|
||||||
.fileName(filename)
|
.fileName(filename)
|
||||||
.fileContent(file.getBytes())
|
.fileContent(file.getBytes())
|
||||||
|
.filePath(filePath)
|
||||||
.build();
|
.build();
|
||||||
ServerFileUploadResponse response = fileService.uploadObs(fileUploadDto, liteSaasContext,
|
ServerFileUploadResponse response = fileService.uploadObs(fileUploadDto, liteSaasContext,
|
||||||
FileUploadTypeEnum.CHECK_POINT.getCode());
|
FileUploadTypeEnum.CHECK_POINT.getCode());
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import java.io.InputStream;
|
|||||||
public interface HuaWeiCloudService {
|
public interface HuaWeiCloudService {
|
||||||
String uploadFile(String bucketName, String fileName, String appCode, InputStream srcStream);
|
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);
|
String multipartUpload(String bucketName, String fileName, String appCode, InputStream srcStream);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -82,7 +82,7 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
|
|||||||
* @return fileUrl
|
* @return fileUrl
|
||||||
*/
|
*/
|
||||||
@Override
|
@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();
|
ObsClient obsClient = huaWeiCloudObsClient.getClient();
|
||||||
|
|
||||||
// 创建文件夹
|
// 创建文件夹
|
||||||
@ -91,7 +91,7 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
|
|||||||
if (APPCODE_CMS.equals(appCode)) {
|
if (APPCODE_CMS.equals(appCode)) {
|
||||||
// 断点续传
|
// 断点续传
|
||||||
CompleteMultipartUploadResult completeMultipartUploadResult =
|
CompleteMultipartUploadResult completeMultipartUploadResult =
|
||||||
this.uploadFile(obsClient, bucketName, objectKey, fileName, null);
|
this.uploadFile(obsClient, bucketName, objectKey, fileName, null, filePath);
|
||||||
return completeMultipartUploadResult.getObjectUrl();
|
return completeMultipartUploadResult.getObjectUrl();
|
||||||
} else {
|
} else {
|
||||||
// 创建上传文件的元信息,通过文件云信息设置HTTP Header
|
// 创建上传文件的元信息,通过文件云信息设置HTTP Header
|
||||||
@ -100,7 +100,7 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
|
|||||||
metadata.setContentDisposition("attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
|
metadata.setContentDisposition("attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
|
||||||
metadata.setContentEncoding("utf-8");
|
metadata.setContentEncoding("utf-8");
|
||||||
CompleteMultipartUploadResult completeMultipartUploadResult =
|
CompleteMultipartUploadResult completeMultipartUploadResult =
|
||||||
this.uploadFile(obsClient, bucketName, objectKey, fileName, metadata);
|
this.uploadFile(obsClient, bucketName, objectKey, fileName, metadata, filePath);
|
||||||
return completeMultipartUploadResult.getObjectUrl();
|
return completeMultipartUploadResult.getObjectUrl();
|
||||||
}
|
}
|
||||||
} catch (ObsException exception) {
|
} catch (ObsException exception) {
|
||||||
@ -197,10 +197,10 @@ public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
|
|||||||
|
|
||||||
// 断点续传
|
// 断点续传
|
||||||
private CompleteMultipartUploadResult uploadFile(ObsClient obsClient, String bucketName, String objectKey,
|
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);
|
UploadFileRequest request = new UploadFileRequest(bucketName, objectKey, fileName);
|
||||||
// 设置待上传的本地文件,fileName为待上传的本地文件路径,需要指定到具体带文件后缀的文件名
|
// 设置待上传的本地文件,fileName为待上传的本地文件路径,需要指定到具体带文件后缀的文件名
|
||||||
request.setUploadFile(objectKey);
|
request.setUploadFile(filePath);
|
||||||
// 设置分段上传时的最大并发数
|
// 设置分段上传时的最大并发数
|
||||||
request.setTaskNum(5);
|
request.setTaskNum(5);
|
||||||
// 设置分段大小为10MB
|
// 设置分段大小为10MB
|
||||||
|
|||||||
@ -34,7 +34,7 @@ public interface FileManager {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
String uploadByStream(String bulkName, String keyPath, String fileName, String appCode, byte[] fileContent,
|
String uploadByStream(String bulkName, String keyPath, String fileName, String appCode, byte[] fileContent,
|
||||||
Integer channelType, Integer fileUploadType);
|
Integer channelType, Integer fileUploadType, String filePath);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载文件
|
* 下载文件
|
||||||
|
|||||||
@ -20,6 +20,7 @@ public class ServerFileUploadDto {
|
|||||||
private String bizScene;
|
private String bizScene;
|
||||||
private String fileName;
|
private String fileName;
|
||||||
private byte[] fileContent;
|
private byte[] fileContent;
|
||||||
|
private String filePath;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|||||||
@ -53,7 +53,7 @@ public class FileManagerImpl implements FileManager {
|
|||||||
* @param keyPath key的前置路径
|
* @param keyPath key的前置路径
|
||||||
*/
|
*/
|
||||||
public String uploadByStream(String bulkName, String keyPath, String fileName, String appCode, byte[] fileContent,
|
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);
|
InputStream inputStream = new ByteArrayInputStream(fileContent);
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ public class FileManagerImpl implements FileManager {
|
|||||||
if (fileUploadType.equals(FileUploadTypeEnum.DEFAULT_UPLOAD.getCode())) {
|
if (fileUploadType.equals(FileUploadTypeEnum.DEFAULT_UPLOAD.getCode())) {
|
||||||
return huaWeiCloudService.uploadFile(bulkName, fileName, appCode, inputStream);
|
return huaWeiCloudService.uploadFile(bulkName, fileName, appCode, inputStream);
|
||||||
} else if (fileUploadType.equals(FileUploadTypeEnum.CHECK_POINT.getCode())) {
|
} else if (fileUploadType.equals(FileUploadTypeEnum.CHECK_POINT.getCode())) {
|
||||||
return huaWeiCloudService.checkPointUploadFile(bulkName, fileName, appCode, inputStream);
|
return huaWeiCloudService.checkPointUploadFile(bulkName, fileName, appCode, inputStream, filePath);
|
||||||
} else {
|
} else {
|
||||||
return huaWeiCloudService.multipartUpload(bulkName, fileName, appCode, inputStream);
|
return huaWeiCloudService.multipartUpload(bulkName, fileName, appCode, inputStream);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package cn.axzo.oss.service.api;
|
|||||||
import cn.axzo.framework.auth.domain.ContextInfo;
|
import cn.axzo.framework.auth.domain.ContextInfo;
|
||||||
import cn.axzo.oss.manager.api.dto.request.*;
|
import cn.axzo.oss.manager.api.dto.request.*;
|
||||||
import cn.axzo.oss.manager.api.dto.response.*;
|
import cn.axzo.oss.manager.api.dto.response.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -48,4 +49,6 @@ public interface FileService {
|
|||||||
|
|
||||||
ServerFileUploadResponse uploadObs(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext,
|
ServerFileUploadResponse uploadObs(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext,
|
||||||
Integer fileUploadType);
|
Integer fileUploadType);
|
||||||
|
|
||||||
|
String getFilePath(MultipartFile file);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,8 +29,12 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.stereotype.Service;
|
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.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -580,7 +584,7 @@ public class FileServiceImpl implements FileService {
|
|||||||
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileConform);
|
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileConform);
|
||||||
// 上传文件
|
// 上传文件
|
||||||
String fileUrl = fileManager.uploadByStream(fileUploadConfig.getBucketName(), tgtFileKey, dto.getFileName(),
|
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)) {
|
if (Utility.isBlank(fileUrl)) {
|
||||||
@ -628,4 +632,32 @@ public class FileServiceImpl implements FileService {
|
|||||||
return setResponse(ossFile);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user