Jfreechart cannot automatically delete the generated image

Source: Internet
Author: User

In many cases, jfreechart cannot automatically delete the generated image as expected when the httpsession fails.

Analyze the original chartdeleter code

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;

}

The reason is that the path of the normally generated image is not always system. getproperty ("Java. Io. tmpdir ").

Solution:

Modify the original chartdeleter. Java file as follows:

...

Public chartdeleter (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;

}

...

 

Modify servletutilities. Java as follows:

...

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 ();

}

...

The image is stored in the root directory of the project.

In fact, the more appropriate way is not to modify the source file, through inheritance, override method is a better choice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.