Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a servlet that is used for many different actions, used in the Front Controller pattern. Does anyone know if it is possible to tell if the data posted back to it is enctype="multipart/form-data"? I can't read the request parameters until I decide this, so I can't dispatch the request to the proper controller.

Any ideas?

like image 686
pkaeding Avatar asked Sep 15 '08 20:09

pkaeding


People also ask

What is a multipart request in Servlet?

public interface MultipartHttpServletRequest extends HttpServletRequest, MultipartRequest. Provides additional methods for dealing with multipart content within a servlet request, allowing to access uploaded files.

How multipart form data is sent?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

What is used for post method in servlet?

doPost. Called by the server (via the service method) to allow a servlet to handle a POST request. The HTTP POST method allows the client to send data of unlimited length to the Web server a single time and is useful when posting information such as credit card numbers.

What is multipart form data in REST API?

multipart/form-data is often found in web application HTML Form documents and is generally used to upload files. The form-data format is the same as other multipart formats, except that each inlined piece of content has a name associated with it.


1 Answers

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
}
like image 141
Darren Hicks Avatar answered Oct 16 '22 15:10

Darren Hicks