随笔 - 37  文章 - 14  trackbacks - 0
<2009年12月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿

随笔分类

随笔档案

文章分类

相关链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜

今天利用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,这样所有的客户端都可以显示。





posted on 2008-09-09 17:26 扭曲的铅笔 阅读(922) 评论(1)  编辑  收藏 所属分类: J2EE

FeedBack:
# re: JFreeChart显示 2009-12-18 14:57 songkewei
能不能把你的源代码发给我让我学习一下,谢谢,谢谢!
songkewei110@126.com  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: