When a file item is encapsulated as a fileitem object, if the file size exceeds the critical value, it will be written to a temporary file.
When these temporary files are no longer used (if the corresponding Java. io. file is recyclable, which is better) will be automatically deleted. this will be. apache. commons. io. A harvest thread started by an instance of filecleaningtracker is silently executed.
In a web application, resource cleanup is controlled by an instance of javax. servlet. servletcontextlistener. In other environments, similar ideas must be applicable.
Filecleanercleanup class
The Web application should use an instance of org. Apache. commons. fileupload. filecleanercleanup, as long as it is stored in Web. xml:
<Web-app>
...
<Listener>
<Listener-class>
Org. Apache. commons. fileupload. servlet. filecleanercleanup
</Listener-class>
</Listener>
...
</Web-app>
Unfortunately, things are not finished yet. If you are in the same situation as below, you only need to follow the above steps to clear the resources.
You are using commons-io 1.3 or later.
You load commons-io from the web-INF/lib of the Web application, not from other locations, such as Tomcat's common/lib.
If commons-io 1.3 is loaded from the classpath of your Web Container, the following situations may occur:
We recommend that you run two applications, A and B. (The two applications may be identical, but the context names are different .) Both applications use filecleanercleanup. Now, if you terminate application a and application B, then application a terminates the harvester thread of application B. In other words, you should carefully consider whether to use filecleanercleanup or not.
Create a diskfileitemfactory
Filecleanercleanup provides an org. apache. commons. io. filecleaningtracker instance. this instance must be created in org. apache. commons. fileupload. disk. diskfileitemfactory is used. this should be achieved by calling the following method:
Filecleaningtracker = filecleanercleanup. getfilecleaningtracker (servletcontext );
Diskfileitemfactory factory = new diskfileitemfactory ();
Factory. setfilecleaningtracker (filecleaningtracker );
Filecleanercleanup provides
Org. Apache. commons. Io. filecleaningtracker object. After constructing diskfileitemfactory, you need to set
The filecleaningtracker object used to trace the temporary files generated. If you do not want to track temporary files, Set
Filecleaningtracker is null.