The request doesn’t contain a multipart/form-data or multipart/mixed stream, content type header is null

September 25th, 2009No Comments

org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn’t contain a multipart/form-data or multipart/mixed stream, content type header is null

If you are iterating the items from the multipart request without checking if an http request is encoded in multipart format
then you will get this error.

If ServletFileUpload.isMultipartContent(request) returns true then you need to get the fields via the upload.parseRequest(request) method.

eg:

// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart)
{
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();

// Set factory constraints
factory.setSizeThreshold(1000000);
factory.setRepository( ConfigServlet.getTempDirectory() );

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

// Set overall request size constraint
upload.setSizeMax(10000000);

// Parse the request
List items;
try {
items = upload.parseRequest(request);

for(FileItem item : items)
{

if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
query.put(name, value);

}
else // file object
{
// do stuff for file request

}

}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
query = new TreeMap();
Map tbl = request.getParameterMap();
query.putAll( tbl );
}

Related Posts:

Tagged : , , ,

No Responses

Leave a Reply