jfreechart在B/S生成图片过程(临时图片处理)-----参考自网络资源


二:
jfreechartB/S生成图片过程(临时图片处理)

开发过程中遇到的问题背景:出现temp文件家中的图片累计 不定时删除的话会导致文件夹文件日积月累

 

基于B/S结构的图片都是使用org.jfree.chart.servlet.DisplayChart这个类来显示的,大致过程如下: 

先是DisplayChartservice()方法调用ServletUtilitiessaveChartAsPNG (JFreeChart chart, int width, int height,HttpSession session)方法,在这个过程里,saveChartAsPNG方法先生成临时文件的名字

然后调用 org.jfree.chart.ChartUtilitiessaveChartAsPNG(File file, JFreeChart chart,int width,int height,ChartRenderingInfo info,boolean encodeAlpha),在服务器的临时文件夹里生成图片,接下来是把图片文件与 org.jfree.chart.servlet.ChartDeleter绑定(就是将图片文件名传递给这个类),ChartDeleter类继承了 HttpSessionBindingListener接口,主要作用是当用户session失效的时候,删除临时文件夹里的图片,这个类是存在于 session之中的。

最后调用ServletUtilitiessendTempFile(File file, HttpServletResponse response,String mimeType)用输出流输出图片。 
概括一下就是说,图片是存放于服务器的临时文件夹里,用户session失效后,程序会自动删除图片的。 

对于session为空时生成的图片名称中会被标记为on-time 

 

 

JfreeChart  saveChartAsJPEG源码

public static String saveChartAsJPEG(JFreeChart chart, int width, 
            int height, ChartRenderingInfo info, HttpSession session)
            throws IOException {

        if (chart == null) {
            throw new IllegalArgumentException("Null 'chart' argument.");   
        }
        
        ServletUtilities.createTempDir();
        String prefix = ServletUtilities.tempFilePrefix;
        if (session == null) {
            prefix = ServletUtilities.tempOneTimeFilePrefix;   
        }
        File tempFile = File.createTempFile(prefix, ".jpeg", 
                new File(System.getProperty("java.io.tmpdir")));
        ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
        if (session != null) {
            ServletUtilities.registerChartForDeletion(tempFile, session);
        }
        return tempFile.getName();

    }

 

 

saveChartAsPNG

public static java.lang.String saveChartAsPNG(JFreeChart chart,

                                              int width,

                                              int height,

                                              ChartRenderingInfo info,

                                              javax.servlet.http.HttpSession session)

                                       throws java.io.IOException

Saves the chart as a PNG format file in the temporary directory and populates the ChartRenderingInfo object which can be used to generate an HTML image map.

Parameters:

chart - the chart to be saved (null not permitted).

width - the width of the chart.

height - the height of the chart.

info - the ChartRenderingInfo object to be populated (null permitted).

session - the HttpSession of the client (if null, the temporary file is marked as "one-time" and deleted by the DisplayChartservlet right after it is streamed to the client).

Returns:

The filename of the chart saved in the temporary directory.

Throws:

java.io.IOException - if there is a problem saving the file.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值