Can a servlet determine if the posted data is multipart/form-data?

Yes, the Content-type header in the user agent's request should include multipart/form-data as described in (at least) the HTML4 spec:

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2


You can call a method to get the content type.

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getContentType()

According to http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2, the content type will be "multipart/form-data".

Don't forget that:

  1. request.getContentType() may return null.

  2. request.getContentType() may not be equal to "multipart/form-data", but may just start with it.

So, with all this in mind:

if (request.getContentType() != null && 
    request.getContentType().toLowerCase().indexOf("multipart/form-data") > -1 ) 
{
    << code block >>
} 

If you are going to try using the request.getContentType() method presented above, be aware that:

  1. request.getContentType() may return null.
  2. request.getContentType() may not be equal to "multipart/form-data", but may just start with it.

With this in mind, the check you should run is :

if (request.getContentType() != null && request.getContentType().toLowerCase().indexOf("multipart/form-data") > -1 ) {
// Multipart logic here
}