JFreeChart显示

JFreeChart显示
今天利用JFreeChart结合DWR ReverseAjax实现服务器数据“推”到客户端;在客户端用JFreeChart默认的org.jfree.chart.servlet.DisplayChart
显示图片,会出现不同的客户端不能显示图片;查看DisplayChart源码
  public   void  service(HttpServletRequest request, 
                        HttpServletResponse response)
            
throws  ServletException, IOException {

        HttpSession session 
=  request.getSession();
        String filename 
=  request.getParameter( " filename " );

        
if  (filename  ==   null ) {
            
throw   new  ServletException( " Parameter 'filename' must be supplied " );
        }

        
//   Replace ".." with ""
        
//   This is to prevent access to the rest of the file system
        filename  =  ServletUtilities.searchReplace(filename,  " .. " "" );

        
//   Check the file exists
        File file  =   new  File(System.getProperty( " java.io.tmpdir " ), filename);
        
if  ( ! file.exists()) {
            
throw   new  ServletException( " File ' "   +  file.getAbsolutePath() 
                    
+   " ' does not exist " );
        }

        
//   Check that the graph being served was created by the current user
        
//   or that it begins with "public"
        boolean isChartInUserList = false;
        ChartDeleter chartDeleter 
= (ChartDeleter) session.getAttribute(
                
"JFreeChart_Deleter");
        
if (chartDeleter != null) {
            isChartInUserList 
= chartDeleter.isChartAvailable(filename);
        }

        
boolean isChartPublic = false;
        
if (filename.length() >= 6) {
            
if (filename.substring(06).equals("public")) {
                isChartPublic 
= true;
            }
        }

        
        
boolean  isOneTimeChart  =   false ;
        
if  (filename.startsWith(ServletUtilities.getTempOneTimeFilePrefix())) {
            isOneTimeChart 
=   true ;   
        }

        
if  (isChartInUserList  ||  isChartPublic  ||  isOneTimeChart) {
            
//   Serve it up
            ServletUtilities.sendTempFile(file, response);
            
if  (isOneTimeChart) {
                file.delete();   
            }
        }
        
else  {
            
throw   new  ServletException( " Chart image not found " );
        }
        
return ;
    }
其中无法显示的图片的原因跟 isChartInUserList  ||  isChartPublic  ||  isOneTimeChart 有关;其中isChartInUserList是为同一session,因服务器推是同时推向多客户端,这个isChartInUserList为false是没法改变;isOneTimeChart是在创建chart的时候,如果session为null则会记录该chart为one-time use,显示一次后会被删除;因此想从isChartPublic入手了;但是JFreeChart API并没发现提供如何产生public+filename的chart;因此重写DisplayChart默认为public,这样所有的客户端都可以显示。





你可能感兴趣的:(JFreeChart显示)