public void photo(@RequestParam("file") MultipartFile file) throws IOException, IM4JavaException, InterruptedException {
//存儲到本地服務器
String fileName = file.getOriginalFilename(); // 獲取原始文件名
String filePath = CustomConfig.filePath; // 文件保存的路徑
File dest = new File(filePath + fileName); // 文件存儲路徑
// 判斷文件路徑是否存在,不存在就建立路徑
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
dest.createNewFile();
}
file.transferTo(dest);
//進行相關尺寸處理
IMOperation operation = new IMOperation();
ConvertCmd cmd = new ConvertCmd();
String filePath1 = dest.toString();
operation.addImage(filePath1);
File picture = new File(filePath1);
FileInputStream fis = new FileInputStream(picture);
BufferedImage sourceImg = ImageIO.read(fis);
int width = sourceImg.getWidth();// 原圖寬度
int height = sourceImg.getHeight();// 原圖高度
if (width > 1920 || height > 1080) {
operation.resize(1920, 1080);
}
String filePath2 = filePath + System.currentTimeMillis() + CustomConfig.fileSuffixName;
operation.addImage(filePath2);
cmd.setSearchPath(CustomConfig.imageMagickPath); //Windows須要設置,Linux不須要
cmd.run(operation);
fis.close();
}複製代碼