OOP-ResearchMake It Simpler by Object Oriented Programming

File upload Servlets/JSP by multipart/form-data : MimeParser Part 1

File upload Servlets/JSP by multipart/form-data. How to upload files by Servlets/JSP. You can set the maximum file size and the maximum width/height of the image file (JPEG, GIF or PNG). Your JSP/Servlet can accept the file upload request only for the specific mime-types. In addition, you have the full control over where to save the uploaded files.
This Java API parses InputStream of HTTP POST request with multipart/form-data encode. By this Java API, only the single method call is enough for your file upload Servlets/JSP.
If the user tries to upload larger files or files of the invalid mime-types, this API will throw the exception, i.e. your Servlets/JSP has a chance to catch the exception and show the appropriate message.
Optionally, your JSP/Servlet can check if the binary contents of the uploaded file are really the ones for PNG, GIF, JPEG or PDF. If the binary contents do not match with the expected pattern for PNG, GIF, JPEG or PDF, this API will throw the exception. By this way, your JSP/Servlet can prevent the fake PNG, GIF, JPEG or PDF to be uploaded.
Your Servlets/JSP has the full control over where to save the uploaded files. Or, your Servlets/JSP can get the binary contents of the uploaded files, without saving the uploaded files on the disk.
Please download and explore the free source code example.

Putting it all togather : OOP Bento framework

This Java API is now a part of OOP Bento framework. In this MVC framework by XML, you can write all the GUIs in the web application in the normal HTML, XHTML, WML, HDML or any other mark-up languages. And you no longer need to bother about the user authentication and the session tracking. Because its full working source code example will be the good starting point of this Java API, please also try OOP Bento framework.

Another solution... file upload by e-mail :

The file upload by multipart/form-data works in most cases. However, a growing number of the WAP phones are shipped with the digital cameras, i.e. the users can take the pictures by their mobile phones and send them to their friends by the e-mails. And, by OOP Pop3Receiver API, you can take the advantage of this feature. It is the Java API for connecting to the POP3 servers through JavaMail API, and you can extract the file attachment from the received e-mail. Be ready for file upload from WAP phone!

Related Pages:


Java API for file upload Servlets/JSP by multipart/form-data

OOP MimeParser is the Java API for file upload Servlets/JSP. This Java API parses the InputStream of the HTTP POST request with multipart/form-data encoding.
Before it parses multipart/form-data, it checks:

  • The file size of the file to be uploaded
  • The width/height of the image file (JPEG, GIF or PNG) to be uploaded
  • The mime-type of the file to be uploaded
  • The binary contents of the file to be uploaded
By this Java API, your Servlet/JSP can specify the maximum file size to be uploaded. As for the image file (JPEG, GIF or PNG), your Servlet/JSP can also check their width/height. This feature will be useful if you are going to build the online photo album. If the user tries to upload the larger file, this API throws the exception. Then, your Servlet/JSP has a chance to catch the exception and show the appropriate message to the user.

In most web applications, the acceptable mime-types (Content-Type) must also be restricted. Moreover, it is desirable to check if the binary contents of the uploaded file match with the expected pattern of the mime-type, such as PNG, JPEG, GIF or PDF. Let's imagine some bad case... The bad user may try to upload the file with the fake file name. For example, s/he may try to upload the text file whose file name is "myphoto.jpg". In this case, the web browser may resolve its mime-type as image/jpeg, based on its file extension. OOP MimeParser can detect this problem, and throws CorruptedBinaryContentsException.

After it parses multipart/form-data, your Servlet/JSP can save the binary contents of the uploaded files on the server side. Your Servlet/JSP has the full control over:
  • The directory where the uploaded file will be saved
  • The file name by which the uploaded file will be saved
In addition, your Servlet/JSP can access:
  • The binary contents of the uploaded files
  • The text contents of the uploaded files
  • The original file names on the client side
  • The mime-types of the uploaded files
  • The values of the text parameters in the CGI FORM
The text parameter includes:
  • INPUT with the type of TEXT
  • INPUT with the type of HIDDEN
  • INPUT with the type of PASSWORD
  • INPUT with the type of CHECKBOX
  • INPUT with the type of RADIO
  • INPUT with the type of SUBMIT
  • SELECT
  • TEXTAREA
in the departing CGI FORM. To get the values of these text parameter, your Servlet/JSP can specify the charset by which the InputStream will be decoded. By this way, the values of these text parameters can contain the 2 bytes characters, such as Japanese Shift_JIS, Chinese BIG5, Chinese GB2312 or Russian KOI8-R.

Go Site Map

Compatible with Servlet/JSP Specification

OOP MimeParser fully complies with Servlet/JSP Specification, which means that MimeParser can be used on all Servlet/JSP servers. In fact, we confirmed that OOP MimeParser runs well on:



Go Site Map

Easy programming interface

The main class of this API is:

  • com.oopreserch.web.mime.MimeParser
This class is responsible for:
  • Check the size of the files to be uploaded
  • Check the mime-type (Content-Type) of the files to be uploaded
  • Check the width/height of the image file
  • Parse the InputStream from the HTTP POST request
  • Save the binary contents of the uploaded files on the server side (optional)
All you need are:
  • Get the instance of MimeParser
  • Call parseOnly(...) method on it
  • Or, call parseAndSave(...) method on it
That's all. For example,

<%!
    // The static reference is enough, because
    // MimeParser is Thread Safe!
    private static MimeParser mime=null;

    // The Set object which includes the acceptable
    // mime-types.
    private static Set accept_mime=null;

    public void jspInit(){
        try{
            mime=MimeParser.getInstance();
	    // Read the accepatble mime-types from:
	    //    WEB-INF/classes/AcceptMime.properties
            accept_mime=AcceptMime.getAcceptMime();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
%>

<%
    // This method does not save the uploaded
    // files by itself.
    // The maximum file size must be specified by:
    //    WEB-INF/classes/MimeParser.properties
    // Only the specified mime-types can be accepted.
    ParsedData data=mime.parseOnly(request,accept_mime);

    // Then, you can save the binary contents
    // to anywhere you like.
    // The path to the file can be specified
    // by the second parameter.
    // The first parameter of this method is
    // the name of FILE INPUT in the CGI FORM.
    // This is required because the multiple
    // files can be uploaded by just one request.
    data.saveBinaryContents("example","/home/foo/tmp/bar.gif");
%>


Or, MimeParser can save the uploaded file by itself:

<%!
    // The static reference is enough, because
    // MimeParser is Thread Safe!
    private static MimeParser mime=null;

    // The Set object which includes the acceptable
    // mime-types.
    private static Set accept_mime=null;

    public void jspInit(){
        try{
            mime=MimeParser.getInstance();
	    // Read the accepatble mime-types from:
	    //    WEB-INF/classes/AcceptMime.properties
            accept_mime=AcceptMime.getAcceptMime();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
%>

<%
    // This method saves the uploaded
    // files by itself.
    // The maximum file size must be specified by:
    //    WEB-INF/classes/MimeParser.properties
    // The uploaded file will be saved in:
    //    test
    // sub-directory under the root directory.
    // The root directory must be specified also by:
    //    WEB-INF/classes/MimeParser.properties
    // Only the specified mime-types can be accepted.
    mime.parseAndSave(request,"test",accept_mime);
%>



In the latter example, the uploaded file will be saved under the specified sub-directory. For example, if the root directory is:
  • /home/foo/webapps/mime
the uploaded file will be saved under:
  • /home/foo/webapps/mime/test
It is easy, isn't it? As you notice, the property file plays an important role in this API. The list of the properties is available at the link below: MimeParser implements:
  • 8 types of parseOnly(...) methods
  • 8 types of parseAndSave(...) methods
with the different set of parameters. And these parameters can override some of the values specified in the property files. So, you have the full controle over: The return type of all the parseXX(...) methods are always ParsedData. Your Servlets/JSP can access:
  • The binary contents of the uploaded files
  • The text contents of the uploaded files
  • The original file names on the client side
  • The mime-types of the uploaded files
  • The values of the text parameters in the CGI FORM
from the returned instance of ParsedData.

Please also look into API documentation (JavaDoc) of OOP MimeParser.

Go Site Map

Source Code Example:

Seeing is believing! The very simple examples are running on our web site. Please try these example to your heart contents. Then, download and explore their source code.


Go Site Map

Requirement

The following API is required :

Please place the JAR files of these API under WEB-INF/lib directory.

Go Site Map

Download

This API is now a part of Bento framework. There are the better documentations and source code examples on the web site for Bento framework. There, you will also find the link to download the entire framework or each API in it.

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.