​​​​​​​javax.imageio.IIOException: Can't create output stream!

运行一个注册显示验证码图片的小程序,之前在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文件夹,右键选中属性

添加允许写入或修改权限,就可以。

 

要将多页 TIFF 转换成一张图片,你可以使用 Java Advanced Imaging API(JAI)和 Java Image IO API。以下是一个示例代码: ```java import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriter; import javax.imageio.metadata.IIOMetadata; import javax.imageio.plugins.tiff.TIFFImageReadParam; import javax.imageio.plugins.tiff.TIFFImageWriteParam; import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageOutputStream; import javax.media.jai.JAI; import javax.media.jai.RenderedOp; public class TIFFUtils { public static BufferedImage convertToSinglePageTIFF(byte[] tiffBytes) throws IOException { ImageReader reader = ImageIO.getImageReadersByFormatName("TIFF").next(); ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(tiffBytes)); reader.setInput(iis); // Read the first page RenderedOp op = JAI.create("stream", reader); BufferedImage image = op.getAsBufferedImage(); // Write the first page to a new TIFF file ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageWriter writer = ImageIO.getImageWritersByFormatName("TIFF").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); writer.setOutput(ios); TIFFImageWriteParam writeParam = (TIFFImageWriteParam) writer.getDefaultWriteParam(); writeParam.setCompressionMode(TIFFImageWriteParam.MODE_DEFAULT); writeParam.setCompressionType("LZW"); IIOMetadata metadata = writer.getDefaultImageMetadata(reader.getImageTypes(0).next(), writeParam); writer.write(metadata, new javax.imageio.IIOImage(image, null, metadata), writeParam); // Write the rest of the pages to the new TIFF file for (int i = 1; i < reader.getNumImages(true); i++) { op = JAI.create("stream", reader); image = op.getAsBufferedImage(); writer.write(null, new javax.imageio.IIOImage(image, null, null), writeParam); } writer.dispose(); ios.close(); iis.close(); // Read the new TIFF file and return the image byte[] singlePageTiffBytes = outputStream.toByteArray(); reader = ImageIO.getImageReadersByFormatName("TIFF").next(); iis = ImageIO.createImageInputStream(new ByteArrayInputStream(singlePageTiffBytes)); reader.setInput(iis); op = JAI.create("stream", reader); BufferedImage singlePageImage = op.getAsBufferedImage(); reader.dispose(); iis.close(); return singlePageImage; } } ``` 这个方法接受一个多页 TIFF 的字节数组作为输入,并返回一个合并后的 BufferedImage。它首先使用 Java Image IO API 读取多页 TIFF,然后将每个页面写入一个新的单页 TIFF 文件。接下来,它使用 Java Image IO API 读取新的单页 TIFF 文件,并返回合并后的 BufferedImage。 希望这可以帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值