Struts upload组件同一个页面中多文件上传

jsp代码如下:

  <html:form method="post" action="/upload.do" enctype="multipart/form-data">
   <table width="75%" border="1" align="center">
    <tr>
     <td>
      <div align="center">
       1、
       <html:file property="file1" size="30"/>
      </div>
     </td>
    </tr>
    <tr>
     <td>
      <div align="center">
       2、
       <html:file property="file2" size="30"/>
      </div>
     </td>
    </tr>
    <tr>
     <td>
      <div align="center">
       3、
       <html:file property="file3" size="30"/>
      </div>
     </td>
    </tr>
    <tr>
     <td>
      <div align="center">
       4、
       <html:file property="file4" size="30"/>
      </div>
     </td>
    </tr>
    <tr>
     <td>
      <div align="center">
       <html:submit>上传它!</html:submit>
      </div>
     </td>
    </tr>
   </table>
  </html:form>
  
struts配置文件中配置动态ActionForm如下:
    <form-bean name="uploadForm" type="org.apache.struts.action.DynaActionForm">
      <form-property name="file3" type="org.apache.struts.upload.FormFile" />
      <form-property name="file1" type="org.apache.struts.upload.FormFile" />
      <form-property name="file4" type="org.apache.struts.upload.FormFile" />
      <form-property name="file2" type="org.apache.struts.upload.FormFile" />
    </form-bean>

Action中处理上传的代码如下:
  DynaActionForm upForm = (DynaActionForm) form;
  String dir = this.getServlet().getServletContext().getRealPath("/upload/uploaded");
  FormFile fileOne = (FormFile)upForm.get("file1");
  FormFile fileTwo = (FormFile)upForm.get("file2");
  FormFile fileThree = (FormFile)upForm.get("file3");
  FormFile fileFour = (FormFile)upForm.get("file4");
  FormFile[] files = new FormFile[]{fileOne, fileTwo, fileThree, fileFour};
  List fileList = new ArrayList();
  for(int i = 0;i<files.length;i++){
   if(files[i] == null)
    continue;
   String fileName = files[i].getFileName();
   int size = files[i].getFileSize();
   try {
    InputStream streamIn = files[i].getInputStream();
    OutputStream streamOut = new FileOutputStream(dir + "/" + fileName);
    int bytesRead = 0;
    byte[] buffer = new byte[4096];
    while((bytesRead = streamIn.read(buffer, 0, 4096)) != -1)
     streamOut.write(buffer, 0, bytesRead);
    streamOut.close();
    streamIn.close();
   } catch (FileNotFoundException e) {
    e.printStackTrace();
    ActionMessages msgs = new ActionMessages();
    ActionMessage msg = new ActionMessage("error.upload.fail");
    msgs.add("error.upload.fail",msg);
    request.setAttribute(Globals.MESSAGE_KEY,msgs);
   } catch (IOException e) {
    e.printStackTrace();
    ActionMessages msgs = new ActionMessages();
    ActionMessage msg = new ActionMessage("error.upload.fail");
    msgs.add("error.upload.fail",msg);
    request.setAttribute(Globals.MESSAGE_KEY,msgs);
   }
   fileList.add(fileName);
   ActionMessages msgs = new ActionMessages();
   ActionMessage msg = new ActionMessage("error.upload.success");
   msgs.add("error.upload.success",msg);
   request.setAttribute(Globals.MESSAGE_KEY,msgs);
  }

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值