title: Image uploading using Java Servlet with Multipartrequest
body: <pre><code> package imageUplaod;
  
      import java.io.File;
      import java.io.IOException;
      import java.io.PrintWriter;
  
      import javax.servlet.ServletContext;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import com.oreilly.servlet.MultipartRequest;
      import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
      import java.util.Enumeration;
  
  
  
       public class UploadImage extends HttpServlet {
         private static final long serialVersionUID = 1L;
  
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {        
  
      final int permitedSize = 314572800;  //~ 300 MB in bytes
  
      PrintWriter out = response.getWriter();
      response.setContentType("text/html;charset=UTF-8");
      response.setHeader("Cache-Control", "no-cache");
      response.setHeader("Pragma", "No-cache");
      response.setDateHeader("Expires",0);                       
  
      try{               
         String type = "";
         String name = "";
         String originalFilename = "";
         String extension1 = "";
         String extension2 = "";
         String filename = "";
  
         String strDirectory = "files";
         ServletContext sc=getServletContext();
         String uploadPath = sc.getRealPath("//WEB-INF//"+strDirectory+"//");
  
         MultipartRequest multipartRequest = new MultipartRequest(request, uploadPath, permitedSize, "ISO-8859-1", new DefaultFileRenamePolicy()); 
  
         Enumeration&lt;String&gt; files = multipartRequest.getFileNames();           
  
            while (files.hasMoreElements()) 
               { 
                   name = files.nextElement();
                   type = multipartRequest.getContentType(name); 
                   filename = multipartRequest.getFilesystemName(name); 
                   originalFilename = multipartRequest.getOriginalFileName(name);                 
  
                   extension1 = filename.substring(filename.length() - 4, filename.length());
                   extension2 = originalFilename.substring(originalFilename.length() - 4, originalFilename.length());
  
                   //return a File object for the specified uploaded file                   
                   File currentFile = multipartRequest.getFile(name);
                   InputStream inputStream = new BufferedInputStream(new FileInputStream(currentFile));
                   if(currentFile == null) {
                        out.println("There is no file selected!");
                        return;
                      }
  
                   //checking the file extensions according to the MIME types
                   if((type.equals("text/plain") || type.equals("image/gif") || type.equals("image/pjpeg")
                     || type.equals("image/x-png") || type.equals("image/jpeg"))) {       
                       //show a few details about the uploaded file        
                       out.println("file: "+filename);
                       out.println("type: "+type); 
                       out.println("size: "+currentFile.length());
                     } else {
                              out.println("\n Invalid format!");
                            } 
                   }                        
  
         }catch (Exception exception)
            {                     
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        } finally { if(out != null) {out.close();} }      
      }       
  
  
      public UploadImage() {
          super();
          // TODO Auto-generated constructor stub
      }
  
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          processRequest(request, response);
  
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          processRequest(request, response);
  
      }
      }
  
         this is my index page:
  
      &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;&lt;%@page
      language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;title&gt;index&lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
      &lt;h5&gt;O'Reilly COS Upload&lt;/h5&gt;
      &lt;table&gt;
      &lt;tr&gt;
       &lt;td align="center"&gt;
        &lt;form enctype="multipart/form-data" action="UploadImage" method="POST"&gt;
         Select the file to upload: 
         &lt;input type="file" name="userfile"&gt;
         &lt;input type="submit" value="Upload"&gt;
      &lt;/form&gt;
      &lt;/td&gt;
      &lt;/tr&gt;
     &lt;/table&gt;
     &lt;/body&gt;
     &lt;/html&gt;
  </code></pre>
  
  <p><strong>when I run it I get following error:</strong></p>
  
  <pre><code>      Error 404: javax.servlet.UnavailableException: SRVE0203E: Servlet [UploadImage]:  
       imageUplaod.UploadImage was found, but is missing another required class. SRVE0206E: 
  </code></pre>
  
  <p>This error typically implies that the servlet was originally compiled with classes which cannot be located by the server. SRVE0187E: Check your class path to ensure that all classes required by the servlet are present.SRVE0210I: This problem can be debugged by recompiling the servlet using only the classes in the application's runtime class path SRVE0234I: </p>
  
  <pre><code>  Application class path=[C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib\dt.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib\htmlconverter.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib\ibmorbtools.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib\jconsole.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\java\lib\tools.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\classes;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\classes;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\EJBCommandTarget.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\IVTClient.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\OTiSConvertTime.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\activation-impl.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\aspectjrt.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\bootstrap.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\bsf-engines.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\cimgr.ops.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\commandlineutils.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\commons-discovery.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\databeans.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\ffdcSupport.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\htmlshell.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\installver.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\installxml.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\iscdeploy.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\ivblogbr.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\j2ee.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\jNative2ascii.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\jacl.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\jrom.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\jsf-nls.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\launchclient.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\lmproxy.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\mail-impl.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\nif.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\pc-appext.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\pmirm4arm.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\rrd-appext.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\rsadbutils.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\rsahelpers.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\serviceadapter.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\setup.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\startup.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\tcljava.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\urlprotocols.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\wses_dynaedge.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\lib\wsif-compatb.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\installedChannels;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\web\help;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime\batch.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime\ejbdeploy.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\deploytool\itp\plugins\com.ibm.etools.ejbdeploy\runtime\ejbmapvalidate.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\derby\lib\derby.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\installedConnectors\sib.api.jmsra.rar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.commonservices.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.connector.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.headers.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.local.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.remote.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jmqi.system.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.jms.admin.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mq.pcf.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.mqjms.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.commonservices.j2se.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.commonservices.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.jms.internal.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.jms.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.matchspace.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.provider.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.ref.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.common.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.factories.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\com.ibm.msg.client.wmq.v6.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1\installedConnectors\wmq.jmsra.rar\dhbcore.jar;C:\Program Files (x86)\IBM\SQLLIB\java\db2jcc.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\universalDriver\lib\db2jcc_license_cu.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7\profiles\was70profile1/properties;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/properties;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/lib/startup.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/lib/bootstrap.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/lib/jsf-nls.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/lib/lmproxy.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/lib/urlprotocols.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/deploytool/itp/batchboot.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/deploytool/itp/batch2.jar;C:\Program Files (x86)\IBM\SDP\runtimes\base_v7/java/lib/tools.jar;C:\Users\drunkendeath\IBM\rationalsdp\workspace\UploadImage\WebContent\WEB-INF\classes;C:\Users\drunkendeath\IBM\rationalsdp\workspace\UploadImage\WebContent]
  </code></pre>
  
  <p>So what is wrong here, can anybody explain?
  I have included all the .jars. There is no error in code.</p>