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.
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();