Uploading multiple Files: arrays or collections
One: Upload the file's main page upload.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Upload Files Home page </title>
<body>
<s:fielderror/>
<form action= "Uploadaction" method= "post" enctype= "Multipart/form-data" >
<table border= "1" >
<caption> Uploading Files </caption>
<tr>
<th> Upload title </th>
<td><input type= "text" name= "title" ></td>
</tr>
<tr>
<th> Select File 1:</th>
<td><input type= "file" name= "upload" ></td>
</tr>
<tr>
<th> Select File 2:</th>
<td><input type= "file" name= "upload" ></td>
</tr>
<tr>
<th> Select File 3:</th>
<td><input type= "file" name= "upload" ></td>
</tr>
<tr>
<TD colspan= "2" align= "center" ><input type= "submit" value= "Upload" ></td>
</tr>
</table>
</form>
</body>
Two: Jump to the corresponding action nuc.sw.action---Uploadaction.java
Package nuc.sw.action;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.util.UUID;
Import Com.opensymphony.xwork2.ActionSupport;
public class Uploadaction extends Actionsupport {
Private String title;
Private file[] upload;
Private string[] Uploadfilename;
Private string[] Uploadcontenttype;
Private String Savepath;
Public String GetTitle () {
return title;
}
public void Settitle (String title) {
This.title = title;
}
Public file[] Getupload () {
return upload;
}
public void Setupload (file[] upload) {
This.upload = upload;
}
Public string[] Getuploadfilename () {
return uploadfilename;
}
public void Setuploadfilename (string[] uploadfilename) {
This.uploadfilename = Uploadfilename;
}
Public string[] Getuploadcontenttype () {
return uploadcontenttype;
}
public void Setuploadcontenttype (string[] uploadcontenttype) {
This.uploadcontenttype = Uploadcontenttype;
}
Public String Getsavepath () {
return savepath;
}
public void Setsavepath (String savepath) {
This.savepath = Savepath;
}
@Override
Public String Execute () throws Exception {
System.out.println (title);
System.out.println (upload);
System.out.println (Uploadfilename);
System.out.println (Uploadcontenttype);
System.out.println (Savepath);
for (int i=0;i<upload.length;i++) {
Input stream
FileInputStream fis=new FileInputStream (Upload[i]);
Output stream
Upload two times the same will be successful and display two, so solve the coverage problem
FileOutputStream fos=new FileOutputStream (savepath+ "/" +uuid.randomuuid (). toString () + "_" +uploadfilename[i]);
Bytes to upload
Byte[] Buffer=new byte[1024];
Define length
int len=0;
Starting from 0
while ((Len=fis.read (buffer)) >0) {
Fos.write (buffer, 0, Len);
}
}
Cannot write input/output stream
return SUCCESS;
}
}
Three: Configuration Struts.xml
1: Limit the type size of files (implement file filtering)
2: Set the true path of the uploaded file to save the. tmp file to the Savepath file
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <! DOCTYPE struts Public 3 "-//apache software foundation//dtd struts Configuration 2.3//en" 4 "Http://struts.apache. Org/dtds/struts-2.3.dtd "> 5 6 <struts> 7 <constant name=" Struts.devmode "value=" true "/> 8 <!--settings file Temporary save path--9 <constant name= "Struts.multipart.saveDir" value= "D:/tmpsavedir" ></constant>10 < Package name= "Default" namespace= "/" extends= "Struts-default" >11 <action name= "uploadaction" class= "nuc.sw.act Ion.uploadaction ">12 <!--need to find the source: Struts-default.xml to find the interceptor and then associated source-->13 <interceptor-ref name = "FileUpload" >14 <!--Set the uploaded file type-->15 <!--hide this property, hackers can freely upload the modified suffix name of the virus file (no change in substance)--& gt;16 <param name= "Allowedtypes" >image/bmp,image/png,image/gif,image/jpeg,image/phpeg,text/plain</p Aram>17 <!--Set the size of the uploaded file-->18 <param name= "MaximumSize" >65535</param>19 <!--settings upload file extension-->20 <param name= "Allowedextensions" >.txt</par Am>21 </interceptor-ref>22 <!--reference the default interceptor, you need to use Defaultstack-->23 <interceptor- Ref name= "Defaultstack"/>24 <!--Set the true path of the save-->25 <param name= "Savepath" >d:/uploadrealfile </param>26 <result name= "Success" >/hello.jsp</result>27 <result name= "Input" >/up load.jsp</result>28 </action>29 </package>30 </struts>
IV: Project Structure
V: Running Results
struts2-18-Uploading multiple files