First of all, Struts.xml. If you want to restrict the type of upload file, you can configure an interceptor:
<!--dynamically specify the file types that are allowed to be uploaded by dynamically setting Allowtypes properties-->
<param name= "Allowtypes" >image/bmp,image/png,image/gif,image/jpeg</param>
Single File Upload jsp:
<body>
<center>
<form action= "upload.action" method= "POST"
enctype= "multipart/ Form-data ">
<table cellpadding=" 0 "cellspacing=" 0 "style=" width:350px; " >
<tr>
<td colspan= "2" class= "Getcenter" >
Single File Upload
</td>
</tr>
<tr>
<td>
file title:
</td>
<td>
<input type= "text" name= "title" class= "txt"/>
</td>
</tr>
<tr>
<td>
Select file:
</td>
<td>
<input type= "file" name= "file" class= "file"/>
</td>
</tr>
<tr>
<td colspan= "2" class= "Getcenter" >
<input type= "Submit" value= "Upload"/>
< Input type= "reset" value= "reset"/>
</td>
</tr>
</table>
</form>
</center>
</body>
Single File upload successful interface:
<body>
File Upload successful!!!
Multiple files:
<script type= "Text/javascript" > Function addtr () {var tab = document.getElementById ("tab");
var newrow = Tab.insertrow (tab.rows.length);
Newrow.insertcell (0). innerhtml= "<input type=\" file\ "name=\" file\ "/>";
function Delrow () {var tab = document.getElementById ("tab");
if (tab.rows.length>3) {tab.deleterow (tab.rows.length-1); }else{alert ("Keep at least two lines.")
"); } </script>
Multi-File Successful interface:
<body>
<c:foreach items= "${filefilename}" var= "file" varstatus= "status" >
files uploaded successfully!!!
web.xml://Configure Struts-clean to avoid the first upload without file
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class> org.apache.struts2.dispatcher.actioncontextcleanup</filter-class>
</filter>
< filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</ Url-pattern>
</filter-mapping>
Struts.xml
<struts> <constant name= "struts.i18n.encoding" value= "Utf-8" ></constant> <package name= " Default "namespace="/"extends=" Struts-default "> <action name=" Product "class=" Com.tudou.struts.action.ProductAction "> </action> <action name=" upload "class=" Com.tudou.struts.action.FileUpLoadAction "> <param name=" Savepath ">/uploadfiles</param> <result Name= "Success" >/fileSuccess.jsp</result> </action> <action name= "Multiupload" Com.tudou.struts.action.MultiFileUpLoadAction "> <param name=" Savepath ">/uploadfiles</param> < Result name= "Success" >/success.jsp</result> </action> </package> <package name= "MyPackage"
Namespace= "/" extends= "Struts-default" > <action name= "Login" class= "Com.tudou.struts.action.LoginAction" > <result name= "Input" >/login.jsp</result> <result name= "index" >/index.jsp</result> </ Action> </package>
Single File Action:
Package com.tudou.struts.action;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionContext; public class Fileuploadaction {private string title;//File title private files file;//domain private string filecontenttype ; File type private String filefilename; filename private String Savepath;
Upload address public void Setsavepath (String savepath) {this.savepath = Savepath;
Public String GetTitle () {return title;
public void Settitle (String title) {this.title = title;
Public file GetFile () {return file;
public void Setfile (file file) {this.file = file;
Public String Getfilecontenttype () {return filecontenttype;
} public void Setfilecontenttype (String filecontenttype) {this.filecontenttype = Filecontenttype;
Public String Getfilefilename () {return filefilename; public void Setfilefilename (String filefilename) {THis.filefilename = Filefilename; //Returns the Save Address public String Getsavepath () throws Exception {return servletactioncontext.getrequest (). Getrealpath (Savep
ATH); The public String execute () throws Exception {//the original filename of the server's file save address to build the upload file output stream FileOutputStream fos = new Fileoutputstrea
M (This.getsavepath () + "\" + this.getfilefilename ());
To upload files to create a file input stream FileInputStream fis = new FileInputStream (This.getfile ());
Writes the uploaded file content to the server byte[] b = new byte[1024];
int len = 0;
while (len = Fis.read (b)) > 0) {fos.write (b, 0, Len);
Actioncontext.getcontext (). put ("Upload", this);
Return "Success";
}
}
Multi-File Upload action:
Package com.tudou.struts.action;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionContext; public class Multifileuploadaction {//private string[] title;//File title private file[] file;//file domain private String file Contenttype[]; File type private string[] filefilename; filename private String Savepath;
Upload address public void Setsavepath (String savepath) {this.savepath = Savepath;
}//Public string[] GetTitle () {//return title;
}////public void Settitle (string[] title) {//this.title = title;
Public file[] GetFile () {return File;
public void Setfile (file[] file) {this.file = File;
String[] Getfilecontenttype () {return filecontenttype;
} public void Setfilecontenttype (string[] filecontenttype) {this.filecontenttype = Filecontenttype;
String[] Getfilefilename () {return filefilename; } public void Setfilefilename (string[] filefilename) {this.filefilename = Filefilename; //Returns the Save Address public String Getsavepath () throws Exception {return servletactioncontext.getrequest (). Getrealpath (Savep
ATH); Public String Execute () throws Exception {for (int i = 0; i < file.length i++) {//Set up upload file output with the original filename of the server's file save address
Stream FileOutputStream fos = new FileOutputStream (This.getsavepath () + "\" + this.getfilefilename () [i]);
Create file input stream FileInputStream fis = new FileInputStream (This.getfile () [i]) by uploading files;
Writes the uploaded file content to the server byte[] b = new byte[1024];
int len = 0;
while (len = Fis.read (b)) > 0) {fos.write (b, 0, Len);
} actioncontext.getcontext (). put ("Upload", this);
Return "Success";
}
}