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.

你可能感兴趣的:(jfreechart)