Parsing Multipart Form Data

Convenient way to parse incoming multipart/form-data parameters in a Servlet

multipart/form-data encoded requests are indeed not by default supported by the Servlet API prior to version 3.0. The Servlet API parses the parameters by default using application/x-www-form-urlencoded encoding. When using a different encoding, the request.getParameter() calls will all return null. When you're already on Servlet 3.0 (Glassfish 3, Tomcat 7, etc), then you can use HttpServletRequest#getParts() instead. Also see this blog for extended examples.

Prior to Servlet 3.0, a de facto standard to parse multipart/form-data requests would be using Apache Commons FileUpload. Just carefully read its User Guide and Frequently Asked Questions sections to learn how to use it. I've posted an answer with a code example before here (it also contains an example targeting Servlet 3.0).

How to parse multipart form-data using only EL

multipart/form-data is used to upload file. This could be big and uploading it could take a while. In order not to block other incoming requests the processing could be asynchronous.

With Java:

@MultipartConfig:

For a servlet receiving multipart/form-data you have to add the @MultipartConfig annotation in front of the servlet, in order to get the values of the plain text parameters by calling request.getParameter("notFileFieldName");. Otherwise the call will return null.

But with the file and its content, it didn't work this way.

The file name, content-type, size and content could be retrieved from the Part object returned by

request.getPart("theFileFieldName");

For JSP you can add the following snipplet in the web.xml:

<servlet>
<servlet-name>UploadFile</servlet-name>
<jsp-file>/path/form.jsp</jsp-file>
<multipart-config></multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>UploadFile</servlet-name>
<url-pattern>/path/form.jsp</url-pattern>
</servlet-mapping>

Where the line <multipart-config></multipart-config> activate the above behavior for the JSP.

So ${param.notFileFieldName} will be evaluated to its value.

But not the ${param.theFileFieldName}

AsyncContext could used to process the upload

  • Documentation
  • Tutorial

But you won't a java solution.

With Javascript: FileReader()

A basic example how you can read file on the client side. (without upload)
Source: https://developer.mozilla.org/de/docs/Web/API/FileReader/readAsDataURL

HTML

<input type="file" onchange="previewFile()"><br>
<img src="" height="200" alt="Image preview...">

JavaScript:

function previewFile() {
var preview = document.querySelector('img');
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();

reader.addEventListener("load", function () {
preview.src = reader.result;
}, false);

if (file) {
reader.readAsDataURL(file);
}
}

try it on jsfiddle

If upload is needed it could be done AJAX.

parse multipart/form-data, received from requests post

If you're receiving a multipart/form-data response, you can parse it using the requests-toolbelt library like so:

$ pip install requests-toolbelt

After installing it

from requests_toolbelt.multipart import decoder

testEnrollResponse = requests.post(...)
multipart_data = decoder.MultipartDecoder.from_response(testEnrollResponse)

for part in multipart_data.parts:
print(part.content) # Alternatively, part.text if you want unicode
print(part.headers)


Related Topics



Leave a reply



Submit