views:

55

answers:

2

Hi all,

I'm using the apache commons fileupload stream api. But the FileItemIterator FileItemIterator iter = upload.getItemIterator(request); returns false in its hasNext() iter.hasNext() What is wrong with this?

The code and the web part is as follows:

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

    /**
     * Apache commons file upload method will be used
     */
    // Check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    if (isMultipart) {
        try {
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload();
            // Parse the request
            FileItemIterator iter = upload.getItemIterator(request);

            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String name = item.getFieldName();
                InputStream stream = item.openStream();
                if (item.isFormField()) {
                    System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
                } else {
                    System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
                    // Process the input stream
                    //...
                }
            }

        } catch (FileUploadException ex) {
              Logger.getLogger(ResourceUploadServlet.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

jsp page is as follows:

 <form action="AServlet" method="POST"
              enctype="multipart/form-data">
                            <input type="file" name="Content" />
                            Description : <input  type="text" name="Description" />
                        <input type="submit" value="Submit" />

        </form>

Best,

A: 

A few things that might be worth checking for:

  • Is the doPost() method the same as that of the servlet "AServlet" referred to in the form? This is primarily to ensure that the request has not already been read, in which case parsing of the request into FileItem objects will not happen. A request once parsed, cannot be parsed again by Commons FileUpload; usually the presence of servlets and filters upstream is responsible for this condition.
  • Is the form submit actually working? And does the issue recur with different files? Ideally, the Commons Fileupload component will not return any more items when the End Of File condition has been reached, i.e. there is no more data to read (the number of bytes in content-length header has been read from the body).

PS: It might be better to use the Logger class to be 'doubly sure' on whether the Form and File field parsing is being done or not.

Vineet Reynolds
- Request is only parsed in this doPost method- I debugged the code and when i submitted the form i've seen the method is called. Maybe a useful observation is the content length of the request is 0. What may cause this? or is this related with this problem?
small_ticket
Aha! Are you submitting from MSIE 6/7? Apparently, one of these browsers sends multiple requests to the server for the multipart POST. You can ignore the one with zero content length, I suppose.
Vineet Reynolds
+1  A: 

In my web.xml file. There was a filter

    <filter>
        <filter-name>resourceUploadServlet</filter-name>
        <filter-class>org.mortbay.servlet.MultiPartFilter</filter-class>
        <init-param>
            <param-name>maxSize</param-name>
            <param-value>2147483648</param-value>
        </init-param>
    </filter>

When i remove the filter the problem is solved...

small_ticket
In other words, filter processed request before your servlet had a chance to parse it.
Peter Štibraný
Indeed, the request body can be parsed only once. You're basically mixing two different file upload API's. The second one will get only an empty stream. You basically need to grab the uploaded files by the first one (or just disable the first one as you just did).
BalusC