Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

problem with apache tomcat and commons file upload API

843838Oct 14 2006 — edited Apr 15 2009
I have been trying to work on a servlet that can handle file uploads..
I have downloaded the commons fileupload API from the jakarta.apache.org site..

i copied the jar file to my jdk\jre\lib\ext directory and jdk\lib\..
I m using netbeans5.0 rc2 as my editor..

Then i made a new servlet file with the following code..
 import java.io.*;
import java.net.*;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class fileuploadservlet2 extends HttpServlet {

 protected void doPost(HttpServletRequest req, HttpServletResponse response)
    throws ServletException, IOException {
       
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        out.println("</html>");
         DiskFileItemFactory factory = new DiskFileItemFactory();

        factory.setSizeThreshold(4096);
      
        factory.setRepository(new File("/tmp"));

        ServletFileUpload upload = new ServletFileUpload(factory);
     
        upload.setSizeMax(1000000);
      
        List fileItems=null;
        
        
       try
       {
           fileItems = upload.parseRequest(req);
       }catch(FileUploadException e)
       {
            out.println("Exception Raised:"+e);
       }
        
        Iterator i = fileItems.iterator();
        String comment = ((FileItem)i.next()).getString();
        FileItem fi = (FileItem)i.next();
         String fileName = fi.getName();
        try
        {
            fi.write(new File("/", fileName));
        }catch(Exception e)
        {
            out.println("Exception Raised:"+e);
        }
        out.close();
    }
public String getServletInfo() {
        return "Short description";
    }
}
this is getting compiled..but at runtime i m getting strange errors with apache tomcat 5.5 which is bundled with Netbeans..

it says a class not found exception..

javax.servlet.ServletException: Servlet execution threw an exception
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)

root cause :
java.lang.NoClassDefFoundError: javax/servlet/ServletInputStream
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)
fileuploadservlet2.doPost(fileuploadservlet2.java:86)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)

i cant understand why this type of exception is occuring...Why cant it find the javax.servlet.ServletInputStream Class....is there anything wrong in the commons fileupload API???

please help..thanks in advance..
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 13 2009
Added on Oct 14 2006
7 comments
1,598 views