笔者使用 tomcat8 部署了一个 web 项目,项目使用 Java 内置的 ImageIO API 来压缩图片。某天更新了项目的前端代码,但是导致图片压缩直接出现如下错误:
2024-01-14 15:00:35 [http-nio-8020-exec-2] [ERROR] com.huangx.website.controller.website.WebsiteBaseController - 压缩图片失败 javax.imageio.IIOException: Can't create cache file! at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:423) at net.coobird.thumbnailator.tasks.io.FileImageSink.write(Unknown Source) at net.coobird.thumbnailator.tasks.SourceSinkThumbnailTask.write(Unknown Source) at net.coobird.thumbnailator.Thumbnailator.createThumbnail(Unknown Source) at net.coobird.thumbnailator.Thumbnails$Builder.toFile(Unknown Source) ... at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) Caused by: java.nio.file.NoSuchFileException: /root/tomcat-8.5.73/temp/imageio477449883252719137.tmp at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86) at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214) at java.nio.file.Files.newByteChannel(Files.java:361) at java.nio.file.Files.createFile(Files.java:632) at java.nio.file.TempFileHelper.create(TempFileHelper.java:138) at java.nio.file.TempFileHelper.createTempFile(TempFileHelper.java:161) at java.nio.file.Files.createTempFile(Files.java:897) at javax.imageio.stream.FileCacheImageOutputStream.<init>(FileCacheImageOutputStream.java:88) at com.sun.imageio.spi.OutputStreamImageOutputStreamSpi.createOutputStreamInstance(OutputStreamImageOutputStreamSpi.java:68) at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:419) ... 50 common frames omitted
通过上面的日志可以明显看到,出现了 java.nio.file.NoSuchFileException 异常信息,原因是 /root/tomcat-8.5.73/temp/imageio477449883252719137.tmp 文件不存在导致的。
如果要解决这个问题,直接在 tomcat 主目录下面创建 temp 目录,问题将得到解决。