You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by N G <ni...@gmail.com> on 2008/12/21 19:08:16 UTC

FileItemIterator only has 1 part, dropping the rest of the file

Hi,

I have the following code using FileUpload streaming. However,
probably due to my overlooking something, it only returns the 'while'
loop executes only once! That is, FileItemIterator returns true for
hasNext only once.

This causes only the first part of the file captured, dropping the rest.

Can someone point out what I am doing wrong here? The code is almost
verbatim from the apache site. I do want to point out that the file is
not very big. It's just a text file of size 6.6Kb.

private String getFileContent(HttpServletRequest request)
    throws IOException
  {
    // Check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    if (!isMultipart)
    {
      String message = "Request must be multipart.";
      logger.error(message);
      throw new IOException(message);
    }

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload();

    // Initialize string content
    StringBuilder content = new StringBuilder();

    try
    {
      // Parse the request
      FileItemIterator iter = upload.getItemIterator(request);
      int i = 0;
      while (iter.hasNext())
      {
        FileItemStream item = iter.next();
        InputStream stream = item.openStream();
        byte[] fileBytes = new byte[stream.available()];
        stream.read(fileBytes);

        String tempContent = new String(fileBytes);
        content.append(tempContent);
      }
    }
    catch (FileUploadException e)
    {
      logger.error(e);
    }

    return content.toString();
  }

-- 
Thanks,
NG

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org