使用 Thumbnails进行图片压缩时报错:
No suitable ImageReader found for 文件路径/文件名.jpg
初始代码:
Thumbnails.of(address).scale(0.25f).toFile( realPath+"\\"+type+"\\"+"copy.jpg");
依赖:
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
本地测试无报错,但是与前端进行联调时报错:No suitable ImageReader found
经查阅信息知是Java不支持格式异常。
将代码改为:
byte[] bytes = file.getBytes();
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(bytes));
Thumbnails.of(originalImage).scale(0.25f).toFile(realPath+"\\"+type+"\\"+"copy.jpg");
其中file是MultipartFile类型,所以可以直接获取bytes
如果是File类型需要以下方式获取bytes数组
File f = new File(file);
int length = (int) f.length();
byte[] data = new byte[length];
再起启动时就解决了报错问题