(Servlet 3.0) Using @MultipartConfig, but getParts() returns null

Setup

You are trying to use the new getParts() method defined in the Servlet 3.0 spec to process your multipart/form-data HTTP-POST uploads without using Commons FileUpload.

Your servlet looks something like this:

@WebServlet("/image/*")
@MultipartConfig(location="D:\\Temp", fileSizeThreshold=10, maxFileSize=10485760)
public class ImageServlet extends APIServlet {
}

And your HTML looks something like this:

<form action="image/upload.json?version=1.0" enctype="multipart/form-data" method="POST">
<input type="file" />
<input type="submit" value="Upload" />
</form>

Problem

No matter what you do, every time your form posts,  down in your servlet where you call request.getParts() it always returns null.

Solution

You probably forgot the name argument for your file field in your HTML, more specifically:

<form action="image/upload.json?version=1.0" enctype="multipart/form-data" method="POST">
<input name="file" type="file" />
<input type="submit" value="Upload" />
</form>

I fought with this for the better part of 2 hours before the simple solution presented itself. It always seems the longer you struggle with a problem, the easier the fix.

Tags: , , , , , , ,

About Riyad Kalla

Software development, video games, writing, reading and anything shiny. I ultimately just want to provide a resource that helps people and if I can't do that, then at least make them laugh.

, , , , , , ,

No comments yet.

Leave a Reply


9 − = 1