close stream

This commit is contained in:
inighty committed 2022-04-11 17:03:05 +08:00
1 parent a7df615d95
commit 17f17f150b
2 files changed
+12 -6

No files matched your search

@@ -22,11 +22,13 @@ import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.pig4cloud.pigx.common.oss.service.OssTemplate;
import lombok.AllArgsConstructor;
import lombok.Cleanup;
import lombok.SneakyThrows;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -84,7 +86,9 @@ public class OssEndpoint {
@PostMapping("/object/{bucketName}")
public S3Object createObject(@RequestBody MultipartFile object, @PathVariable String bucketName) {
String name = object.getOriginalFilename();
template.putObject(bucketName, name, object.getInputStream(), object.getSize(), object.getContentType());
@Cleanup
InputStream inputStream = object.getInputStream();
template.putObject(bucketName, name, inputStream, object.getSize(), object.getContentType());
return template.getObjectInfo(bucketName, name);
}
@@ -93,7 +97,9 @@ public class OssEndpoint {
@PostMapping("/object/{bucketName}/{objectName}")
public S3Object createObject(@RequestBody MultipartFile object, @PathVariable String bucketName,
@PathVariable String objectName) {
template.putObject(bucketName, objectName, object.getInputStream(), object.getSize(), object.getContentType());
@Cleanup
InputStream inputStream = object.getInputStream();
template.putObject(bucketName, objectName, inputStream, object.getSize(), object.getContentType());
return template.getObjectInfo(bucketName, objectName);
}
@@ -37,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
@@ -68,13 +69,12 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> impl
resultMap.put("fileName", fileName);
resultMap.put("url", String.format("/admin/sys-file/%s/%s", ossProperties.getBucketName(), fileName));
try {
minioTemplate.putObject(ossProperties.getBucketName(), fileName, file.getInputStream(),
try (InputStream inputStream = file.getInputStream()) {
minioTemplate.putObject(ossProperties.getBucketName(), fileName, inputStream,
file.getContentType());
// 文件管理数据记录,收集管理追踪文件
fileLog(file, fileName);
}
catch (Exception e) {
} catch (Exception e) {
log.error("上传失败", e);
return R.failed(e.getLocalizedMessage());
}