jfreechart不能自动删除生成的图片

很多情况下jfreechart 在HttpSession失效时不能按预期的自动删除所生成的图片。

通过分析ChartDeleter的原码

 public void valueUnbound(HttpSessionBindingEvent event) {

        Iterator iter = this.chartNames.listIterator();
        while (iter.hasNext()) {
            String filename = (String) iter.next();
            File file = new File(System.getProperty("java.io.tmpdir"), filename);
            if (file.exists()) {
                file.delete();
            }
        }
        return;

    }

可知,原因在于通常生成的图片的路径并不总是System.getProperty("java.io.tmpdir")。

解决办法:

修改ChartDeleter.java原文件如下

...

 public ChartDeleter(HttpSession httpSession) {
        super();
        this.httpSession = httpSession;
    }

public void valueUnbound(HttpSessionBindingEvent event) {

        Iterator iter = this.chartNames.listIterator();
        while (iter.hasNext()) {
            String filename = (String) iter.next();
            File file = new File(this.httpSession.getServletContext().getRealPath("/"), filename);
            if (file.exists()) {
                file.delete();
            }
        }
        return;

    }

...

 

修改ServletUtilities.java如下:

。。。

public static String saveChartAsPNG(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.getTempFilePrefix();
     if (session == null) {
         prefix = ServletUtilities.getTempOneTimeFilePrefix();
     }
     File tempFile = File.createTempFile(prefix, ".png",
             new File(session.getServletContext().getRealPath("/")));
     ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
     if (session != null) {
         ServletUtilities.registerChartForDeletion(tempFile, session);
     }
     return tempFile.getName();
  
 }

。。。

就是把图片存在项目根目录下。

其实更合适的方式是不修改源文件,通过继承、重写方法的途径是更好的选择。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 添加JFreeChart依赖 在项目中添加以下依赖: ``` <dependency> <groupId>org.jfree</groupId> <artifactId>jfreechart</artifactId> <version>1.5.0</version> </dependency> ``` 2. 创建JFreeChart对象 首先,我们需要创建一个JFreeChart对象,用于生成图像。在这个例子中,我们将创建一个折线图。 ``` // 创建数据集 XYSeries series = new XYSeries("Data"); series.add(1, 2); series.add(2, 3); series.add(3, 4); XYSeriesCollection dataset = new XYSeriesCollection(series); // 创建图表 JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y", dataset); ``` 3. 生成图片 接下来,我们需要将JFreeChart对象转换为图像。我们可以使用ChartUtilities类来完成这个任务。 ``` // 生成图片 ByteArrayOutputStream out = new ByteArrayOutputStream(); ChartUtilities.writeChartAsPNG(out, chart, 400, 300); // 将图片转换为字节数组 byte[] imageBytes = out.toByteArray(); ``` 注意,这里我们将图片转换为字节数组以便后续操作。 4. 显示图片 最后,我们将字节数组转换为Image对象,然后将其显示在Swing组件中。 ``` // 将字节数组转换为Image对象 Image image = Toolkit.getDefaultToolkit().createImage(imageBytes); // 显示图片 JLabel label = new JLabel(new ImageIcon(image)); JFrame frame = new JFrame(); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true); ``` 这个例子演示了如何使用JFreeChart生成图片并将其显示在Swing组件中。您可以根据需要修改代码以生成不同类型的图表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值