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 ";
}}