我尝试使用 Spring mvc 控制器以动态方式生成图像。
@Controller
@RequestMapping("/")
public String generateMik(final HttpServletResponse response){
...
BufferdImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
...
response.setContentType("image/png");
try {
OutputStream os = response.getOutputStream();
ImageIO.write(buffer, "png", os);
os.flush();
os.close();
...
}
我在 .jsp 中展示了这张图片:
<img src="picture" id="picture">
一切正常,图片显示在我的浏览器中,但我遇到了一个异常,我不知道正常解决这个问题的原因(我不想捕捉这个异常)。
org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:626)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:215)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)
...
我的问题是:我如何以可能简单的方式生成动态图片?我不想临时保存图片。也许放在模型 outputStream 并在 jsp 中以某种神奇的方式显示?