views:

211

answers:

2

Does anyone know of a workaround whereby if you are trying to flush the servlet output stream, apache commons fileupload throws the following exception?

FileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly

Basically I have code that loops through each file uploaded using apache commons fileupload, and then am trying to out.flush() some stats about each file. ie:

 FileItemFactory factory = new DiskFileItemFactory();
 ServletFileUpload upload = new ServletFileUpload(factory);
 List<FileItem> items = upload.parseRequest(request);

 for(FileItem field : items) {
  if (!field.isFormField() && field.getName().length()>0 && field.getName().getSize()>0) {
   ArticleImport helper = new ArticleImport(new ArticleImportResponder(user,out));
   // This helper object uses out.flush() to provide feedback to the user.
   helper.process(field.getInputStream(), user);
   }
  }
 }

The problem does not occur in Apache Tomcat 6.0.20, but it does occur in earlier versions.

+1  A: 

Unfortunately this is a known bug, there is nothing you can do about it except ensure that your using at least 6.0.20.

You could detect which version of tomcat is running and use that to determine if you can use out.flush() using the following call:

javax.servlet.ServletContext.getServerInfo();
Jacob
close enough (:
corydoras
What exactly was the known bug? I'm having roughly the same issue with Tomcat and a webapp using Apache Commons File Upload.
Jon
A: 

Hi there, Technically this is not an answer, and I apologize for that in advance, but StackOverflow does not appear to allow me to comment on questions. No idea why.

I've been having a similar issue, and I wondered if you could refer me to the Tomcat bug which was mentioned in the above answer?

Thank you

Jon

Jon
This was long enough ago that I unfortunately can't remember. I have just made it a point now to ensure when this feature is needed, the tomcat server is up to date. From memory it was some of the installs of 6.0.1x that had the problem.
corydoras
Thanks for the reply. I need to be able to justify any move away from the version of Tomcat we are using, and for that I need to lay out exactly what the bug is. Any idea what the exact version number with the problem was? I can't seem to find anything in the Tomcat release notes.
Jon
If you have run into this problem cant you just show people the problem, and show that it is fixed in the next version? (If you are not having this problem then I am not sure why you would care)
Jacob
Only a single one of our clients is having this problem, the rest are fine. We're also unable to replicate the bug when the client machine is Windows 7. And, call me an idiot, but I really want to know *why* this is happening. WireShark hasn't been too much help. It shows the last frame sent to the server, a few times before it gives up - then you see a stack trace, server side.
Jon