[Demo] Multipart File Upload in Servlet 3.0 Using Multipart Config - Errors and Exceptions

Methods related to getPart will throw IOException if an I/O error occurred during the retrieval of the Part components of this request, ServletException if this request is not of type multipart/form-data or IllegalStateException if the request body is larger than maxRequestSize, or any Part in the request is larger than maxFileSize.

This is the second part of the demo and we will be using the same class files as in first demo with any modifications as needed. You can see the first part for more details and complete source code: http://javajee.com/demo-multipart-file-upload-in-servlet-30-using-multip...

 

HTML Form

We will need an html form to upload the file from client side. To send the request as multipart/form-data, the enctype attribute must be supplied and set to multipart/form-data, so that no characters are encoded. So what will happen if we don’t mention anything there? Other possible values for the enctype attribute are text/plain and application/x-www-form-urlencoded, which is the default if nothing is specified. The text/plain value denotes that spaces are converted to "+" symbols, but no special characters are encoded. The application/x-www-form-urlencoded (default) value denotes that All characters are encoded before sent (spaces are converted to "+" symbols, and special characters are converted to ASCII HEX values).

Change the value to application/x-www-form-urlencoded.

<form method="POST" action="upload" enctype="application/x-www-form-urlencoded">

Since we are changing only html (static content), you may directly do so from within your context root folder in tomcat webapps folder. Once you execute the upload request using this form, you will get below exception:

javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded

...

org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded

...

Part of the request sent as captured using tcpmon is as below:

If you remove the attribute completely, you will get the same exception and request, as application/x-www-form-urlencoded is the default.

If you change the enctype to text/plain, then also you will get a similar error response and request: application/x-www-form-urlencoded in the error and request will be changed to text/plain.

 

FileUploadServlet.java

We will use the maxFileSize and maxRequestSize attributes of @MultipartConfig to specify sizes and then will try to upload a file with greater size than maxFileSize. You can pass an attribute maxFileSize to the MultipartConfig annotation as:

@MultipartConfig(maxFileSize=100,maxRequestSize=200)

Since this is a java class change, you will need to redeploy your war file.

Also remember to change back enctype value to multipart/form-data: enctype="multipart/form-data".

After deploying, run the html file and add a very small file.

Since the request is of more size than maxRequestSize, I got the exception as:

java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (2652) exceeds the configured maximum (200)

org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (2652) exceeds the configured maximum (200)

Now try uploading a bigger file and you will get the same exception for request size.

Now try to increase the maxRequestSize value to a bigger size, say 50000, and then try to upload a file bigger than 100, but with the total request size not exceeding 5000.

You will now get the exception as:

java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 100 bytes.

org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 100 bytes.

Even though the documentation doesn’t mention explicitely, the sizes are all in bytes.

 

Development and Execution Environment

Eclipse Luna Java EE IDE for Web Developers and Apache Tomcat 8.0.18, using Servlet spec 3.1.

 

References

http://javajee.com/multipart-file-upload-in-servlet-30-using-multipart-config

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!