General
FileUpload and Struts 1
FileUpload and Flash
There are two common causes for this error.
Firstly, it might simply mean that you do not have the Commons IO jar in your classpath. FileUpload depends on IO (see dependencies) - you can tell if this is the case if the missing class is within the org.apache.commons.io package.
Secondly this happens when attempting to rely on a shared copy of the Commons FileUpload jar file provided by your web container. The solution is to include the FileUpload jar file as part of your own web application, instead of relying on the container. The same may hold for FileUpload's IO dependency.
String fileName = item.getName();
if (fileName != null) {
filename = FilenameUtils.getName(filename);
}
At least as of version 8, Flash contains a known bug: The multipart stream it produces is broken, because the final boundary doesn't contain the suffix "--", which ought to indicate, that no more items are following. Consequently, FileUpload waits for the next item (which it doesn't get) and throws an exception.
The problems details and a possible workaround are outlined in Bug 143 . The workaround suggests to use the streaming API and catch the exception. The resulting code could look like this:
final List<FileItem> items = new ArrayList<FileItem>();
HttpServletRequest servletRequest = [...];
RequestContext ctx = new ServletRequestContext(servletRequest);
FileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(ctx);
try {
while (iter.hasNext()) {
FileItemStream item = iter.next();
FileItem fileItem = fileItemFactory.createItem(item.getFieldName(),
item.getContentType(),
item.isFormField(),
item.getName());
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
items.add(fileItem);
}
} catch (MalformedStreamException e) {
// Ignore this
}