Commons-fileupload Upload Component

Source: Internet
Author: User

Official resources home http://jakarta.apache.org/commons/fileupload/index.html

The fileupload component solves Common File Upload problems. It provides an easy-to-use interface to manage files uploaded to the server and can be used in JSP and Servlet. The fileupload component complies with rfc1867, which analyzes input requests and provides applications with a series of files uploaded to the server. The uploaded file can be stored in the memory or in a temporary location (a parameter indicating the file size can be configured. If the size of the uploaded file exceeds the size specified by this parameter, write the file to a temporary location ). In addition, some parameters are available for configuration, including the acceptable maximum file and the location of temporary files.

The following describes how to use the fileupload component.

First, create an HTML page. Note that the enctype attribute must be set for all forms to upload files, the attribute value must be multipart/form-data, and the request method must be post. In addition to uploading two files, the following form also has a common text input box:

Code content
<Form name = "myform" Action = "fileuploaddemo. jsp" method = "Post" enctype = "multipart/form-Data"> enter your name: <br/>
<Input type = "text" name = "name" size = "15"/> <br/>
Image: <br/>
<Input type = "file" name = "myimage"> <br/>
File: <br/>
<Input type = "file" name = "myfile"> <br/>
<Input type = "Submit" name = "Submit" value = "Submit your files"/>

Next, create the JSP page.

Code content
//...
// ① Check whether the input request is multipart form data.
Boolean ismultipart = fileupload.
Ismultipartcontent (request );
//...
// ② Create a handle for the request and use it to parse the request. Run
// After parsing, all form items are saved in a list.
Diskfileupload upload = new diskfileupload ();
// Parse the request using a handle and save the parsed project to a list.
List items = upload. parserequest (request );
//...
// ③ Obtain the file items in the list in a loop. Distinguished Representation
// File project and common form input project, use isformfield ()
// Method. Based on the request processing requirements, we can save the uploaded file
//, Or one byte to process the file content, or
// Input stream for opening a file.
Iterator itr = items. iterator ();

While (itr. hasnext ()){
Fileitem item = (fileitem) itr. Next ();

// Check whether the current project is a common form element or an uploaded file
If (item. isformfield ()){
// Obtain the form field name
String fieldname = item. getfieldname ();
// If the form field name is name...
If (fieldname. Equals ("name "))
Request. setattribute ("MSG ",
"Thank you:" + item. getstring ());

} Else {
// This project is an uploaded file and saved to the disk.
// Note item. getname ()
// Returns the full path name of the uploaded file on the client. This seems to be a bug.
// To solve this problem, fullfile. getname () is used ().
File fullfile = new file (item. getname ());
File savedfile = new file
(Getservletcontext (). getrealpath ("/"),
Fullfile. getname ());
Item. Write (savedfile );
}
}

We can use Upload. setsizemax of the upload handle to limit the size of the uploaded file. When the size of the uploaded file exceeds the permitted value, the program encounters an exception. In the preceding example, the file size is limited to-1, indicating that files of any size can be uploaded.

There are other slightly changed usage forms. As mentioned above, we can open an input stream on the uploaded file, or let them reside in the memory until the space usage reaches a certain limit value, or obtain the content of the file in the form of a string or byte array based on the judgment of the file type, or directly delete the file. All of this can be done easily by using the method provided by the fileitem class (defaultfileitem is an implementation of fileitem ).

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.