上传支持元信息设置下载文件名
This commit is contained in:
parent
8d35c83167
commit
dca534c3cb
@ -21,7 +21,7 @@ public interface BaseS3Service {
|
||||
/**
|
||||
* upload file
|
||||
*/
|
||||
String uploadByStream(String bucketName, String tgtFileKey, InputStream srcStream);
|
||||
String uploadByStream(String bucketName, String tgtFileKey, String fileName, InputStream srcStream);
|
||||
|
||||
/**
|
||||
* upload multi file
|
||||
|
||||
@ -8,6 +8,7 @@ import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.model.GetObjectRequest;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -17,6 +18,8 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
/**
|
||||
* @program: oss
|
||||
@ -35,14 +38,19 @@ public class AliOssServiceImpl implements AliOssService {
|
||||
* upload file
|
||||
*/
|
||||
@Override
|
||||
public String uploadByStream(String bucketName, String tgtFileKey, InputStream srcStream) {
|
||||
public String uploadByStream(String bucketName, String tgtFileKey, String fileName, InputStream srcStream) {
|
||||
OSS client = aliOssClient.getClient();
|
||||
try {
|
||||
client.putObject(bucketName, tgtFileKey, srcStream);
|
||||
// 创建上传文件的元信息,通过文件云信息设置HTTP Header
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
// 设置内容被下载时的名称
|
||||
metadata.setContentDisposition("attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
|
||||
metadata.setContentEncoding("utf-8");
|
||||
client.putObject(bucketName, tgtFileKey, srcStream, metadata);
|
||||
} catch (OSSException e) {
|
||||
LogUtil.error("uploadByStream OSSException", e);
|
||||
return "";
|
||||
} catch (ClientException e) {
|
||||
} catch (Exception e) {
|
||||
LogUtil.error("uploadByStream ClientException", e);
|
||||
return "";
|
||||
}
|
||||
|
||||
@ -27,7 +27,7 @@ public interface FileManager {
|
||||
* @param fileContent
|
||||
* @return
|
||||
*/
|
||||
String uploadByStream(String bulkName, String keyPath, byte[] fileContent);
|
||||
String uploadByStream(String bulkName, String keyPath, String fileName, byte[] fileContent);
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
|
||||
@ -38,11 +38,11 @@ public class FileManagerImpl implements FileManager {
|
||||
* @param bulkName 默认的取值为
|
||||
* @param keyPath key的前置路径
|
||||
*/
|
||||
public String uploadByStream(String bulkName, String keyPath, byte[] fileContent) {
|
||||
public String uploadByStream(String bulkName, String keyPath, String fileName, byte[] fileContent) {
|
||||
|
||||
InputStream inputStream = new ByteArrayInputStream(fileContent);
|
||||
|
||||
return aliOssService.uploadByStream(bulkName, keyPath, inputStream);
|
||||
return aliOssService.uploadByStream(bulkName, keyPath, fileName, inputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -435,6 +435,16 @@ public class FileServiceImpl implements FileService {
|
||||
// 生成上传文件的唯一key
|
||||
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileConform);
|
||||
|
||||
// 上传文件
|
||||
String fileUrl = fileManager.uploadByStream(
|
||||
fileUploadConfig.getBucketName(), tgtFileKey, dto.getFileName(), dto.getFileContent());
|
||||
|
||||
// 保存失败
|
||||
if (Utility.isBlank(fileUrl)) {
|
||||
log.error("fileUrl is empty");
|
||||
throw new BizException(CodeEnum.FILE_UPLOAD_FAILED);
|
||||
}
|
||||
|
||||
File ossFile = new File();
|
||||
ossFile.setAppChannelBucketNo(fileUploadConfig.getAppChannelBucketNo());
|
||||
ossFile.setAppCode(fileUploadConfig.getAppCode());
|
||||
@ -445,17 +455,6 @@ public class FileServiceImpl implements FileService {
|
||||
ossFile.setStorageUnit(fileUploadConfig.getStorageUnit());
|
||||
ossFile.setStorageSize(fileUploadConfig.getStorageSize());
|
||||
ossFile.setFileFormat(fileConform);
|
||||
// 上传文件
|
||||
String fileUrl = fileManager.uploadByStream(
|
||||
fileUploadConfig.getBucketName(), tgtFileKey, dto.getFileContent());
|
||||
|
||||
// 保存失败
|
||||
if (Utility.isBlank(fileUrl)) {
|
||||
log.error("fileUrl is empty");
|
||||
//fileDao.save(ossFile);
|
||||
throw new BizException(CodeEnum.FILE_UPLOAD_FAILED);
|
||||
}
|
||||
|
||||
ossFile.setFileUuid(uuid);
|
||||
ossFile.setFileUrl(fileUrl);
|
||||
ossFile.setUrlMd5(Utility.getMd5(fileUrl));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user