Results 1 to 4 of 4
  1. #1
    jeniramires is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Default File Upload Servlet problem

    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.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Thumbs up simpler than it looks

    Quote Originally Posted by jeniramires View Post
    ...For me it is quite strange, i have been dealing with this problem for 2 days and can't figure the problem......
    I wrestled with this for long time too , simple is that there is some sort of remarkably simple approach which revolves around just climibing back along the inheritace tree for servlet - there is some file object back there by which you can just read whatever they ship minus all the folks who tell you it cannot be done. Swamped right now with writing files server-side, will try to check back.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    jeniramires is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Default

    Hi,thanks for the help,i probably would try to follow the approach you proposed, but it happened that little before i read it i think i solved the problem(btw it really was simpler than it looks).After a couple of unsuccessful tries to understand the what caused these exceptions, i downloaded others API for uploading files, this time from Oreilly.When i wrote one simple file upload application with them, on the first it worked ok.When then i tried it again, it (surprisingly ) stopped to work, with exception like this :java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest . Then again and again it was causing the same exception, when i started it with Mozilla,IE and Opera.It made me think that the problem is not in the API,but in tomcat, then from the tool Monitor Tomcat in the tab JAVA there is a field to choose the the java virtual machine, and it was jre1.5.0_10\bin\client\jvm.dll
    so i decided to try with jre6\bin\client\jvm.dll and this seems solved the problem (i hope for this), because after starting round 10 times the simple file upload application, it works excellent:).

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Post okay

    Okay, I came here because I found this:

    JUpload - File Upload Applet

    I am trying to figure out how to write server-only files. If you can be of any help - post in [SOLVED] writing to WEB-INF while on server. which I marked as solved but that was just because I had to many things open. Yesterday I managed to get throwaway AES grade unique request identifiers and today I have come back around to that post. First attempt at running the code proposed in the last post there has showing up in the filename and regex.find("%20").replace(" ") failed , I am trying to employ work from Brian D. Carlstrom and my hopes are back up. I think the apporach that will work is simplifiy to File f = new File("."); but I need any assistance or remarks you can provide as I have a google adwords ready to go from beginner to standard waiting on one sole remaining barrier: Saving the text from a <form><textarea><submit> to a a hardcode file as I understand files and do it on the server so that I have an avenue of retireving what the user actually typed instead of getting some fancy tools' scripting popup asking my if I am satisifed with the screwing I got.

    Really inane, with promotional claims ranging as high as eight million dollars a second going on the internet it is needful that I have a code avenue I understand for records of what was sent to Pay Pal and what they sent me back along with what the customer actually typed at the keyboard.

    pm me if you want to trade emails or something.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. how to upload a file?
    By tommy in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 06-30-2008, 02:50 PM
  2. file upload
    By sundarjothi in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-27-2008, 11:52 AM
  3. file upload in java
    By muralikrishna in forum New To Java
    Replies: 6
    Last Post: 06-25-2008, 08:24 AM
  4. File Upload
    By ShoeNinja in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-09-2007, 10:09 PM
  5. how to upload a file using servlet
    By simon in forum Java Servlet
    Replies: 1
    Last Post: 08-05-2007, 03:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •