Web development is very convenient based on struts2, and can also easily implement the FileUpload function, here demonstrated struts2 in the case of multiple file form upload operation
Struts.xml Action Definition
<action name= "Douploadmultfile" class= "cn.upload1.UploadAction" method= "Upload" > <result "name="
Success "Type=" JSON >
<param name= "root" >ActionResult</param>
</result>
</ Action>
jquery Ajax Support
<script type= "Text/javascript" src= "script/jquery.min.js" ></script>
<script type= "text/" JavaScript "src=" Script/jquery.form.js ></script>
form for JSP upload file
<s:form id= "UploadExce2" action= "Douploadmultfile" method= "POST" enctype= "Multipart/form-data" >
<s: File label= "file (1)" name= "Upload"/>
<s:file label= "file (2)" name= "Upload"/> <s:file label=
" FIle (3) "name=" Upload "/>
<s:submit cssclass=" btn btn-primary "/>
</s:form>
<script Type= "Text/javascript" >
$ (' #uploadExce2 '). Ajaxform ({
dataType: ' json ',
success:function (data)
{
//alert (data);
},
error:function (data)
{
alert (' Error ');
}
});
</script>
Note the name of the file control Uload, which is the STRUTS2 's contract name and cannot be changed.
Struts2 UploadFile
Package cn.test.upload1;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.rmi.RemoteException;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Java.util.UUID;
Import Javax.servlet.ServletContext;
Import org.apache.commons.codec.binary.Base64;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.struts2.ServletActionContext;
Import com.opensymphony.xwork2.Action;
Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;
Import Cn.sharcom.config.ChiliLogin;
Import cn.sharcom.config.ChiliProduct;
Import cn.sharcom.config.Constant;
public class Uploadaction {private file[] uploads;
Private string[] uploadfilenames;
Private string[] uploadcontenttypes;
Private String Stractionresult;
Public uploadaction () {} public file[] Getupload () {return this.uploads;
} public void Setupload (file[] upload) {this.uploads = upload;
String[] Getuploadfilename () {return this.uploadfilenames;
} public void Setuploadfilename (string[] uploadfilename) {this.uploadfilenames = Uploadfilename;
String[] Getuploadcontenttype () {return this.uploadcontenttypes;
} public void Setuploadcontenttype (string[] uploadcontenttype) {this.uploadcontenttypes = Uploadcontenttype;
Public String Upload () {Actioncontext AC = Actioncontext.getcontext ();
ServletContext sc = (ServletContext) ac.get (Servletactioncontext.servlet_context);
A directory String Realpath = Sc.getrealpath ("/") for a stored file;
Get the actual number of uploaded files int length = Uploads.length; All downloaded files for (int i = 0; i < length; i++) {//file-Write Magnetic try {//new file name String
strFileName = NewFileName (i);
File SaveFile = new file (new file (Realpath), strFileName); Write to FilEutils.copyfile (Uploads[i], savefile);
catch (IOException e) {e.printstacktrace ();
} return action.success; /** * Returns action result * to form @return/public String Getactionresult () {return Stractionresul
T
/** * Constructs a new filename * * @return/private string newfilename (int index) {string strresult = null;
UUID uuid = Uuid.randomuuid ();
String str1 = uuid.tostring ();
String prefix = uploadfilenames[index].substring (Uploadfilenames[index].lastindexof ("."));
strresult = str1 + prefix;
return strresult; /** * Convert File to Base64 * * @param strfilename * @return */private String apachebase64filetostring (Fin
Al string strFileName) {string strresult = null;
try {//Open file filename = new strFileName;
Get file byte byte[] bytefile = org.apache.commons.io.FileUtils.readFileToByteArray (file);
Start Base64 Base64 base64 = new Base64 ();
Get base64 byte byte[] bytetext = Base64.encode (bytefile);
Dump String strresult = new string (bytetext);
Stringtofile ("C:/base64_2.txt", strresult);
Set return} catch (IOException e) {e.printstacktrace ();
return strresult;
}
}
Text Connection blog.csdn.net/joyous/article/details/51103064
Reference https://struts.apache.org/docs/file-upload.html
Q Group Discussion 236201801