Struts2 framework-------File upload mechanism

Source: Internet
Author: User
Tags file upload
Uploading files is a feature of many Web applications Struts2 is the implementation of file uploads through the Org.apache.commons.fileupload.FileUpload class. FileUpload by saving the HTTP data to a temporary folder, and struts uses the FileUpload interceptor to bind the file to an instance of the action. This allows us to upload files to the browser in a local file mode. An interceptor is provided in the STRUTS2 to assist in the operation of uploading the file, which is not configured and is automatically loaded

To implement a file upload, the following two jars are required in addition to the STRUTS2 basic jar file:



Upload a single file using normal action but in order to get some information to upload files, such as uploading file names, uploading file types, and uploading a stream object, you need to add some getter and setter methods to the action class according to certain rules. Struts2 uploads the file to a temporary path (the default is the value of Javax.servlet.context.tempdir) and uses Java.io.File to open the temporary file; In the Execute method of the action, copy only from the temporary file to the specified path
File Upload steps
First step: Add Commons-fileupload-1.2.1.jar, Commons-io-1.3.2.jar under Web-inf/lib
The second step: Set the form table Enctype to: "Multipart/form-data", as follows:
<form enctype= "Multipart/form-data" action= "${pagecontext.request.contextpath}/xxx.action" method= "POST" >
<input type= "File" name= "Uploadimage" > <!-can also be labeled with struts ' s:file tags-
</form>
This form type indicates that the form will pass the data in a binary stream
Step three: Write the action class. Create a file, FileName, contenttype three member variables, and generate getter and setter. Note the naming problem for three key member variables.



Uploading a file action three member variable naming rules <s:file/> tags or input tags are not just bound to the file type member variable, but also bound to ContentType (the mime of the uploaded file Type) and filename (the filename of the uploaded file) Therefore, the <s:file name= "xxx"/> corresponds to the three attributes of XXX, xxxcontenttype, and xxxfilename inside the action class.
Multiple file uploads
First step: Add Commons-fileupload-1.2.1.jar, Commons-io-1.3.2.jar under Web-inf/lib

The second step: Set the form table's enctype to: "Multipart/form-data", while writing multiple input or s:file tags, all tags name consistent, as follows:
<form enctype= "Multipart/form-data" action= "${pagecontext.request.contextpath}/xxx.action" method= "POST" >
<input type= "File" name= "UploadImages" >
<input type= "File" name= "UploadImages" >
</form>

Step three: Write the action class, and change the three key attribute types to a collection or array



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.