Struts实现自动多文件上传

本文引自其它文章(具体地方和作者已经遗忘)并根据自己需求修改

Form 部分:
public class MultiUploadForm extends ActionForm {
    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 String[] getDescription() {
  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];
 } 
  // 注意 这个 方法的定
   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());
      }
  }

        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());
                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" %>
<html:html>
<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);
  //生成一个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);
 //直接编写语句加入td中
 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>&nbsp;
       <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>
 struts-config.xml 部分:
  <form-beans>
    <form-bean name="multiUploadForm" type="MultiUploadForm" />
  </form-beans>
<action name="multiUploadForm" path="/multiUploadAction" type="MultiUploadAction" />  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值