Struts2 Study Notes (10): File Upload

Source: Internet
Author: User
Step 1: WEB-INF/lib Add below Commons-fileupload-1.2.1.jar , Commons-io-1.3.2.jar . These two files can be Http://commons.apache.org/ Download. Step 2: Form Table Enctype Set: " Multipart/form-Data " , As follows: <Form enctype = "multipart/form-Data" Action = "$ {pagecontext. request. contextpath}/xxx. action "method =" Post "> <input type =" file "name =" Uploadimage "> </Form> Step 3: Action Class to add the following attributes :
 Public   Class Helloworldaction {
Private File uploadimage; // Get the uploaded file
Private String uploadimagecontenttype; // Obtain the file type.
Private String uploadimagefilename;// Get the file name
// The getter/setter method of the attribute is omitted here.
Public String upload () Throws Exception {
String realpath = servletactioncontext. getservletcontext (). getrealpath ("/images ");
File file = New File (realpath );
If (! File. exists () file. mkdirs ();
Fileutils. copyfile (uploadimage, New File (file, uploadimagefilename ));
Return "Success ";
}
}
Multi-File Upload Step 1: WEB-INF/lib Add below Commons-fileupload-1.2.1.jar , Commons-io-1.3.2.jar . These two files can be Http://commons.apache.org/ Download.
Step 2: Form Table Enctype Set: " Multipart/form-Data " , 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 3: Action Class, The red part of the attribute corresponds to the name of the file field in the form. :
 Public   Class Helloworldaction {
Private File [] uploadimages;// Get the uploaded file
Private String [] uploadimagescontenttype; // Obtain the file type.
Private String [] uploadimagesfilename; // Get the file name
// The getter/setter method of the attribute is omitted here.
Public String upload () Throws Exception {
String realpath = servletactioncontext. getservletcontext (). getrealpath ("/images ");
File file = New File (realpath );
If (! File. exists () file. mkdirs ();
For ( Int I = 0; I <uploadimages. length; I ++) {file uploadimage = uploadimages [I];
Fileutils. copyfile (uploadimage, New File (file, uploadimagesfilename [I]);
}
Return "Success ";
}}

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.