JFreeChart Struts2组件报IllegalStateException解决方案
程序员文章站
2022-06-11 12:16:42
...
在s2sh框架中应用了JFreeChart组件,使用了struts2-jfreechart-plugin,jfreechart生成图片的时候在服务器后台会产生如下的错误,但是图片却能够正常的显示出来。
这个问题困扰了我很久,当请求JFreeChart图片时突然中断或者第一次请求图片还没完成就发生第二次请求时就会抛出这个异常。
网上解释说是产生图片过程中发生了io异常,修改struts plugins包里ChartResult.java源文件。
经测试问题依旧存在,查看struts2源码采用如下办法解决,在org.apache.struts2.dispatcher.Dispatcher 770行左右:
问题解决。
严重: Servlet.service() for servlet default threw exception java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407) at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:770) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:505) at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:574) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2018) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:651) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:676) at java.lang.Thread.run(Thread.java:595)
这个问题困扰了我很久,当请求JFreeChart图片时突然中断或者第一次请求图片还没完成就发生第二次请求时就会抛出这个异常。
网上解释说是产生图片过程中发生了io异常,修改struts plugins包里ChartResult.java源文件。
... HttpServletResponse response = ServletActionContext.getResponse(); //OutputStream os = response.getOutputStream();//原来是这么写的 OutputStream os = null; try { os = response.getOutputStream();//挪到try里面 // check the type to see what kind of output we have to produce if ("png".equalsIgnoreCase(type)) { response.setContentType("image/png"); ChartUtilities.writeChartAsPNG(os, chart, getIntValueFromString(width), getIntValueFromString(height)); } else if ("jpg".equalsIgnoreCase(type) || "jpeg".equalsIgnoreCase(type)) { response.setContentType("image/jpg"); } else throw new IllegalArgumentException(type + " is not a supported render type (only JPG and PNG are)."); } catch (IOException e) {//加上异常捕获 } finally { if (os != null) os.flush(); } ...
经测试问题依旧存在,查看struts2源码采用如下办法解决,在org.apache.struts2.dispatcher.Dispatcher 770行左右:
try { // WW-1977: Only put errors in the request when code is a 500 error if (code == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) { // send a http error response to use the servlet defined error handler // make the exception availible to the web.xml defined error page request.setAttribute("javax.servlet.error.exception", e); // for compatibility request.setAttribute("javax.servlet.jsp.jspException", e); } // send the error response response.sendError(code, e.getMessage()); } catch (IOException e1) { // we're already sending an error, not much else we can do if more stuff breaks } catch (IllegalStateException e2) { //添加IllegalStateException 异常捕捉 }
问题解决。