出现错误:
Jan 23, 2015 4:19:21 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [DisplayChart] in context with path [/aldb] threw exception [Chart image not found] with root cause
javax.servlet.ServletException: Chart image not found
at org.jfree.chart.servlet.DisplayChart.service(DisplayChart.java:149)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
html源代码:
/DisplayChart?filename=jfreechart-5351821214105924657.png
文件存在:
find / -name jfreechart-5351821214105924657.png -print
/home/soft/tomcat7/temp/jfreechart-5351821214105924657.png
ll /home/soft/tomcat7/temp/jfreechart-5351821214105924657.png
-rw-r--r-- 1 root root 21517 Jan 23 16:19 /home/soft/tomcat7/temp/jfreechart-5351821214105924657.png
相关代码:
File file = new File(System.getProperty("java.io.tmpdir"), filename);
if (!(file.exists())) {
throw new ServletException("File '" + file.getAbsolutePath() + "' does not exist");
} boolean isChartInUserList = false;
ChartDeleter chartDeleter = (ChartDeleter)session.getAttribute("JFreeChart_Deleter"); if (chartDeleter != null) {
isChartInUserList = chartDeleter.isChartAvailable(filename);
} boolean isChartPublic = false;
if ((filename.length() >= 6) &&
(filename.substring(0, 6).equals("public"))) {
isChartPublic = true;
} boolean isOneTimeChart = false;
if (filename.startsWith(ServletUtilities.getTempOneTimeFilePrefix())) {
isOneTimeChart = true;
} if ((isChartInUserList) || (isChartPublic) || (isOneTimeChart))
{
ServletUtilities.sendTempFile(file, response);
if (isOneTimeChart)
file.delete();
}
else
{
throw new ServletException("Chart image not found");
}
解决方法:
ServletUtilities.setTempOneTimeFilePrefix("jfreechart"); // 加了这个即可;使得上面代码的第20行结果为true;
String filename = ServletUtilities.saveChartAsPNG(chart, 1024, 400, null, session);
官方解释:
My web application runs fine on my Windows development machine, but when I deploy it to the Unix/Linux production server, it doesn't work. What is the problem?
Most likely your server does not have X11 running. This is a Java (AWT/Java2D) issue, not something that is specific to JFreeChart. There is some more information at Sun's website:
In addition, the following thread in the JFreeChart forum contains some useful information:
REF:
关于jfreechart的几个知识点
http://wangrusheng5200.iteye.com/blog/406876
http://www.jfree.org/jfreechart/faq.html
http://www.cnblogs.com/dkblog/archive/2007/09/25/1980861.html
http://www.2cto.com/os/201411/354304.html
http://www.linuxidc.com/Linux/2011-04/34633.htm