gpt4 book ai didi

java - ImageIO 写入方法 - javax imageio IIOException 无法创建输出流

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:11:04 27 4
gpt4 key购买 nike

protected void doGet(HttpServletRequest req, HttpServletResponse response)
throws IOException, ServletException {
// Expire response
try {
//
ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
byte[] captchaBytes;

BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
Hashtable<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
Random r = new Random();
String token = Long.toString(Math.abs(r.nextLong()), 36);
String ch = token.substring(0, 6);
Color c = new Color(0.6662f, 0.4569f, 0.3232f);
GradientPaint gp = new GradientPaint(30, 30, c, 15, 25,
Color.white, true);
graphics2D.setPaint(gp);
Font font = new Font("Verdana", Font.CENTER_BASELINE, 26);
graphics2D.setFont(font);
graphics2D.drawString(ch, 2, 22);
graphics2D.dispose();

HttpSession session = req.getSession(true);
session.setAttribute(CAPTCHA_KEY, ch);

OutputStream outputStream = response.getOutputStream();
ImageIO.write(image, "jpeg", imgOutputStream);
captchaBytes = imgOutputStream.toByteArray();
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Max-Age", 0);
response.setContentType("image/jpeg");
outputStream.write(captchaBytes);
outputStream.close();
utils.PQSoftLogger.getInstance().log(
"Captch servlet: " + image.toString());
} catch (Exception e) {
utils.PQSoftLogger.getInstance().log(
e.toString() + ":" + e.getMessage());
for (StackTraceElement s : e.getStackTrace()) {
utils.PQSoftLogger.getInstance().log(
s.getLineNumber() + ":" + s.getMethodName() + ":"
+ s.getFileName());
}
} finally {
utils.PQSoftLogger.getInstance().log("temp dir-"+System.getProperty("java.io.tmpdir"));
utils.PQSoftLogger.getInstance().log("Captch servlet executed");
}
}

在 tomcat 服务器环境中调用 servlet 时,ImageIO.write 方法会抛出以下异常,但它会在本地系统上正常工作。

javax.imageio.IIOException:无法创建输出流!

最佳答案

我刚刚遇到了同样的问题。我之前删除了 Tomcat 安装下的临时目录。 Tomcat 不会在启动时创建此目录。再次添加临时目录后,问题就解决了。

关于java - ImageIO 写入方法 - javax imageio IIOException 无法创建输出流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21043313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com