index.jsp:
<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<title>
multiUploadDemo
</title>
</head>
<script language="javascript">
var num = 0;
function upload(){
document.getElementById("status").innerHTML = "文件上传中...";
multiUploadForm.submit();
}
function additem(id)
{
var row,cell,str;
row = eval("document.all["+'"'+id+'"'+"]").insertRow();
if(row != null )
{
cell = row.insertCell();
str="<input type="+'"'+"file"+'"'+" name=uploadFile["+ num +"].file><input type="+'"'+"button"+'"'+" value="+'"'+"删除"+'"'+" onclick='deleteitem(this,"+'"'+"tb"+'"'+");'>"
cell.innerHTML=str;
}
num++;
}
function deleteitem(obj,id)
{
var rowNum,curRow;
curRow = obj.parentNode.parentNode;
rowNum = eval("document.all."+id).rows.length - 1;
eval("document.all["+'"'+id+'"'+"]").deleteRow(curRow.rowIndex);
}
function callback(msg)
{
document.getElementById("status").innerHTML = "文件上传完成...<br>" + msg;
}
</script>
<body bgcolor="#ffffff">
<div id="status"></div>
<html:form method="post" action="/multiUpload.do" enctype="multipart/form-data" target="hidden_frame">
<table id="tb">
</table>
</html:form>
<iframe name='hidden_frame' id="hidden_frame" style="display:none"></iframe>
<input type="button" name="btnAddFile" value="Add File" onclick="additem('tb')"/>
<input type="button" name="btnUpload" value="upload" onclick="upload()"/>
</body>
</html:html>
Struts config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="multiUploadForm" type="com.yourcompany.struts.MultiUploadForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="multiUploadForm"
name="multiUploadForm"
path="/multiUpload"
scope="request"
type="com.yourcompany.struts.MultiUploadAction"
validate="false" />
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
Form bean: MultiUploadForm.java
package com.yourcompany.struts;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.action.ActionForm;
public class MultiUploadForm extends ActionForm {
private List myFiles;
public MultiUploadForm(){
myFiles = new ArrayList();
//为了能够在页面初始显示一个file
myFiles.add(new UploadFile());
}
public List getMyFiles() {
return myFiles;
}
//注意这个方法的定义
public UploadFile getUploadFile(int index){
int size = myFiles.size();
if(index>size-1){
myFiles.add(new UploadFile());
}
return (UploadFile)myFiles.get(index);
}
public void setMyFiles(List myFiles) {
this.myFiles = myFiles;
}
}
ActionBean :MultiUploadAction.java
package com.yourcompany.struts;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class MultiUploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
MultiUploadForm multiUploadForm = (MultiUploadForm) form;
List myFiles = multiUploadForm.getMyFiles();
String fileStr = "";
for (int i = 0; i < myFiles.size(); i++) {
UploadFile uploadFile = (UploadFile) myFiles.get(i);
FormFile file = uploadFile.getFile();
if (file == null) {
System.out.println("file is null");
} else {
// 能运行到这里,就可以使用单个文件上传的方法进行上传了。循环而已
System.out.println("filename:::" + file.getFileName());
System.out.println("file size:::" + file.getFileSize());
fileStr += "filename:::" + file.getFileName() + "file size:::" + file.getFileSize() ;
}
}
//返回文本
try
{
response.setHeader("ContentType", "text/html;charset=gbk");
PrintWriter out = response.getWriter();
out.write("<script>parent.callback('upload file success" + fileStr + "')</script>");
}catch(IOException e)
{
e.printStackTrace();
}
return null;
}
}
Bean: UploadFile.java
package com.yourcompany.struts;
import java.io.Serializable;
import org.apache.struts.upload.FormFile;
public class UploadFile implements Serializable {
private FormFile file;
public FormFile getFile() {
System.out.println("run in uploadFile.getFile()");
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}