Org. Apache. commons. fileupload method 2

Source: Internet
Author: User

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.

 

  • Cleanup method 1

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.

  • Cleanup method 2

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.

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.