|
@@ -132,6 +132,7 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
|
|
|
// 初始化 CosClient
|
|
|
COSClient cosClient = getClient();
|
|
|
+ InputStream inputStream = null;
|
|
|
try {
|
|
|
|
|
|
// [配置]
|
|
@@ -164,7 +165,8 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
// // --------------------------------------------------------------------
|
|
|
|
|
|
// 创建 PutObjectRequest 对象,PutObject 请求。(异步)
|
|
|
- PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, object_key, multipartFile.getInputStream(), metadata);
|
|
|
+ inputStream = multipartFile.getInputStream();
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, object_key, inputStream, metadata);
|
|
|
|
|
|
// 普通上传 (没有进度)
|
|
|
// // cosClient.putObject(putObjectRequest);
|
|
@@ -188,57 +190,16 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
} catch (InterruptedException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
} finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 忽略关闭流时的异常
|
|
|
+ }
|
|
|
+ }
|
|
|
if (cosClient != null) cosClient.shutdown();
|
|
|
}
|
|
|
|
|
|
-// // 获得文件分类列表
|
|
|
-// SysFileCategoryDTO categoryDTO = new SysFileCategoryDTO();
|
|
|
-// List<SysFileCategoryDTO> categoryList = sysFileCategoryMapper.queryFileCategoryList(categoryDTO);
|
|
|
-//
|
|
|
-// // 获得图片分类的后缀 (待做),再进行以下判断
|
|
|
-// // 还有删除也要同时删除缩略图
|
|
|
-// // 还有大文件分片也要做缩略图
|
|
|
-// // 还有 视频要做截图生成
|
|
|
-//
|
|
|
-// boolean isImage = Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "tiff", "webp").contains(suffix.toLowerCase());
|
|
|
-//
|
|
|
-// // 检查对象是否存在于存储桶中
|
|
|
-// if (checkObjectExist(cosClient, key)) {
|
|
|
-// // -- [COS] 对象存在时,直接返回路径 -------------------------------
|
|
|
-// resp.setIs_fast_upload(true);
|
|
|
-//
|
|
|
-// } else {
|
|
|
-// // 获取文件内容
|
|
|
-// InputStream inputStream = file.getInputStream();
|
|
|
-//
|
|
|
-// // 将文件内容写入临时文件
|
|
|
-// String tempFilename = CommonUtil.generateFilename(null);
|
|
|
-// File tempFile = File.createTempFile(tempFilename, null);
|
|
|
-// try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
|
|
|
-// IOUtils.copy(inputStream, outputStream);
|
|
|
-// }
|
|
|
-// // 创建 PutObjectRequest 对象,PutObject 请求。(异步)
|
|
|
-// PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, tempFile);
|
|
|
-// cosClient.putObject(putObjectRequest);
|
|
|
-//
|
|
|
-//
|
|
|
-// // 创建缩略图
|
|
|
-// if (isImage) {
|
|
|
-// File tempFileThumb = File.createTempFile(tempFilename, "." + suffix);
|
|
|
-// String sourceFilePath = tempFileThumb.getPath();
|
|
|
-// File sourceFile = new File(sourceFilePath);
|
|
|
-// // 使用Thumbnailator生成缩略图并写入到临时文件
|
|
|
-// Thumbnails.of(tempFile).size(60, 60).outputQuality(0.2).toFile(tempFileThumb);
|
|
|
-// PutObjectRequest putObjectRequestThumb = new PutObjectRequest(bucketName, keyThumb, sourceFile);
|
|
|
-// cosClient.putObject(putObjectRequestThumb);
|
|
|
-// resp.setPath_thumb(remotePathThumb);
|
|
|
-// tempFileThumb.delete();
|
|
|
-// }
|
|
|
-//
|
|
|
-// tempFile.delete();
|
|
|
-//
|
|
|
-// }
|
|
|
-
|
|
|
}
|
|
|
|
|
|
// [腾讯云COS] 查询对象是否存在
|
|
@@ -255,7 +216,6 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
public void deleteObject(String object_key) {
|
|
|
COSClient cosClient = getClient();
|
|
|
cosClient.deleteObject(BUCKET_NAME, object_key);
|
|
|
- // cosClient.deleteObject(BUCKET_NAME, FilenameUtil.insertThumbSuffix(object_key));
|
|
|
cosClient.shutdown();
|
|
|
}
|
|
|
|
|
@@ -307,6 +267,7 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
@Override
|
|
|
public UploadPartResult uploadPart(MultipartFile multipartFile, String upload_id, String object_key, Integer partNumber) {
|
|
|
COSClient cosClient = getClient();
|
|
|
+ InputStream inputStream = null;
|
|
|
try {
|
|
|
|
|
|
// "The parameter partNumber is not valid
|
|
@@ -314,14 +275,15 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
long partSize = 524288;
|
|
|
|
|
|
|
|
|
-
|
|
|
// 加 partNumber 和 partSize 入参吧
|
|
|
|
|
|
UploadPartRequest uploadPartRequest = new UploadPartRequest();
|
|
|
uploadPartRequest.setBucketName(BUCKET_NAME);
|
|
|
uploadPartRequest.setKey(object_key);
|
|
|
uploadPartRequest.setUploadId(upload_id);
|
|
|
- uploadPartRequest.setInputStream(multipartFile.getInputStream());
|
|
|
+
|
|
|
+ inputStream = multipartFile.getInputStream();
|
|
|
+ uploadPartRequest.setInputStream(inputStream);
|
|
|
// uploadPartRequest.setPartSize(multipartFile.getSize()); // 设置分块的长度
|
|
|
uploadPartRequest.setPartSize(partSize); // 设置分块的长度
|
|
|
// uploadPartRequest.setPartNumber(partNumber); // 设置要上传的分块编号,从 1 开始
|
|
@@ -329,10 +291,6 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
return cosClient.uploadPart(uploadPartRequest);
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
// // 每个分块上传之后都会得到一个返回值 etag,保存起来用于最后合并分块时使用
|
|
|
// List<PartETag> partETags = new LinkedList<>();
|
|
|
// // 上传数据, 这里上传 10 个 1M 的分块数据
|
|
@@ -367,6 +325,13 @@ public class TencentCosServiceImpl implements TencentCosService {
|
|
|
} catch (IOException | CosClientException e) {
|
|
|
throw new CustException(e.getMessage());
|
|
|
} finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 忽略关闭流时的异常
|
|
|
+ }
|
|
|
+ }
|
|
|
if (cosClient != null) cosClient.shutdown();
|
|
|
}
|
|
|
}
|