Another parser for multipart http request that handle multipart/form-data request, object MutiForm accept multiple files and parameters in http request, fully compatible rfc1867, rfc2388 and API Servlet 2.4.

Object MultiForm is fast, robust and elegant to use :

- Use getMaxSize() method to set maximum size of http request

- Use getTempDirectory() method to set directory for temporary file

Example in servlet, upload a simple gif image :

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jservlet.servlet.multiform.MultiForm;
import com.jservlet.servlet.tool.BServlet;

public class UploadImage extends BServlet
{
 
     /**
      * Méthode appelée lorsque la requête est de type ENCTYPE="multipart/form-data" ...
      */
      public void doMultiForm(MultiForm _request, HttpServletResponse _res)
      throws ServletException, IOException
      {
           // do multiform...
          
        PrintWriter out = _res.getWriter();
   
        // errors...
        String err = "Upload OK !!!";
       
        // get data
        String nom = _request.getParameter("nom");
        String prenom = _request.getParameter("prenom");
       
        int age = -1;
        try { age = (new Integer(_request.getParameter("age")).intValue());}
        catch(Exception e) {}
       
        String photo = _request.getParameter("photo");
        photo = (photo==null) ? "" : photo;
   
        // get photo gif ...
        if (!photo.equals("") && _request.isFile("photo"))
        {
            MultiForm.File a_mf = _request.getFile("photo");
           
            // Test file size
            if (!a_mf.getName().equals("") && a_mf.length()>0)
            {
                if (a_mf.length()<=102400) // 1024 x 100 octets
                {
                    if (a_mf.isGif())  // test type file
                    {
                        // save image on disk           
                        try { a_mf.save("/tmp/photo.gif"); }
                        catch(Exception e) { out.println(e.toString()); }
                       
                    } else err = "Image is not Gif format!";
                } else err = "Image is too big! (100 ko max)";
            } else err = "Image is empty!";
        }
      

        //...
        out.write(err);

    }

   /**
    * Méthode pour indiquer quel répertoire utiliser pour les fichiers temporaires...
    */
    public String getTempDirectory() { return "/tmp/"; }

   /**
    * Méthode pour indiquer quelle est la taille maximale que peut avoir une requête.
    */
    public int getMaxSize() { return 5242880; } // 5 Mo maxi!
     
    /**
     * Méthode appelée lorsque la requête est de type Get.
     */
    public void doGet(HttpServletRequest _request, HttpServletResponse _res)
    throws ServletException, IOException
    {
        // do get...
    }
   
    /**
     * Méthode appelée lorsque la requète est de type Post.
     */
    public void doPost(HttpServletRequest _request, HttpServletResponse _res)
    throws ServletException, IOException
    {
        // do post...
    }
} // end of class


In JSP :

<%@ page import="java.util.*, com.jservlet.servlet.multiform.*" language="java"%>
<%

    // test request if appropriate...  
    MultiForm a_mf = null;
    if (MultiForm.isMultiForm(request))
    {
      String a_dir = "c:\\temp\\";     // dir temp...
      int a_maxSize = 5242880;         // max http request size...
     
      if (a_dir!=null) a_mf = (a_maxSize<0) ? new MultiForm(request,a_dir) : new MultiForm(request,a_dir,a_maxSize);    
      else a_mf = (a_maxSize<0) ? new MultiForm(request) : new MultiForm(request,a_maxSize);
    }

    // error ?
    String err = "Upload OK !!!";
       
    String photo = a_mf.getParameter("photo");
    photo = (photo==null) ? "" : photo;

    // get photo gif ...
    if (!photo.equals("") && a_mf.isFile("photo"))
    {
        MultiForm.File a_f = a_mf.getFile("photo");
       
        // Teste file
        if (!a_f.getName().equals("") && a_f.length()>0)
        {
            if (a_f.length()<=102400) // 1024 x 100 octets
            {
                if (a_f.isGif())  // gif format ?
                {
                    // ok save image on disk       
                    try { a_f.save("C:\\temp\\photo.gif"); }
                    catch(Exception e) { out.println(e.toString()); }
                   
                } else err = "Image is not Gif format!";
            } else err = "Image is too big! (100 ko max)";
        } else err = "Image is empty!";
    }
   

    //...
    out.write(err);
%>


Source : MultiForm.java