jfreechart图片默认存放路径的修改

上传人:豆浆 文档编号:874163 上传时间:2017-05-20 格式:DOC 页数:4 大小:40KB
返回 下载 相关 举报
jfreechart图片默认存放路径的修改_第1页
第1页 / 共4页
jfreechart图片默认存放路径的修改_第2页
第2页 / 共4页
jfreechart图片默认存放路径的修改_第3页
第3页 / 共4页
jfreechart图片默认存放路径的修改_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《jfreechart图片默认存放路径的修改》由会员分享,可在线阅读,更多相关《jfreechart图片默认存放路径的修改(4页珍藏版)》请在金锄头文库上搜索。

1、修改JFreeChart 图片的默认存放路径 JFreeChart会把生成的图片,默认放在应用服务器的temp目录下,有的时候我们是不能放在该目录下的,需要改变这个默认的存放路径发现jfreechart的ServletUtilities类里有protected static void createTempDir() String tempDirName = System.getProperty(java.io.tmpdir);/这里我的系统默认的临/时文件路径:C: UsersADMINI1AppDataLocalTempif (tempDirName = null) throw new Ru

2、ntimeException(应用服务器目录下不存在 temp目录或该目录无法创建 );/ create the temporary directory if it doesnt existFile tempDir = new File(tempDirName);if (!tempDir.exists() tempDir.mkdirs();该方法创建了默认的图片存放路径在该类的saveChartAsPNG() 和saveChartAsJPEG () 里被调用,产生图形,因此我们的思路就是将ServletUtilities的saveChartAsPNG() 和saveChartAsJPEG ()

3、 这2 个方法改造成自己定义的方法修改前源文件如下: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 p

4、refix = 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) Servle

5、tUtilities.registerChartForDeletion(tempFile, session);return tempFile.getName();修改后如下:public static String saveChartAsJPEG(JFreeChart chart, int width, int height, ChartRenderingInfo info, HttpSession session)throws IOException /从application中读取出临时文件目录, 我事先已经在系统启动时 ,创建了目录File tempDr=(File)session.ge

6、tServletContext().getAttribute(tempDirectory);if (chart = null) throw new IllegalArgumentException(chart 对象为空); UIServletUtilities.createTempDir();String prefix = UIServletUtilities.tempFilePrefix;if (session = null) prefix = UIServletUtilities.tempOneTimeFilePrefix; File tempFile = File.createTempF

7、ile(prefix, .jpeg, tempDr);ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);if (session != null) UIServletUtilities.registerChartForDeletion(tempFile, session);return tempFile.getName();接下来就可以使用String filename = ServletUtilities.saveChartAsJPEG(chart, 800, 600, info, session);Str

8、ing graphURL = request.getContextPath() + /DisplayChart?filename= + filename;来生成图形了下面是具体做法:JFreeChart自己的路径是这样生成的:首先它会使用类ServletUtilities的createTempDir()来产生一个临时文件夹,生成的图片默认保存在tomcat/temp下,要想修改该路径,需要继承ServletUtilities,并且重写createTempDir()和saveChartAsPNG(JFreeChart chart, int width, int height, ChartRend

9、eringInfo info, HttpSession session).public class BarChart extends ServletUtilities/为生成的图片创建文件夹protected static void createTempDir()String tempDirName = D:/graph; /此路径可以在属性文件中配置if (tempDirName = null)throw new RuntimeException(Temporary directory system property + (java.io.tmpdir) is null.);/ create

10、 the temporary directory if it doesnt existFile tempDir = new File(tempDirName);if (!tempDir.exists()tempDir.mkdirs();/覆盖父类的方法public static String saveChartAsPNG(JFreeChart chart, int width, int height,ChartRenderingInfo info, HttpSession session) throws IOException if (chart = null)throw new Illega

11、lArgumentException(Null chart argument.); createTempDir();String prefix = ServletUtilities.getTempFilePrefix();if (session = null)prefix = ServletUtilities.getTempOneTimeFilePrefix();File tempFile = File.createTempFile(prefix, .png, new File(D:/graph);ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);if (session != null)ServletUtilities.registerChartForDeletion(tempFile, session);return tempFile.getName();/ 在其他方法中调用子类的方法,不用父类的方法public String createChart()/ 省略其他代码.String filename =new BarChart(). saveChartAsPNG(chart, 600, 400, info, session);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号