Commons-fileupload source code learning experience

Source: Internet
Author: User

Commons-fileupload depends on the commons-io package.

How to Use commons-fileupload:

1. Create a file project factory class diskfileitemfactory.

Diskfileitemfactory has two constructor methods:

1 diskfileitemfactory () in which sizethreshold is the default value of 10 KB. The file size cannot exceed this value to save the content in the memory. If this value is exceeded, the file is saved to the temporary directory. system is available. getproperty ("Java. io. tmpdir ") Get; 2 diskfileitemfactory (INT sizethreshold, File Repository) can specify sizethreshold, and the path where the file is saved to the disk.

Diskfileitemfactory has a filecleaningtracker attribute that can be used to track the deletion of temporary files. When this temporary file is no longer used, it will be deleted immediately. More accurately, when the file object is recycled by the garbage collector, filecleaningtracker will start the reaper thread) this temporary file is automatically deleted. Filecleaningtracker is a tool class of the commons-io package.

1 filecleaningtracker = filecleanercleanup. getfilecleaningtracker (servletcontext); 2 diskfileitemfactory factory = new diskfileitemfactory (); 3 factory. setfilecleaningtracker (filecleaningtracker); 4 servletcontext: 5 javax. servlet. HTTP. httpsession. getservletcontext (); 6 javax. servlet. JSP. pagecontext. getservletcontext (); 7 javax. servlet. servletconfig. getservletcontext ();

2. Create a file processing class servletfileupload.

Servletfileupload parses the request information of the upload request and encapsulates it into the fileitem class. We can use fileitem to obtain the file name, size, file stream, and other information.

1 servletfileupload SFU = new servletfileupload (factory); 2 servletfileupload can be set: 3 headerencoding: the maximum size of the total size of the files that can be uploaded by a single request when reading request header information, the default value is-1, and the maximum size of a single file that can be uploaded by a single request is not limited. The default value is-1, and the size is 6 list <fileitem> items = SFU. parserequest (req );

 

The process of parsing the upload request information by servletfileupload:

The HTML code for file upload is as follows:

1 <form action="http://server.dom/cgi/handle" enctype="multipart/form-data" method=POST>2   What is your name? <input type=text name=submitter/>3   What files are you sending? <input type=file name=pics/>4 </form>

The data format transmitted by the browser is as follows:

 1    Content-type: multipart/form-data, boundary=AaB03x 2  3   --AaB03x 4  5   content-disposition: form-data; name="field1" 6  7   Joe Blow 8  9   --AaB03x10 11   content-disposition: form-data; name="pics"; filename="file1.txt"12 13   Content-Type: text/plain14 15   ... contents of file1.txt ...16 17   --AaB03x--

 

 

 

  

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.