Servlet 3 File Upload Example using MultipartConfig

Servlet 3 File Upload Example using MultipartConfig shows you how to handle file upload with Servlet 3. This is additional feature that Servlet 3.0 Specification supports it out of the box. Thus, a annotation @MultiPartConfig has been introduced in Servlet 3.0 Specification. This annotation can be used to annotate a servlet class in order to handle request of type multipart and configure other upload file settings. Servlets that are annotated with MultipartConfig can retrieve the Part components by calling the request.getPart(String name) or request.getParts() method. This is thirteenth post of series of Java Servlet Tutorial. This series tutorial will provide you full knowledge about Servlet 3.0.

Table of contents:
1. Project structure
2. Maven dependency
3. @MultiPartConfig annotation introduction
4. Create File Upload Servlet
5. Create view
6. Deploy Servlet 3 File Upload Example using MultipartConfig

Project structure
We will create a project with structure like below

Servlet 3 File Upload Example using MultipartConfig project

Maven dependency
We can use Servlet 3 API by adding the below dependency

@MultiPartConfig annotation introduction

The @MultipartConfig annotation includes the following optional attributes:

location: An absolute path to a directory that ised to store files temporarily. This attribute does not support a path relative to the application context. The default location is “”.

fileSizeThreshold: If the upload file’s size is greater than this threshold, it will be stored temporarily in disk. Otherwise the file is stored in memory. Size in bytes and the default value is 0 bytes.

MaxFileSize: The maximum size of uploaded files, in bytes. If the size of any uploaded file is greater than this size, the exception (IllegalStateException) will be thrown. The default size is unlimited.

maxRequestSize: The maximum size of multipart/form-data request, in bytes. The exception will be thrown if the overall size of all uploaded files exceeds this threshold. The default size is unlimited.

Create File Upload Servlet
This following class shows how to configure a servlet to handle file upload using @MultipartConfig annotation:

Let’s dig deeper:

The getParts() method returns collections of all Part objects. If form has more than one input of type file, multiple Part objects are returned.
The javax.servlet.http.Part interface provide methods that allow to get information of each Part. The methods do the following: Retrieve the name, size, and content-type of the Part, Query the headers submitted with a Part, Delete a Part, Write a Part out to disk

Create view
For demonstration the above servlet, we create a view jsp like this:

Deploy Servlet 3 File Upload Example using MultipartConfig
Now, we are ready to build war file using maven and deploy it on servlet container (for example Tomcat 8). Then, running the address http://localhost:8080/multipartconfig/upload. A a upload form will appear and we browse a file such as the below screen shot:

Servlet 3 File Upload Example using MultipartConfig form

Next, Click the submit button and we will get the uploaded file information like below:

Servlet 3 File Upload Example using MultipartConfig uploaded

That’s all on the tutorial Servlet 3 File Upload Example using MultipartConfig. You can also find other servlet example in the series of Java Servlet Example.

Download complete source code, please click link below (46 downloads)

Leave a Comment


Please share it if you found this useful
Hide Buttons