简体   繁体   English

java.lang.ClassNotFoundException:org.apache.commons.fileupload.FileItemFactory

[英]java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory

I am trying to compile UploadServlet servlet via apache tomcat 8.0.14. 我正在尝试通过apache tomcat 8.0.14编译UploadServlet servlet。 my web.xml is fine and UploadServlet.java has compiled properly but yet when try to run i get following error 我的web.xml很好, UploadServlet.java已正确编译,但是尝试运行时出现以下错误

ERROR 错误

HTTP Status 500 - Error instantiating servlet class UploadServlet

exception
javax.servlet.ServletException: Error instantiating servlet class UploadServlet

root cause
java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory

root cause
java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory

UploadServlet.java UploadServlet.java

import java.io.*;
import java.util.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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;
import org.apache.commons.io.output.*;

public class UploadServlet extends HttpServlet {

   private boolean isMultipart;
   private String filePath;
   private int maxFileSize = 50 * 1024;
   private int maxMemSize = 4 * 1024;
   private File file ;

   public void init( ){

      filePath =  getServletContext().getInitParameter("file-upload"); 
   }
   public void doPost(HttpServletRequest request, 
               HttpServletResponse response)
              throws ServletException, java.io.IOException {

      isMultipart = ServletFileUpload.isMultipartContent(request);
      response.setContentType("text/html");
      java.io.PrintWriter out = response.getWriter( );
      if( !isMultipart ){
         out.println("<html>");
         out.println("<head>");
         out.println("<title>Servlet upload</title>");  
         out.println("</head>");
         out.println("<body>");
         out.println("<p>No file uploaded</p>"); 
         out.println("</body>");
         out.println("</html>");
         return;
      }

      DiskFileItemFactory factory = new DiskFileItemFactory();

      factory.setSizeThreshold(maxMemSize);

      factory.setRepository(new File("C:\\apache-tomcat-8.0.14\\temp"));


      ServletFileUpload upload = new ServletFileUpload(factory);

      upload.setSizeMax( maxFileSize );

      try{ 

      List fileItems = upload.parseRequest(request);

      Iterator i = fileItems.iterator();

      out.println("<html>");
      out.println("<head>");
      out.println("<title>Servlet upload</title>");  
      out.println("</head>");
      out.println("<body>");
      while ( i.hasNext () ) 
      {
         FileItem fi = (FileItem)i.next();
         if ( !fi.isFormField () )  
         {

            String fieldName = fi.getFieldName();
            String fileName = fi.getName();
            String contentType = fi.getContentType();
            boolean isInMemory = fi.isInMemory();
            long sizeInBytes = fi.getSize();
            System.out.println(fileName);

            if( fileName.lastIndexOf("\\") >= 0 )
            {
               file = new File( filePath + 
               fileName.substring(fileName.lastIndexOf("\\"))) ;
            }
            else
            {
               file = new File( filePath + 
               fileName.substring(fileName.lastIndexOf("\\")+1)) ;
            }
            fi.write( file ) ;
            out.println("Uploaded Filename: " + fileName + "<br>");
         }
      }
      out.println("</body>");
      out.println("</html>");
   }catch(Exception ex) {
       System.out.println(ex);
   }
   }
   public void doGet(HttpServletRequest request, 
                       HttpServletResponse response)
        throws ServletException, java.io.IOException {

        throw new ServletException("GET method used with " +
                getClass( ).getName( )+": POST method required.");
   } 
}

You need to put commons-fileupload.jar and commons-io.jar to your WEB-INF/lib forlder. 您需要将commons-fileupload.jarcommons-io.jar放入WEB-INF/lib forlder中。 Classpath is used during build, but they must be available during runtime also. 类路径在构建期间使用,但它们在运行时也必须可用。

Actually, if you had used IDE for Java EE development (like Eclipse) putting these Jars to WEB-INF/lib would be enough, as they would be automatically visible in the claspath for build. 实际上,如果您使用IDE进行Java EE开发(例如Eclipse),将这些Jars放到WEB-INF/lib就足够了,因为它们会在claspath中自动可见以进行构建。

as @Gas , I forgot to add .jar file in WEB-INF/lib folder. 作为@Gas,我忘记在WEB-INF / lib文件夹中添加.jar文件。 and then the issue resolved. 然后问题解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到Maven org.apache.commons.fileupload.FileItemFactory类 - Maven org.apache.commons.fileupload.FileItemFactory class not found java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory - java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory java.lang.NoClassDefFoundError:java.lang.Class.getDeclaredConstructors0上的org / apache / commons / fileupload / FileItemFactory(本机方法) - java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory at java.lang.Class.getDeclaredConstructors0(Native Method) 春季如何解决多部分分解器? java.lang.NoClassDefFoundError:org / apache / commons / fileupload / FileItemFactory - How to solve multipart resolver in spring?? java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory org.apache.commons.lang.SerializationException:java.lang.ClassNotFoundException - org.apache.commons.lang.SerializationException: java.lang.ClassNotFoundException java.lang.ClassNotFoundException:org.apache.commons.dbcp.BasicDataSource - java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource java.lang.ClassNotFoundException:org.apache.commons.collections4.ListValuedMap - java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap java.lang.ClassNotFoundException:org.apache.commons.dbcp2.datasources - java.lang.ClassNotFoundException: org.apache.commons.dbcp2.datasources java.lang.ClassNotFoundException:org.apache.commons.logging.LogFactory - java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory java.lang.ClassNotFoundException:org.apache.commons.text.WordUtils - java.lang.ClassNotFoundException: org.apache.commons.text.WordUtils
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM