mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-03 12:39:12 +08:00
refactor(项目设置): 图片压缩方法增加锁保护
This commit is contained in:
parent
7f2b0ca14a
commit
7efe629986
@ -63,7 +63,6 @@ public class TempFileUtils {
|
||||
g2d.drawImage(originalImage, 0, 0, previewWidth, previewHeight, null);
|
||||
g2d.dispose();
|
||||
ImageIO.setUseCache(false);
|
||||
|
||||
ImageIO.write(previewImage, "JPEG", outputStream);
|
||||
return outputStream.toByteArray();
|
||||
} catch (Exception e) {
|
||||
|
@ -200,8 +200,7 @@ public class FileManagementService {
|
||||
|
||||
if (previewImg == null || previewImg.length == 0) {
|
||||
try {
|
||||
byte[] fileBytes = this.getFile(fileMetadata);
|
||||
previewImg = TempFileUtils.compressPic(fileBytes);
|
||||
previewImg = this.compressPicWithFileMetadata(fileMetadata);
|
||||
fileService.upload(previewImg, previewRequest);
|
||||
return previewImg;
|
||||
} catch (Exception e) {
|
||||
@ -210,4 +209,10 @@ public class FileManagementService {
|
||||
}
|
||||
return previewImg;
|
||||
}
|
||||
|
||||
//获取文件并压缩的方法需要上锁,防止并发超过一定数量时内存溢出
|
||||
private synchronized byte[] compressPicWithFileMetadata(FileMetadata fileMetadata) throws Exception {
|
||||
byte[] fileBytes = this.getFile(fileMetadata);
|
||||
return TempFileUtils.compressPic(fileBytes);
|
||||
}
|
||||
}
|
||||
|
@ -287,6 +287,7 @@ public class FileMetadataService {
|
||||
String filePath = fileService.upload(file, uploadFileRequest);
|
||||
|
||||
if (TempFileUtils.isImage(fileMetadata.getType())) {
|
||||
//图片文件自动生成预览图
|
||||
byte[] previewImg = TempFileUtils.compressPic(file.getBytes());
|
||||
uploadFileRequest.setFolder(DefaultRepositoryDir.getFileManagementPreviewDir(fileMetadata.getProjectId()));
|
||||
fileService.upload(previewImg, uploadFileRequest);
|
||||
|
Loading…
Reference in New Issue
Block a user