Fileupload Application Guide

Source: Internet
Author: User


This work is licensed using the "knowledge sharing signature"-"non-commercial use"-2.5 mainland China License Agreement in the same way.

 

 

When the client browser specifies that enctype is multipart/form-data to submit a form, the servlet (servlet class or JSP webpage) corresponding to the request on the HTTP server will not be able to pass the getparameter () of the request object () method to obtain the attribute value of the form field. If you are familiar with the HTTP protocol, we can analyze the multipart/form-data stream to obtain useful information. However, this is completely unnecessary. The Apache commons fileupload package has completed this complex task for us.

Basic usage

The fileupload API provides two core processing classes:PortletfileuploadAndServletfileuploadThis document does not describe portletfileupload in detail. The servletfileupload class has a method to determine whether Content-Type is multipart/form-data. At the same time, two methods are provided to read multipart/form-data: Data Stream reading and temporary file reading.

Determine whether it is multipart/form-Data

The static method of the servletfileupload class can be used to determine whether the Content-Type of a request is multipart/form-data. Example:

Boolean ismultipart = <br/> servletfileupload. ismultipartcontent (request );

 

Data Stream reading Method

The servletfileupload object generated by the no-argument constructor uses the data stream reading method. You can use the getitemiterator () method to obtain data in each form field. For example:

// Create a core application object <br/> servletfileupload upload = new servletfileupload (); </P> <p> // get form field iterator <br/> fileitemiterator/* fileitemstream */iter = upload. getitemiterator (request); <br/> while (ITER. hasnext () {<br/> // obtain each form field <br/> fileitemstream item = ITER. next (); </P> <p> // obtain the name of the form field <br/> string name = item. getfieldname (); </P> <p> // obtain the data stream corresponding to the form field. <br/> inputstream stream = item. openstream (); </P> <p> If (item. isformfield () {<br/> // process common form fields </P> <p> // convert data streams into strings, for example: <br/> string value = streams. asstring (stream, "UTF-8 "); <br/>} else {<br/> // process the file form field </P> <p> // obtain the contenttype of the file form field, for example: <br/> string filecontenttype = item. getcontenttype (); </P> <p> // obtain the file name, for example, <br/> string filename = item. getname (); </P> <p> // stream operations in any way <br/> // call method <br/>}< br/>}

 

Data Stream reading is a one-time reading method. On the one hand, the upload object can only perform one-time analysis on the request data. That is to say, the above 5th rows are only valid for the first call. On the other hand, the analysis data can only be read once, and the data does not exist during the second read. That is to say, the fileitemiterator object obtained from the first row can only be traversed once, and data cannot be retrieved when this object is traversed for the second time.

Temporary File Reading Method

The servletfileupload object generated by the parameter constructor uses the temporary file reading method. You can use the parserequest () method to obtain the table field data list. For example:

// Create a single hard disk-based data factory <br/> fileitemfactory factory = new diskfileitemfactory (); </P> <p> // create a core application object based on the factory <br/> servletfileupload upload = new servletfileupload (factory ); </P> <p> // parse request data to generate a single data item list <br/> List <fileitem> items = upload. parserequest (request); </P> <p> // form field iterator <br/> iterator <fileitem> iter = items. iterator (); <br/> while (ITER. hasnext () {<br/> fileitem item = (fileitem) ITER. next (); </P> <p> // obtain the name of the form field <br/> string name = item. getfieldname (); </P> <p> If (item. isformfield () {<br/> // process common form fields </P> <p> // convert data streams into strings, for example: <br/> inputstream stream = item. getinputstream (); <br/> string value = streams. asstring (stream, "UTF-8"); </P> <p> // or use the getstring () method to obtain a string, such as: <br/> // string value = item. getstring ("UTF-8 "); <br/>} else {<br/> // process the file form field </P> <p> // obtain the contenttype of the file form field, for example: <br/> string filecontenttype = item. getcontenttype (); </P> <p> // obtain the file name, for example, <br/> string filename = item. getname (); </P> <p> // operate inputstream in any way <br/> // inputstream = item. getinputstream (); <br/> // call method </P> <p> // modify the content of the temporary file if necessary <br/> // outputstream = item. getoutputstream (); <br/> // call method <br/>}</P> <p>

 

Similarly, the upload object can only perform one-time analysis on the request data. However, in contrast to the data stream reading method, the data parsed in the temporary file reading method is cached in the buffer file, and the cached data can be read multiple times, you can modify the cached content as necessary.

The fileupload API implements the temporary file reading method for the following considerations:

  • If the content to be uploaded is small, it is directly put into the memory for management;
  • Large upload content will be saved to a temporary folder on the disk;
  • Large upload content will be ignored.

By default, fileupload requires the following data sizes:

  • Data smaller than 10 KB is directly stored in the memory for management;
  • Data larger than 10 KB is saved to the temporary system directory (the system temporary directory is the return value of system. getproperty ("Java. Io. tmpdir );
  • All data is not ignored, even if it is very large.

How can I change these default values?

How to Control fileupload

As the core application object, servletfileupload can set the following attributes:

Sizemax
Limit the size of the entire multipart/form-data in bytes. The default value is-1, indicating no limit.
Filesizemax
Limit the size of data in each file form field in bytes. The default value is-1, indicating no limit.
Headerencoding
Specifies the encoding method for each multipart header. The default value is null. If this value is null, The characterencoding value in the request is used. If characterencoding is null, the default character set encoding method is used.

 

An example of setting properties is as follows:

// Create core application objects based on the factory <br/> servletfileupload upload = new servletfileupload (); <br/> upload. setheaderencoding ("UTF-8 ");

 

If fileupload uses the temporary file reading method, you can also set the following attributes for the factory class:

Sizethreshold
Limit the size of the critical bytes in which data in the file form field is saved to the memory. The default value is 10 KB. Data smaller than 10 KB is directly stored in the memory. Data larger than 10 KB is saved in a temporary folder.
Repository
The location of the Temporary Folder. The default value is system. getproperty ("Java. Io. tmpdir.

 

For example, to change the location of the Temporary Folder to "C:/tmp", the method is as follows:

// Create a single hard disk-based data factory <br/> fileitemfactory factory = new diskfileitemfactory (); <br/> factory. setrepository (new file ("C: // tmp "));

 

Attachment 1: fileupload Additional information

Official Website
Http://commons.apache.org/fileupload/
Core jar package
Commons-fileupload-1.2.1.jar
Dependent jar package
Commons-io-1.4.jar

 

Appendix 2: http request data structure

When the client browser uses GBK encoding to submit the following form...

<Form action = "/test/hello.html" method = "Post" <br/> enctype = "multipart/form-Data"> <br/> <input type = "text" name = "name" value = "China"/> <br/> <input type = "file" name = "file"/> <br/> <input type = "Submit "value =" Submit "/> <br/> </form>

 

An HTTP request with the following data content will be sent to the HTTP server.

Post/test/hello.html HTTP/1.1
Referer: http: // localhost: 9000/test/
Accept-language: ZH-CN
Content-Type: multipart/form-data; boundary = --------------------------- 7d91f4b304fc
Accept-encoding: gzip, deflate
HOST: localhost: 9000
Content-Length: 301
Connection: keep-alive
Cache-control: No-Cache
COOKIE: JSESSIONID = 763d1c03a83ae30ccfba88f000065f598

 

----------------------------- 7d91f4b304fc
Content-Disposition: Form-data; name = "name"

 

China
----------------------------- 7d91f4b304fc
Content-Disposition: Form-data; name = "file"; filename = "C:/test.txt"
Content-Type: text/plain

 

This is the content of the test document.
----------------------------- 7d91f4b304fc --

[End]

The header information of each multipart is represented in red; the character set used for encoding is the character set used when the webpage where the form is located is displayed in the browser.
If multipart is a common form field, the content of the name form field in the preceding example is generally a string, and the character set used for string encoding is the same as the header information of multipart.
If multipart is a file form field, the content of the file form field in the preceding example is binary representation of the file data, and the content format is specified by the header information of multipart.

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.