本文引自其它文章(具体地方和作者已经遗忘)并根据自己需求修改
Form
部分:
public class MultiUploadForm extends ActionForm {
private List myFiles;
private List myFiles;
private String[] description = new String[100];
public MultiUploadForm(){
myFiles = new ArrayList();
// 为 了能 够 在 页 面初始 显 示一 个 file
myFiles.add(new UploadFile());
}
public List getMyFiles() {
return myFiles;
}
public MultiUploadForm(){
myFiles = new ArrayList();
// 为 了能 够 在 页 面初始 显 示一 个 file
myFiles.add(new UploadFile());
}
public List getMyFiles() {
return myFiles;
}
public String[] getDescription() {
return description;
}
return description;
}
public void setDescription(String[] description) {
this.description = description;
}
public void setDescription(int i ,String description) {
this.description[i] = description;
}
public String getDescription(int i) {
return description[i];
}
this.description = description;
}
public void setDescription(int i ,String description) {
this.description[i] = description;
}
public String getDescription(int i) {
return description[i];
}
//
注意
这个
方法的定
义
public UploadFile getUploadFile(int index){
int size = myFiles.size();
if(index < size){
myFiles.set(index,new UploadFile());
}else{
for(int i = index ;i>size-1;i--){
myFiles.add(new UploadFile());
}
}
int size = myFiles.size();
if(index < size){
myFiles.set(index,new UploadFile());
}else{
for(int i = index ;i>size-1;i--){
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();
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("file is null");
}
else{
//
能
运
行到
这
里,就可以使用
单个
文件上
传
的方法
进
行上
传
了。循
环
而已
System.out.println("filename:::" + file.getFileName());
System.out.println("file size:::" + file.getFileSize());
System.out.println("filename:::" + file.getFileName());
System.out.println("file size:::" + file.getFileSize());
System.out.println("description:::" + file.getDescription());
}
}
}
}
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" %>
<%@ 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">
var fileIndex= 0;
function addRow(){
<head>
<title>
multiUploadDemo
</title>
</head>
<script language="JavaScript">
var fileIndex= 0;
function addRow(){
//得到fileList这个id
var oFileList = document.getElementById("fileList");
//生成tr
var mytr=document.createElement("tr");
//得到整个fileList里的个数
//var fileIndex = oFileList.childNodes.length;
//设置tr的id
mytr.setAttribute("id","tr_" + fileIndex);
var oFileList = document.getElementById("fileList");
//生成tr
var mytr=document.createElement("tr");
//得到整个fileList里的个数
//var fileIndex = oFileList.childNodes.length;
//设置tr的id
mytr.setAttribute("id","tr_" + fileIndex);
//生成一个td
var mytd2=document.createElement("td");
mytd2.setAttribute("width","300");
mytd2.setAttribute("align","center");
var myfile=document.createElement("input");
myfile.setAttribute("type","file");
myfile.setAttribute("size","18");
myfile.setAttribute("name","uploadFile["+ fileIndex + "].file");
myfile.setAttribute("id","file_" + fileIndex);
mytd2.appendChild(document.createTextNode("图片上传: "));
mytd2.appendChild(myfile);
mytr.appendChild(mytd2);
//生成一个td
var mytd1=document.createElement("td");
//设置td宽度
mytd1.setAttribute("width","450");
//td居中
mytd1.setAttribute("align","center");
//生成一个功能块(文本框,按钮)
var mytextfield1=document.createElement("input");
//设置属性
mytextfield1.setAttribute("type","text");
//设置名称
mytextfield1.setAttribute("name","description["+ fileIndex + "]");
//设置长度
mytextfield1.setAttribute("size","48");
//设置id
mytextfield1.setAttribute("id","title_" + fileIndex);
//加文字说明
mytd1.appendChild(document.createTextNode("图片说明: "));
//将功能块加入td中
mytd1.appendChild(mytextfield1);
//将td加入tr中
mytr.appendChild(mytd1);
var mytd3=document.createElement("td");
mytd3.setAttribute("width","50");
mytd3.setAttribute("align","center");
//**如果fuction不传参数就可用以下
//var mybutton=document.createElement("input");
//mybutton.setAttribute("type","button");
//mybutton.setAttribute("value","删除");
//mytd3.appendChild(mybutton);
//mybutton.οnclick=removeFile(fileIndex);
var mytd2=document.createElement("td");
mytd2.setAttribute("width","300");
mytd2.setAttribute("align","center");
var myfile=document.createElement("input");
myfile.setAttribute("type","file");
myfile.setAttribute("size","18");
myfile.setAttribute("name","uploadFile["+ fileIndex + "].file");
myfile.setAttribute("id","file_" + fileIndex);
mytd2.appendChild(document.createTextNode("图片上传: "));
mytd2.appendChild(myfile);
mytr.appendChild(mytd2);
//生成一个td
var mytd1=document.createElement("td");
//设置td宽度
mytd1.setAttribute("width","450");
//td居中
mytd1.setAttribute("align","center");
//生成一个功能块(文本框,按钮)
var mytextfield1=document.createElement("input");
//设置属性
mytextfield1.setAttribute("type","text");
//设置名称
mytextfield1.setAttribute("name","description["+ fileIndex + "]");
//设置长度
mytextfield1.setAttribute("size","48");
//设置id
mytextfield1.setAttribute("id","title_" + fileIndex);
//加文字说明
mytd1.appendChild(document.createTextNode("图片说明: "));
//将功能块加入td中
mytd1.appendChild(mytextfield1);
//将td加入tr中
mytr.appendChild(mytd1);
var mytd3=document.createElement("td");
mytd3.setAttribute("width","50");
mytd3.setAttribute("align","center");
//**如果fuction不传参数就可用以下
//var mybutton=document.createElement("input");
//mybutton.setAttribute("type","button");
//mybutton.setAttribute("value","删除");
//mytd3.appendChild(mybutton);
//mybutton.οnclick=removeFile(fileIndex);
//直接编写语句加入td中
mytd3.innerHTML = "<input type='button' name='button' value='删除' οnclick='removeFile("+fileIndex+")'> ";
mytr.appendChild(mytd3);
oFileList.appendChild(mytr);
mytd3.innerHTML = "<input type='button' name='button' value='删除' οnclick='removeFile("+fileIndex+")'> ";
mytr.appendChild(mytd3);
oFileList.appendChild(mytr);
fileIndex++;
}
}
function removeFile(fileIndex){
//生成一个id里信息
var oTR = document.getElementById("tr_" + fileIndex);
//在tbody里删除id
fileList.removeChild(oTR);
}
</script>
<body>
<html:form method="POST" action="/multiUploadAction.do" enctype="multipart/form-data">
<table width="800" border="1" align="center">
<tr>
<td>
<input type="button" name="button" value="添加"
οnclick="addRow()">
</td>
<td align="center" colspan="2"><html:submit value="提交" property="submit"></html:submit>
<html:reset value="重置" property="reset"></html:reset>
</td>
</tr>
</tbody>
</table>
<table width="800" border="0" align="center">
<TBODY id="fileList"></TBODY>
</table>
</html:form>
//生成一个id里信息
var oTR = document.getElementById("tr_" + fileIndex);
//在tbody里删除id
fileList.removeChild(oTR);
}
</script>
<body>
<html:form method="POST" action="/multiUploadAction.do" enctype="multipart/form-data">
<table width="800" border="1" align="center">
<tr>
<td>
<input type="button" name="button" value="添加"
οnclick="addRow()">
</td>
<td align="center" colspan="2"><html:submit value="提交" property="submit"></html:submit>
<html:reset value="重置" property="reset"></html:reset>
</td>
</tr>
</tbody>
</table>
<table width="800" border="0" align="center">
<TBODY id="fileList"></TBODY>
</table>
</html:form>
</body>
</html:html>
</html:html>
struts-config.xml
部分:
<form-beans>
<form-bean name="multiUploadForm" type="MultiUploadForm" />
</form-beans>
<form-bean name="multiUploadForm" type="MultiUploadForm" />
</form-beans>
<action name="multiUploadForm" path="/multiUploadAction" type="MultiUploadAction" />