commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "N G" <nistar...@gmail.com>
Subject FileItemIterator only has 1 part, dropping the rest of the file
Date Sun, 21 Dec 2008 18:08:16 GMT
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


Mime
View raw message