Merge pull request #262 from secondarycoder/dev

修复腾讯云上传文件可能导致的oom问题
pull/296/head
John Niang 2019-07-26 20:47:39 +08:00 committed by GitHub
commit 658ef3dbce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 0 deletions

View File

@ -54,6 +54,11 @@ public class TencentYunFileHandler implements FileHandler {
String ossStyleRule = optionService.getByPropertyOfNonNull(TencentYunProperties.OSS_STYLE_RULE).toString();
String ossSource = StringUtils.join("https://", ossBucketName, ".cos." + ossRegion+".myqcloud.com");
//get file attribute
long size = file.getSize();
String contentType = file.getContentType();
COSCredentials cred = new BasicCOSCredentials(ossAccessKey, ossAccessSecret);
Region region = new Region(ossRegion);
@ -74,6 +79,10 @@ public class TencentYunFileHandler implements FileHandler {
// Upload
ObjectMetadata objectMetadata = new ObjectMetadata();
//提前告知输入流的长度, 否则可能导致 oom
objectMetadata.setContentLength(size);
// 设置 Content type, 默认是 application/octet-stream
objectMetadata.setContentType(contentType);
PutObjectResult putObjectResponseFromInputStream = cosClient.putObject(ossBucketName, upFilePath, file.getInputStream(),objectMetadata);
if (putObjectResponseFromInputStream == null) {
throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到腾讯云失败 ");