Apache Commons FileUpload Does not behave like expected

Posted by Harry Pham on Stack Overflow See other posts from Stack Overflow or by Harry Pham
Published on 2010-03-12T19:34:42Z Indexed on 2010/03/12 19:37 UTC
Read the original article Hit count: 644

Filed under:
|
|
|

I just want to assure you guys that before I post this, I have read couple of similar post but none solved my problem. So here it is. I use Ajax taglib for my form since it wont refresh my screen

<%@ taglib uri="WEB-INF/taglib.tld" prefix="a" %> 
<a:AjaxUpload action="UploadServlet"> 
     <input type="file"> 
     <input type="submit" value="Upload"> 
</a:AjaxUpload>

If this is in any confused you guys, then you can think of the above code will create these form, but send request asynchronously.

    <form action="UploadServlet" enctype="multipart/form-data" method="post">
         <input type="file">
         <input type="submit">
    </form>  

My first question is, if I add name="file" inside the <input> to make it become like this, <input type="file" name="file">, my servlet will throw an exception. It wont if I take it out. Here is the exception:

SEVERE: Servlet.service() for servlet UploadServlet threw exception javax.servlet.ServletException: Servlet execution threw an exception at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:637)

My second question is: On the server side, I check the request to see if it is isMultipartContent, and it is. But then when I parseRequest() and store into List<FileItem> items, the items.size() is 0. Here is my Servlet code

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = null;
if(request.getContentType() != null && request.getContentType().toLowerCase().indexOf("multipart/form-data") > -1){
       if(upload.isMultipartContent(request)){
            items = upload.parseRequest(request);   //It got to here, but items.size is 0. why?????????????         
        }
}

© Stack Overflow or respective owner

Related posts about jsp

Related posts about servlets