[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

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)