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

查看:38
本文介绍了java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 apache tomcat 8.0.14 编译 UploadServlet servlet.我的 web.xml 很好,UploadServlet.java 已正确编译,但是当我尝试运行时出现以下错误

I am trying to compile UploadServlet servlet via apache tomcat 8.0.14. my web.xml is fine andUploadServlet.java has compiled properly but yet when try to run i get following 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

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.");
   } 
}

推荐答案

你需要把 commons-fileupload.jarcommons-io.jar 放到你的WEB-INF/lib 文件夹.类路径在构建期间使用,但它们也必须在运行时可用.

You need to put commons-fileupload.jar and commons-io.jar to your WEB-INF/lib forlder. Classpath is used during build, but they must be available during runtime also.

实际上,如果您使用 IDE 进行 Java EE 开发(如 Eclipse),将这些 Jars 放在 WEB-INF/lib 中就足够了,因为它们会在构建的 claspath 中自动可见.

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.

这篇关于java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆