4

I want to uploal a doc file to the servlet using commons-fileupload-1.2.2.

I'm using this code in the front end:

<form action="fileuploader" method="post" enctype="multipart/form-data">
<br>File : <input type="file" name="uploadedFile">
<br><input type="submit">

and using this code in the servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
}

but system gives me a this error

SEVERE: Servlet.service() for servlet FileUploaderServlet threw exception
java.lang.ClassNotFoundException: org.apache.commons.fileupload.servlet.ServletFileUpload
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at org.jspFileUploader.fileUploader.FileUploaderServlet.doPost(FileUploaderServlet.java:31)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)

I think problem is in this line:

boolean isMultipart = ServletFileUpload.isMultipartContent(request);

Help me with this please

beatgammit
  • 19,817
  • 19
  • 86
  • 129
Lakshitha Herath
  • 628
  • 2
  • 9
  • 18

4 Answers4

8

You need to drop the commons-fileupload.jar and commons-io.jar files in /WEB-INF/lib folder of your webapp project. This folder becomes ultimately part of webapp's runtime classpath. Note that in a bit decent IDE like Eclipse/Netbeans/IntelliJ, you do not need to fiddle with buildpath properties afterwards. The IDE is perfectly aware that libraries in /WEB-INF/lib are to be taken part of the runtime classpath, so it adds that to the build path automagically.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
3

No, the problem is that (Tomcat?) can't find "org.apache.commons.fileupload.servlet.ServletFileUpload".

Make sure you've installed the correct .jar, make sure you've configured your server and/or web app correctly.

You haven't provided any details about exactly how you're running "servlets" (Tomcat? Jboss? Something else entirely?), so we can't guide you any further than "The problem is server configuration on your part!"

ADDENDUM: Copy the .jar file to your Tomcat lib directory, restart Tomcat, and try again. That should resolve the "class not found" error.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • i'm using Tomcat 6 and commons-fileupload-1.2.3.jar and i added that to the Libraries in "Java build path".And i imported that jar in the servlet like this import org.apache.commons.fileupload.*; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.disk.DiskFileItemFactory; these imports doesn't give any error.Do i need to do another configurations? – Lakshitha Herath Sep 11 '11 at 06:11
  • When you say "added that to the libraries in 'Java build path'", I think you're talking about your IDE (for example, about Eclipse). YOUR IDE CONFIGURATION != YOUR TOMCAT CONFIGURATION!!!! You need to copy the .jar to Tomcat. Your IDE might help automate these tasks but I would encourage you to learn how to configure Tomcat manually, independent of the IDE. In your case, just copy "commons-fileupload-1.2.3.jar to $CATALINA_BASE/conf – paulsm4 Sep 11 '11 at 06:29
  • 3
    @paulsm4 In general the lib should be copied to `WEB-INF/lib` instead of `tomcat/lib`. Nevertheless, +1. This seems to be the problem... – home Sep 11 '11 at 10:56
2

Apart from adding those libs to WEB-INFO/lib, I had to change the imports from my servlet to use those certain libs, from:

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

to

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
Ima
  • 680
  • 1
  • 15
  • 28
  • 1
    You seem to be using Tomcat 7. You don't need to use commons fileupload at all. Just use the new Servlet 3.0 builtin `request.getPart()` method instead. See also http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824 – BalusC Feb 15 '12 at 16:52
-1

you should have commons-fileupload.jar and commons-io.jar files in /WEB-INF/lib

Saurabh
  • 21
  • 5