Java EE Development of various resources download list, the history of the most IT resources, personal collection summary.
1. Single File Upload:
Struts-fileupload.xml
<struts>
<constant name= "Struts.configuration.xml.reload" value= "true" ></constant>
<!--limit the total number of uploads to 10M, the default is 2M
throws org.apache.commons.fileupload.fileuploadbase$ when a single file or file array and size is greater than 2Mb Sizelimitexceededexception: The
request was rejected because its size (2667603) exceeds the configured maximum (209715 2)--
<constant name= "struts.multipart.maxSize" value= "10701096" ></constant>
<package name= "Upload" namespace= "/upload" extends= "Struts-default" >
<action name= "one" class= " Edu.action.OneFileUploadAction "method=" Singlefile ">
<result name=" Onemessage ">/page/ uploadmessage.jsp</result>
</action>
<action name= "many" class= " Edu.action.ManyFileUpload "method=" Manyfile ">
<result name=" Manymessage ">/page/manyfilemessage.jsp </result>
</action>
</package>
</struts>
Onefileuploadaction.java
Package edu.action;
Import Java.io.File;
Import java.io.IOException;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionContext;
public class Onefileuploadaction {private String userName;
Private File image;
Private String Imagefilename;
Private String Imagecontenttype; public string Singlefile () throws IOException {//Save the file under/image/, get the true path of the file string realpath = Servletactioncontext.g
Etservletcontext (). Getrealpath ("/image");
if (image!=null) {//Get uploaded file path ' ImageFile = ' new file ' (new file (Realpath), imagefilename); if (!imagefile.getparentfile (). exists ()) {Imagefile.getparentfile (). mkdirs ();//contains multi-level directory creation}//struts before executing the next sentence, the upload
Files into the temporary directory, after the execution of the following sentence, the temporary files are copied into the real directory, will remove the temporary file Fileutils.copyfile (image, ImageFile);
Actioncontext.getcontext (). Put ("message", "Upload succeeded!"); Test binary data under the UserName property is garbled System.out.println ("uploader:" +username+ ", FileName:" +imagefilename+ ", File type:" +imagecontenttype+", Status: Upload Success");
} return "Onemessage";
} public String GetUserName () {return userName;
} public void Setusername (String userName) {this.username = UserName;
} public String Getimagefilename () {return imagefilename;
}//File name definition format "file name +filename" public void Setimagefilename (String imagefilename) {this.imagefilename = Imagefilename;
}//With the form <input type= "file" name= "image" > property name same as public void setimage (file image) {this.image = image; }//file type name definition format "file name +contenttype" public void Setimagecontenttype (String imagecontenttype) {This.imagecontenttype = i
Magecontenttype;
}
}
After uploading successfully, console print: uploader: Xiao Hua, file name: 2012_03130103.jpg, File type: image/jpeg, Status: Upload successful
onefileupload.jsp
<body>
<form action= "${pagecontext.request.contextpath}/upload/one" enctype= "Multipart/form-data" Method= "POST" >
name: <input type= "text" name= "userName" ><br/>
photos: <input type= "file" Name= " Image "><br/>
<input type=" Submit "value=" Regist ">
</form>
</body>
uploadmessage.jsp
<body>
${username}:${imagefilename}:${message}
</body>
Summary:
The first step: Add Commons-fileupload-1.2.1.jar, Commons-io-1.3.2.jar under Web-inf/lib. These two files can be downloaded from http://commons.apache.org/.
The second step: Set the form table Enctype to: "Multipart/form-data", as follows: <form action= "${pagecontext.request.contextpath}/upload/one" C0>enctype= "Multipart/form-data" method= "POST">
Name: <input type= "text" name= "UserName" ><br/>
Photo: <input type= "file" name= "image" ><br/>
<input type= "Submit" value= "Regist" >
</form>
Step Three: Add the following attribute to the action class, the red part of the attribute corresponds to the name of the file field in the form, and the blue part of the property is a fixed format:
public class helloworldaction{
Private file uploadimage;//to get uploaded files
Private String uploadimagecontenttype;//Gets the type of file
Private String uploadimagefilename;//Gets the name of the file
The Getter/setter method of the property is slightly saved here, and after the setter method is provided, Struts2 automatically calls the Setter method to assign a value
}
Note : (1) Struts default one-time maximum file upload size is 2Mb, so if the sum of a single file or file array is larger than 2Mb, it will be thrown: o Rg.apache.commons.fileupload.fileuploadbase$sizelimitexceededexception:the request was rejected because its size ( 2667603) exceeds the configured maximum (2097152)
Therefore, you should configure the constant control file upload maximum size in the Struts.xml file.
<constant name= "struts.multipart.maxSize" value= "10701096" ></constant>
(2) If a file with the same name exists under the/image file, the latter overwrites the former without reporting any errors.
(3) Struts2 on the form of Chinese garbled processing is very good, and enctype= "Multipart/form-data" form of the character processing is very good, without the programmer manual processing.
2. Multi-File Upload: Manyfileuploadaction.java
Manyfileuploadaction.java
Package edu.action;
Import Java.io.File;
Import java.io.IOException;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionContext; public class Manyfileuploadaction { private String userName; private file[] image; /Here you can use list or array save private string[] imagefilename;//Here you can use list or array to save public String Manyfile () throws IOException { //Save the file under/image/, get the true path of the file & nbsp
string Realpath = Servletactioncontext.getservletcontext (). Getrealpath ("/image"); if (image!=null) { //get uploaded files directory file
Imagedir = new File (Realpath); if (!imagedir.exists ()) { imagedir.mkdirs (); Includes multi-level catalog creation } for (int i = 0; i < image.length; i++) { &n Bsp
file imagefile = new File (Imagedir,imagefilename[i]); fileutils.copyfile (Image[i], ImageFile)
; }
Actioncontext.getcontext (). Put ("Count", image.length);
} return "Manymessage"; } public String getusername () { return userName; &n bsp; } public void Setusername (String userName) { this.username = UserName; } public string[] Getimagefilename () {
return imagefilename; } public void Setimagefilename (string[] imagefilename) {
this.imagefilename = imagefilename; } public void SetImage (file[] image) {
This.image = image;
}}
manyfileupload.jsp
<body>
<form action= "${pagecontext.request.contextpath}/upload/many" enctype= "Multipart/form-data" Method= "POST" >
name: <input type= "text" name= "UserName" ><br/>
photo 1:<input type= "file" Name= " Image "><br/>
photos 2:<input type=" file "name=" image "><br/>
photos 3:<input type=" file "name = "image" ><br/>
<input type= "Submit" value= "Regist" >
</form>
</body>
manyfilemessage.jsp
<body>
user name: ${username}, number of photos:${count}<br>
<c:foreach var= "ImageName" items= "${ Imagefilename} ">
${imagename}:<br/>
</c:forEach>
</body>
Browser:
Click the regist button:
Here for
If the file name contains Chinese, it cannot be displayed and does not know why:
Summarize:
(1) For multi-file uploads, the Name property of the File text box must be the same, multiple files in the action can be saved with the array and list collection.
(2) File text box without uploading the file, Struts2 will not pass it to struts action, from the above length=2 can be seen.