运行一个注册显示验证码图片的小程序,之前在myeclipse上运行正常,但是今天移动到idea上就报错了,生成的图片显示不出来 javax.imageio.IIOException: Can't create output stream! javax.imageio.ImageIO.write(ImageIO.java:1574) cn.edu.sicau.utils.VerifyCode.output(VerifyCode.java:104) cn.edu.sicau.web.servlet.VerifyCodeServlet.doGet(VerifyCodeServlet.java:27) javax.servlet.http.HttpServlet.service(HttpServlet.java:635) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
javax.imageio.IIOException: Can't create cache file! javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:423) javax.imageio.ImageIO.write(ImageIO.java:1572) cn.edu.sicau.utils.VerifyCode.output(VerifyCode.java:104) cn.edu.sicau.web.servlet.VerifyCodeServlet.doGet(VerifyCodeServlet.java:27) javax.servlet.http.HttpServlet.service(HttpServlet.java:635) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.nio.file.AccessDeniedException: D:\Program Files\apache-tomcat-8.5.23\temp\imageio8710657563592930352.tmp sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83) sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230) java.nio.file.Files.newByteChannel(Files.java:361) java.nio.file.Files.createFile(Files.java:632) java.nio.file.TempFileHelper.create(TempFileHelper.java:138) java.nio.file.TempFileHelper.createTempFile(TempFileHelper.java:161) java.nio.file.Files.createTempFile(Files.java:897) javax.imageio.stream.FileCacheImageOutputStream.<init>(FileCacheImageOutputStream.java:88) com.sun.imageio.spi.OutputStreamImageOutputStreamSpi.createOutputStreamInstance(OutputStreamImageOutputStreamSpi.java:68) javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:419) javax.imageio.ImageIO.write(ImageIO.java:1572) cn.edu.sicau.utils.VerifyCode.output(VerifyCode.java:104) cn.edu.sicau.web.servlet.VerifyCodeServlet.doGet(VerifyCodeServlet.java:27) javax.servlet.http.HttpServlet.service(HttpServlet.java:635) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
参考网上的方法:https://blog.csdn.net/chenzenan/article/details/5604598
将这句代码ImageIO.write(image, "JPEG", out); 改成下面就可以了: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image);
再次运行就成功了
后来观察
java.nio.file.AccessDeniedException: D:\Program Files\apache-tomcat-8.5.23\temp\imageio8710657563592930352.tmp
这个错误,可能是和权限有关,我在本机win10 系统上把D:\Program Files\apache-tomcat-8.5.23\temp的权限修改称可写入
选中temp文件夹,右键选中属性
添加允许写入或修改权限,就可以。