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.