Servlets 如何解决这个java.lang.NoClassDefFoundError:org/apache/commons/io/output/DeferredFileOutputStream?

Servlets 如何解决这个java.lang.NoClassDefFoundError:org/apache/commons/io/output/DeferredFileOutputStream?,servlets,upload,tomcat5.5,Servlets,Upload,Tomcat5.5,我正在使用下面的代码将一个文件上传到tomcat5.5,它给了我以下异常 java.lang.NoClassDefFoundError:org/apache/commons/io/output/DeferredFileOutputStream 你能帮我查一下吗 import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servle

我正在使用下面的代码将一个文件上传到tomcat5.5,它给了我以下异常

java.lang.NoClassDefFoundError:org/apache/commons/io/output/DeferredFileOutputStream

你能帮我查一下吗

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class FileUploadServlet
 */
public class FileUploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public FileUploadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @SuppressWarnings("rawtypes")
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        System.out.println("Status : "+isMultipart);
        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();
            }
        }
        response.sendRedirect("upload.jsp");
    }

}

这是我使用的jar
commons-fileupload-1.2.2.jar

特殊的异常消息告诉您,类路径中缺少所提到的类。正如
org.apache.commons.io
包名所提示的,所提到的类是项目的一部分

事实上,Commons FileUpload将Commons IO作为一个组件。您还需要在
/WEB-INF/lib
中添加和删除
commons io.jar

另见:
使用maven依赖项

<dependency> 
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-io</artifactId> 
  <version>1.3.2</version> 
</dependency> 

org.apache.commons
公地io
1.3.2 

或者将commons-io.1.3.2.jar下载到您的lib文件夹中

只需将所有ApacheComons jar和文件上传jar放在tomcat解决方案的lib文件夹中

即可

默认情况下,Struts使用Apache“commons io.jar”进行文件上传过程。要修复此问题,必须将此库包含到项目依赖项库文件夹中

  • 直接获取
  • 从官方网站获取“commons io.jar”——

  • 从Maven获得
  • 首选的方法是从Maven存储库获取“commons io.jar”

    文件:pom.xml

      <dependency>
        <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
        <version>1.4</version>
      </dependency>
    
    
    公地io
    公地io
    1.4
    
    如果您在WebSphere容器中收到此错误,请确保正确设置应用程序类加载策略。我不得不将我的WAR策略从默认设置更改为“parent last”和“Single class loader for application”。这是因为在我的例子中,commons io*.jar是在应用程序中打包的,所以必须先加载它。

    您必须从这里下载文件并选择

    现在,接下来将下载的文件添加到项目中:


    右键单击您的项目->构建路径->配置构建路径->

    即使它没有回答问题,它也回答了我的问题,谷歌把我带到了这里。所以谢谢:)它实际上不包含在commons io 1.3.2版中。。尝试2.4或更高版本。在maven回购协议中,它不再位于“org.apache”之下,而是直接位于“commons io:commons io”之下