mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-04 21:19:52 +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.drawImage(originalImage, 0, 0, previewWidth, previewHeight, null);
|
||||||
g2d.dispose();
|
g2d.dispose();
|
||||||
ImageIO.setUseCache(false);
|
ImageIO.setUseCache(false);
|
||||||
|
|
||||||
ImageIO.write(previewImage, "JPEG", outputStream);
|
ImageIO.write(previewImage, "JPEG", outputStream);
|
||||||
return outputStream.toByteArray();
|
return outputStream.toByteArray();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -200,8 +200,7 @@ public class FileManagementService {
|
|||||||
|
|
||||||
if (previewImg == null || previewImg.length == 0) {
|
if (previewImg == null || previewImg.length == 0) {
|
||||||
try {
|
try {
|
||||||
byte[] fileBytes = this.getFile(fileMetadata);
|
previewImg = this.compressPicWithFileMetadata(fileMetadata);
|
||||||
previewImg = TempFileUtils.compressPic(fileBytes);
|
|
||||||
fileService.upload(previewImg, previewRequest);
|
fileService.upload(previewImg, previewRequest);
|
||||||
return previewImg;
|
return previewImg;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -210,4 +209,10 @@ public class FileManagementService {
|
|||||||
}
|
}
|
||||||
return previewImg;
|
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);
|
String filePath = fileService.upload(file, uploadFileRequest);
|
||||||
|
|
||||||
if (TempFileUtils.isImage(fileMetadata.getType())) {
|
if (TempFileUtils.isImage(fileMetadata.getType())) {
|
||||||
|
//图片文件自动生成预览图
|
||||||
byte[] previewImg = TempFileUtils.compressPic(file.getBytes());
|
byte[] previewImg = TempFileUtils.compressPic(file.getBytes());
|
||||||
uploadFileRequest.setFolder(DefaultRepositoryDir.getFileManagementPreviewDir(fileMetadata.getProjectId()));
|
uploadFileRequest.setFolder(DefaultRepositoryDir.getFileManagementPreviewDir(fileMetadata.getProjectId()));
|
||||||
fileService.upload(previewImg, uploadFileRequest);
|
fileService.upload(previewImg, uploadFileRequest);
|
||||||
|
Loading…
Reference in New Issue
Block a user