主要代码如下
Form部分:
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;
}
}
Dataset部分:
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;
}
}
Action部分:
public class MultiUploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
MultiUploadForm multiUploadForm = (MultiUploadForm) form;
List myFiles = multiUploadForm.getMyFiles();
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());
}
}
return null;
}
}
JSP部分:
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<html:html>
<head>
<title>
multiUploadDemo
</title>
</head>
<script language="javascript" type="">
var num = 0;
function addFile(){
var innerHTML = multiUploadForm.innerHTML;
var signal = "<TD id=tdTarget>";
var signalLen = signal.length;
var targetIndex = innerHTML.indexOf(signal);
var preHTML = innerHTML.substring(0,(targetIndex-6));
var subHTML = innerHTML.substring(targetIndex-6);
num++;
var insertHTML = "<TR><TD><input type=file name=uploadFile["+ num + "].file value=''></TD>";
innerHTML = preHTML + insertHTML + subHTML;
multiUploadForm.innerHTML = innerHTML;
//alert(multiUploadForm.innerHTML);
}
function upload(){
multiUploadForm.submit();
}
</script>
<body bgcolor="#ffffff">
<html:form method="post" action="/multiUploadAction.do" enctype="multipart/form-data">
<table border="1">
<TBODY>
<br><br>
this is file
<nested:iterate id="uploadFile" property="myFiles" name="multiUploadForm" indexId="index">
<nested:nest property="uploadFile">
<nested:file property="file" name="uploadFile" indexed="true"/>
</nested:nest>
</nested:iterate>
<TR>
<input type="button" name="btnAddFile" value="Add File" onclick="addFile()"/>
<TR><TD id="tdTarget"></TD>
<input type="button" name="btnUpload" value="upload" onclick="upload()">
</TBODY>
</table>
</html:form>
</body>
</html:html>
struts-config.xml部分:
<form-beans>
<form-bean name="multiUploadForm" type="MultiUploadForm" />
</form-beans>
<action name="multiUploadForm" path="/multiUploadAction" type="MultiUploadAction" />
posted on 2006-05-08 09:19
happytian 阅读(351)
评论(0) 编辑 收藏