欢迎来到天天文库
浏览记录
ID:62159597
大小:23.50 KB
页数:5页
时间:2021-04-19
《jfreechart图片默认存放路径的修改.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、个人收集整理勿做商业用途修改JFreeChart图片的默认存放路径JFreeChart会把生成的图片,默认放在应用服务器的temp目录下,有的时候我们是不能放在该目录下的,需要改变这个默认的存放路径发现jfreechart的ServletUtilities类里有protectedstaticvoidcreateTempDir(){ StringtempDirName=System.getProperty("java。io。tmpdir");//这里我的系统默认的临//时文件路径:C:UsersADMINI~1AppDataLocalTemp
2、 if(tempDirName==null){ thrownewRuntimeException("应用服务器目录下不存在temp目录或该目录无法创建"); } //createthetemporarydirectoryifitdoesn'texist FiletempDir=newFile(tempDirName); if(!tempDir.exists()){ tempDir.mkdirs(); } }该方法创建了默认的图片存放路径在该类的sav
3、eChartAsPNG()和saveChartAsJPEG()里被调用,产生图形,因此我们的思路就是将ServletUtilities的saveChartAsPNG()和saveChartAsJPEG()这2个方法改造成自己定义的方法修改前源文件如下: publicstaticStringsaveChartAsJPEG(JFreeChartchart,intwidth, intheight,ChartRenderingInfoinfo,HttpSessionsession) throwsIOException{
4、 if(chart==null){ thrownewIllegalArgumentException(”Null’chart’argument。”); } //注意,源文件使用了默认路径 ServletUtilities。createTempDir(); Stringprefix=ServletUtilities。tempFilePrefix; if(session==null){ prefix=ServletUtilities.tempOneTimeF
5、ilePrefix; } FiletempFile=File.createTempFile(prefix,".jpeg", 个人收集整理勿做商业用途 newFile(System。getProperty("java。io。tmpdir”))); ChartUtilities。saveChartAsJPEG(tempFile,chart,width,height,info); if(session!=null){ ServletUtilities.registerC
6、hartForDeletion(tempFile,session); } returntempFile。getName();本文为互联网收集,请勿用作商业用途 } 修改后如下: publicstaticStringsaveChartAsJPEG(JFreeChartchart,intwidth, intheight,ChartRenderingInfoinfo,HttpSessionsession) throwsIOException{//从application中读取出临时文件目录,我事先
7、已经在系统启动时,创建了目录 FiletempDr=(File)session.getServletContext()。getAttribute("tempDirectory”); if(chart==null){ thrownewIllegalArgumentException(”chart对象为空”); } UIServletUtilities.createTempDir(); Stringprefix=UIServletU
8、tilit
此文档下载收益归作者所有