资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
修改JFreeChart 图片的默认存放路径 JFreeChart会把生成的图片,默认放在应用服务器的temp目录下,有的时候我们是不能放在该目录下的,需要改变这个默认的存放路径发现jfreechart的ServletUtilities类里有protected static void createTempDir() String tempDirName = System.getProperty(java.io.tmpdir);/这里我的系统默认的临/时文件路径:C: UsersADMINI1AppDataLocalTempif (tempDirName = null) throw new RuntimeException(应用服务器目录下不存在 temp目录或该目录无法创建 );/ create the temporary directory if it doesnt existFile tempDir = new File(tempDirName);if (!tempDir.exists() tempDir.mkdirs();该方法创建了默认的图片存放路径在该类的saveChartAsPNG() 和saveChartAsJPEG () 里被调用,产生图形,因此我们的思路就是将ServletUtilities的saveChartAsPNG() 和saveChartAsJPEG () 这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 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();修改后如下:public static String saveChartAsJPEG(JFreeChart chart, int width, int height, ChartRenderingInfo info, HttpSession session)throws IOException /从application中读取出临时文件目录, 我事先已经在系统启动时 ,创建了目录File tempDr=(File)session.getServletContext().getAttribute(tempDirectory);if (chart = null) throw new IllegalArgumentException(chart 对象为空); UIServletUtilities.createTempDir();String prefix = UIServletUtilities.tempFilePrefix;if (session = null) prefix = UIServletUtilities.tempOneTimeFilePrefix; File tempFile = File.createTempFile(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);String graphURL = request.getContextPath() + /DisplayChart?filename= + filename;来生成图形了下面是具体做法:JFreeChart自己的路径是这样生成的:首先它会使用类ServletUtilities的createTempDir()来产生一个临时文件夹,生成的图片默认保存在tomcat/temp下,要想修改该路径,需要继承ServletUtilities,并且重写createTempDir()和saveChartAsPNG(JFreeChart chart, int width, int height, ChartRenderingInfo 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 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 IllegalArgumentException(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);
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号