Struts1.X 上传多个文件

一开始没有引入

commons-io-1.4.jar

commons-fileupload-1.2.2.jar

报错

 

java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException

 

-------------------------------------------------------------------------------------------------------------------------------

Jsp:

<form action="/uploadAll.do" method="post" enctype="multipart/form-data" >
     <input type="file" name="f1">
     <input type="file" name="f2">
     <input type="file" name="f3">

 
<--特别注意,这里多个input:file的name属性必须有并各不相同-->

......
</form>

Action:

public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
MultipartRequestHandler MRH = form.getMultipartRequestHandler();
Hashtable fes = MRH.getFileElements();
Collection values = fes.values();
for (java.util.Iterator i = values.iterator(); i.hasNext();) {
    FormFile file = (FormFile) i.next();
    InputStream inStream = file.getInputStream();
    OutputStream outStream = new FileOutputStream("D:\\xxx.x");
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
             outStream.write(buffer, 0, bytesRead);
    }
    inStream.close();
    outStream.close();
    file.destory();
}
......
}

 
这里只列出关键代码,Action的配置等和其它并无不同,就不多说了。还有FileForm的其它一些方法大家自己去看就好。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/oobug/archive/2007/07/12/1686439.aspx

你可能感兴趣的:(apache,jsp,.net,Blog)