OOP-ResearchMake It Simpler by Object Oriented Programming

How to set the maximum file size by OOP MimeParser: Java API for file upload JSP/Servlet by multipart/form-data

Java API for file upload JSP/Servlet by multipart/form-data. It can set the maximum file size to be uploaded. If the user tries to upload the larger files, your JSP/Servlet can catch the exception.
This page describes how to set the maximum file size by OOP MimeParser.

This Java API can set the maximum width/height of the image file (JPEG, GIF or PNG). For details about this feature, please read another page.

Related Pages:


Restrict Content-Length (in bytes) of the HTTP request

To protect your JSP/Servlet against some kinds of denial-of-service attacks, all the parseXX( ) methods check the Content-Length of each HTTP request before processing the HTTP request. Please specify the max_request property in:

  • (docBase)/WEB-INF/classes/MimeParser.properties
If the Content-Length of the HTTP request is larger than this property, these methods will throw TooLargeBodyException soon, without reading any bytes of the request body.
The Content-Length consists of:
  • The size of the uploaded file
  • The size of the submitted text
  • The size of the boundary text, which is generated by the web browser
So, the maximum size of the Content-Length must be larger than the maximum size of the uploaded file. For example, if you'd like to set the maximum file size to 100 KB, the maximum Content-Length should be set to 150 KB or 200 KB. If the CGI FORM has many text input fields, it should be 200 KB larger (or more) than the maximum file size.
This property must be specified in bytes. For example, if you'd like to set the maximum Content-Length to 100 KB, this property will be 102400.

Restrict the size of the file to be uploaded

In usual, your Servlets/JSP needs to set the maximum file size to be uploaded. The default maximum size can be specified in the resource file. If 0 (zero) is specified, any size of files can be uploaded. The following 8 methods use this default maximum file size:

  • parseAndSave(HttpServletRequest req, String dir)
  • parseAndSave(HttpServletRequest req, String dir, Set mime)
  • parseAndSave(HttpServletRequest req, String ch, String dir)
  • parseAndSave(HttpServletRequest req, String ch, String dir, Set mime)
  • parseOnly(HttpServletRequest req)
  • parseOnly(HttpServletRequest req, Set mime)
  • parseOnly(HttpServletRequest req, String ch)
  • parseOnly(HttpServletRequest req, String ch, Set mime)
But, the default maximum file size can be overridden by max parameter of:
  • parseAndSave(HttpServletRequest req, String dir, int max)
  • parseAndSave(HttpServletRequest req, String dir, int max, Set mime)
  • parseAndSave(HttpServletRequest req, String ch, String dir, int max)
  • parseAndSave(HttpServletRequest req, String ch, String dir, int max, Set mime)
  • parseOnly(HttpServletRequest req, int max)
  • parseOnly(HttpServletRequest req, int max, Set mime)
  • parseOnly(HttpServletRequest req, String ch, int max)
  • parseOnly(HttpServletRequest req, String ch, int max, Set mime)
By these methods, you can set the different upper limits for each requests.

When the user tries to upload the larger files, all parseXXX(...) methods will throw:
  • TooLargeFileException
So, your Servlets/JSP can catch it and have a chance to display the appropriate message.

Caution!
All the APIs for Servlet/JSP introduced by this web site are now included in Bento framework:
  • Simpler than JSTL or Apache Struts
  • MVC framework by HTML
  • Input validation from CGI FORM
  • Easy user authentication
  • Easy localization (L10n)
To download the APIs and source code examples, please visit the web site of Bento framework.


JBuilder 2007


General Information

For Java Development

Java and all Java-based trademarks and logos are trademarks or registered of Sun Microsystems, Inc. in the United States and other countries.


ALL CONTENTS COPYRIGHT 1997-2007, OOP-Research Corporation. All rights reserved.