2013-02-11 72 views
-1

我試圖運行在Tomcat中7一個servlet文件上傳例子是基於org.apache.commons.fileuploadjava.lang.NoClassDefFoundError - org.apache.commons下,Tomcat 7

我遵守Servlet類文件中CMD(Win 7 64bit)通過使用這個命令。

C:\Users\Preet\Desktop>javac -cp .;E:/servlet-api.jar;"C:\Program Files\Apache S 
oftware Foundation\Tomcat 7.0\webapps\sim\WEB-INF\lib\commons-fileupload-1.2.2.j 
ar" "C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\sim\WEB-INF\ 
classes\SimpleServlets.java" 

一切都很好。

編譯我想我的例子後,我得到了一個錯誤,是

exception 

javax.servlet.ServletException: Servlet execution threw an exception 
root cause 

java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest 
    org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68) 
    SimpleServlets.doPost(SimpleServlets.java:21) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 

然後在最後的google搜索,發現了幾個解決方案,我跟着他們一個接一個。

1.在WEB-INF中創建一個lib文件夾並複製commons-fileupload-1.2.2和commons-io-2.2。 但沒有運氣。

2.在tomcat/lib中打開commons-fileupload-1.2.2和commons-io-2.2。 但沒有運氣。

3.將commons-fileupload-1.2.2和commons-io-2.2添加到classpaath中,但沒有運氣。

請告訴我什麼是錯的。

我的代碼

import org.apache.commons.fileupload.servlet.ServletFileUpload; 
import org.apache.commons.fileupload.FileItemFactory; 
import org.apache.commons.fileupload.FileUploadException; 
import org.apache.commons.fileupload.FileItem; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory; 

import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.ServletException; 
import java.io.IOException; 
import java.io.File; 
import java.util.List; 
import java.util.Iterator; 

public class SimpleServlets extends HttpServlet { 
    private static final long serialVersionUID = -3208409086358916855L; 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     boolean isMultipart = ServletFileUpload.isMultipartContent(request); 

     if (isMultipart) { 
      FileItemFactory factory = new DiskFileItemFactory(); 
      ServletFileUpload upload = new ServletFileUpload(factory); 

      try { 
       List items = upload.parseRequest(request); 
       Iterator iterator = items.iterator(); 
       while (iterator.hasNext()) { 
        FileItem item = (FileItem) iterator.next(); 

        if (!item.isFormField()) { 
         String fileName = item.getName(); 

         String root = getServletContext().getRealPath("/"); 
         File path = new File(root + "/uploads"); 
         if (!path.exists()) { 
          boolean status = path.mkdirs(); 
         } 

         File uploadedFile = new File(path + "/" + fileName); 
         System.out.println(uploadedFile.getAbsolutePath()); 
         item.write(uploadedFile); 
        } 
       } 
      } catch (FileUploadException e) { 
       e.printStackTrace(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

的可能重複的[java.lang.NoClassDefFoundError:的javax/servlet的/ HTTP/HttpServletRequest的](http://stackoverflow.com/questions/8404791/java-lang-noclassdeffounderror-javax-servlet-http-httpservletrequest) – 2013-02-11 14:58:31

回答

1

你錯過javax.servlet.http.HttpServletRequest,這是目前在servlet-api.jar文件。這是您需要在部署的解決方案中包含的一個。

退房findjar.com,它會告訴你哪些罐子包含給定的類。它不能幫助您解決所需的版本號,但它會指向正確的方向。

+0

如何我可以修好它? – 2013-02-11 14:47:38

+0

uf,抱歉沒有幫助,鏈接包含很多文件 – 2013-02-11 14:52:34

+0

但是另一個servlet表單示例工作正常,沒有文件上傳功能,但包含import javax.servlet.http。*; – 2013-02-11 14:54:48