Hi everyone,
i have problem with servlet for upload files,every help will be much appreciated.
I am using the Apache API for uploading files.My original servlet is longer than what i will paste here,but i think that if i understand the problem of the short servlet here, i will fix it for my main servlet.The short servlet is this:

import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;

public class TestUploadFiles extends HttpServlet {
public void doGet(javax.servlet.http.HttpServletRequest aRequest , HttpServletResponse aResponse)
throws ServletException , IOException
{
HttpSession session1 = aRequest.getSession();

// boolean isMultipart = ServletFileUpload.isMultipartContent( new ServletRequestContext(aRequest) );
boolean isMultipart = ServletFileUpload.isMultipartContent( aRequest );
}

public void doPost(HttpServletRequest aRequest , HttpServletResponse aResponse )
throws ServletException , IOException
{
doGet(aRequest,aResponse);

}
}

The html form that i am using is this:
<html>
<head>
<title> Upload File Form </title>
</head>
<body>
aaa
<form method=post enctype="multipart/form-data" action="TestUploadFiles">
File to upload: <input type=file name="upfile"><br>
<input type=submit value="Submit">
</form>
</body>
</html>

When i am starting the form on Tomcat , the form appears on the screen, then i push the Browse button and choose a file from the harddisk, then i press the Submit button and i get exception:


javax.servlet.ServletException: Servlet execution threw an exception

root cause

java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
org.apache.commons.fileupload.servlet.ServletFileU pload.isMultipartContent(ServletFileUpload.java:68 )
TestUploadFiles.doGet(TestUploadFiles.java:20)
TestUploadFiles.doPost(TestUploadFiles.java:26)
javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

Then i decided to change the row boolean isMultipart = ServletFileUpload.isMultipartContent( aRequest ); with a
boolean isMultipart = ServletFileUpload.isMultipartContent( new ServletRequestContext(aRequest) );

The result when it is executed from the html from (when i choose file from the harddisk and press submit) is exception again, this time it is:
javax.servlet.ServletException: Servlet execution threw an exception

root cause

java.lang.NoClassDefFoundError: javax/servlet/ServletInputStream
TestUploadFiles.doGet(TestUploadFiles.java:20)
TestUploadFiles.doPost(TestUploadFiles.java:26)
javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

For me it is quite strange, i have been dealing with this problem for 2 days and can't figure the problem.
I have tried to check for the first case if aRequest is null, well - it is not, i have check for the servlet-api.jar - it is in my classpath, it is in the lib directory of tomcat, i have added it and to the lib folder of the webapplication of the file upload servlet that i am writing, i have opened servlet-api.jar and inside everything looks ok, there is HttpServletRequest.Then i checked for commons-fileupload-1.2.1.jar and commons-io-1.4.jar that i downloaded from apache file upload api site - these jars are also in my classpath, they are in the lib directory of Tomcat, finally i added them to the lib folder of the web application - nothing changed. The web.xml file in WEB-INF is in my opinion ok,similar to this in every other web application that i have done.The Tomcat version that i am using is 6.0.18, the file upload api from apache site are the last versions.Btw HttpServletRequest is found in a web application where i don use apache file upload api, i mean i can take parameters from forms,etc, it's ok in my opinion. Please give your opinions,no matter what they are.

Does a minimalistic servlet example work in your case i.e. one which just outputs 'Hello world' to console? How are you deploying your applications? What is the directory structure of your web application? Are you overriding any classpath settings? Does your environment variable contain any entry other than JAVA_HOME?

Without knowing the answers to these questions, troubleshooting would be a bit difficult.

This article has been dead for over six months. Start a new discussion instead.