gpt4 book ai didi

java - 服务器上的 ImageIO NullPointerException

转载 作者:行者123 更新时间:2023-11-28 22:08:53 24 4
gpt4 key购买 nike

当我运行我的 Java Servlet 时,我得到一个 NullPointerException

这是我的代码:

out.println("<image>");
int BI_WIDTH = 1000;
int BI_HEIGHT = 1400;
BufferedImage bImage = new BufferedImage(BI_WIDTH, BI_HEIGHT,BufferedImage.TYPE_INT_RGB);

Graphics2D g2d = bImage.createGraphics();
g2d.setBackground(Color.WHITE);
g2d.clearRect(0, 0, BI_WIDTH, BI_HEIGHT);
g2d.setColor(Color.BLACK);
BasicStroke bs = new BasicStroke(10);
g2d.setStroke(bs);
g2d.drawLine(30, 30, 800, 30);
//out.println("<status ref=\"done\" />");
//out.println("<status ref=\"" + bImage.getWidth() + "x" + bImage.getHeight() + "\" />");
out.println("<status ref=\"" + bImage.getWidth() + "x" + bImage.getHeight() + "\" />");

try {
ImageIO.write(bImage, "jpg", new File("C:\\test.jpg"));
} catch (IOException e) {
out.println("<status ref=\"" + e +"\" />");
}
out.println("</image>");

这是我从 Tomcat 得到的错误:

description The server encountered an internal error () that prevented it from fulfilling this request.exception
java.lang.NullPointerException
javax.imageio.ImageIO.write(ImageIO.java:1538)
SendBasicReport.doGet(SendBasicReport.java:62)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

最佳答案

可能是 "C:\" 在您的服务器上不存在,或者它无法从 Web 服务器访问或写入。

或者,您想要的格式可能是 "jpeg" 而不是 "jpg"

发送到 ImageIO.write 的其他参数看起来没问题。

关于java - 服务器上的 ImageIO NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17045337/

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