title: JSP - File upload with Apache Commons
body: <p>Why my file-upload code is not working?</p>
  
  <p>I am using "commons-fileupload-1.1.1.jar".</p>
  
  <p>Also I am seeing a strikethrough in the 2nd Line on "isMultipartContent" in NetBeans 6.1.</p>
  
  <pre><code>    // Check that we have a file upload request  
  &gt;&gt;&gt;&gt;    boolean isMultipart = FileUpload.isMultipartContent(request);  
  
  // Create variables for path, filename and extension  
  appPath = application.getRealPath("\\");
  
  // Create a factory for disk-based file items  
  FileItemFactory factory = new DiskFileItemFactory();  
  
  // Create a new file upload handler  
  ServletFileUpload upload = new ServletFileUpload(factory);  
  
  // Parse the request and Extract request items  
  &gt;&gt;&gt;&gt;&gt;    List items = upload.parseRequest(request);
  
  // create an Iterator to iterate through request items  
  Iterator iter = items.iterator();
  
  //Form fields
  //out.println("&lt;br&gt;&lt;br&gt;While loop started");
  
  while (iter.hasNext())
  {  
    FileItem item = (FileItem) iter.next();
  
    if(item.isFormField())
    {
      String name = item.getFieldName();  
      String value = item.getString();  
  
      if(name.equals("txtUsername")) 
      {
        _USERNAME_ = value;
  
        Class.forName("org.gjt.mm.mysql.Driver");
  
        try
        {
          connection = DriverManager.getConnection("jdbc:mysql://localhost/ict");
          statement = connection.createStatement();
          recordset = statement.executeQuery("SELECT * FROM registered_students WHERE username=\'" + _USERNAME_ + "\'");
  
          while(recordset.next())
          {
            roll = recordset.getString(4);
  
            _ROLL_ = roll;
          }
  
          recordset.close();
          recordset=null;
  
          statement.close();
          statement=null;
        }
        finally
        {
          if(connection!=null)
          {
            connection.close();
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>And I am getting this message:</p>
  
  <pre><code>org.apache.jasper.JasperException: An exception occurred processing JSP page /Process_FileUpload.jsp at line 75
  
  73: 
  74:         // Parse the request and Extract request items  
  75:         List items = upload.parseRequest(request);  
  76:         
  77: 
  78:         // create an Iterator to iterate through request items  
  79:         Iterator iter = items.iterator();
  
  Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
  
  root cause
  
  javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
      org.apache.jsp.Process_005fFileUpload_jsp._jspService(Process_005fFileUpload_jsp.java:546)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
  
  root cause
  
  java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
      org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:179)
      org.apache.commons.fileupload.FileUploadBase.createItem(FileUploadBase.java:500)
      org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:367)
      org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)
      org.apache.jsp.Process_005fFileUpload_jsp._jspService(Process_005fFileUpload_jsp.java:138)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
  
  note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.
  </code></pre>