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
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.
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.
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;
you should have commons-fileupload.jar and commons-io.jar files in /WEB-INF/lib
精彩评论