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