前几天遇到如题目描述问题,要求象163邮箱附件那样的效果。从网上找了半天也没解决,自己断续的尝试了几天才弄出来。为了能帮以后的朋友遇到类似问题的朋友顺利解决问题,特在此描述。
主要代码如下
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" οnclick="addFile()"/>
<TR><TD id="tdTarget"></TD>
<input type="button" name="btnUpload" value="upload" οnclick="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" />
以上只是对动态多文件上传部分进行了描述,只是一个Demo。大家可以根据自己的需求变通的调整一下。
肯定大家有更好的方法,欢迎讨论。