【问题标题】:java.lang.NoClassDefFoundError - org.apache.commons, tomcat 7java.lang.NoClassDefFoundError - org.apache.commons,tomcat 7
【发布时间】:2013-02-11 14:40:39
【问题描述】:

我正在尝试在基于 org.apache.commons.fileupload 的 tomcat 7 中运行 servlet 文件上传示例

我使用此命令在 CMD (Win 7 64bit) 中编译了 servlet 类文件。

 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)

然后最后在谷歌上搜索,找到了一些解决方案,我一个一个地跟着他们。

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 添加到 classpath 但没有运气。

请告诉我出了什么问题。

我的代码

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();
            }
        }
    }
}

【问题讨论】:

标签: java class jsp servlets file-upload


【解决方案1】:

您缺少 javax.servlet.http.HttpServletRequest,它存在于 servlet-api.jar 中。这就是您需要在部署的解决方案中包含的内容。

查看findjar.com,它将告诉您哪些 jar 包含给定的类。它不能帮助您解决所需的版本号,但它会为您指明正确的方向。

【讨论】:

  • 但是另一个 servlet Form 示例工作正常,它没有文件上传功能,但包含 import javax.servlet.http.*;
猜你喜欢
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
  • 2012-02-14
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
相关资源
最近更新 更多