上传页面jsp:
<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("至少保留二行!");
}
}
</script>
</head>
<body>
<center>
<form action="multiupload.action" method="post"
enctype="multipart/form-data">
<table cellpadding="0" cellspacing="0" style="width: 500px;"
id="tab">
<tr>
<td>
<input type="button" value="增加上传文件" />
<input type="button" value="减少" />
<input type="reset" value="重置" />
<input type="submit" value="上传" />
</td>
</tr>
<tr>
<td>
<input type="file" name="file" />
</td>
</tr>
<tr>
<td>
<input type="file" name="file" />
</td>
</tr>
</table>
</form>
</center>
</body>
成功页面:
<body>
<c:forEach items="${fileFileName}" var="file" varStatus="status">
文件上传成功!!!
<hr>
<br />
文件名称:${upload.fileFileName[status.index]}
<br />
文件类型:${upload.fileContentType[status.index]}
<br />
</c:forEach>
</body>
web.xml://配置struts-clean,避免第一次上传取不到文件
<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" class="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>
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; // 文件标题
private File[] file; // 文件域
private String fileContentType[]; // 文件类型
private String[] fileFileName; // 文件名
private String savePath; // 上传地址
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;
}
// 返回保存地址
public String getSavePath() throws Exception {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public String execute() throws Exception {
for (int i = 0; i < file.length; i++) {
// 以服务器的文件保存地址的原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(this.getSavePath()
+ "\\" + this.getFileFileName()[i]);
// 以上传文件建立文件输入流
FileInputStream fis = new FileInputStream(this.getFile()[i]);
// 将上传文件内容写入服务器
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";
}
}