8

我正在使用 apache FileUpload 来处理文件上传。我将它与码头一起使用。servlet 看到多部分请求,但在执行时抛出 NoClassDefFoundError 异常:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException 
{
  boolean isMultipart = ServletFileUpload.isMultipartContent(req);
  if (isMultipart) {
    try {
      FileItemFactory factory = new DiskFileItemFactory();

      ServletFileUpload upload = new ServletFileUpload(factory);

      List items = upload.parseRequest(req); //  exception
      ...

抛出:

java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
   at org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:199)

造成的:

java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream

除了 commons-fileupload-1.2.2.jar 之外,我们还需要包含一个额外的 jar 吗?

我的目标是只上传一个文件并将其写入磁盘。

谢谢

4

2 回答 2

14

您需要将CommonsIO添加到类路径中。Commons File Upload 依赖于它。

于 2011-03-17T16:43:51.293 回答
4

添加以下依赖项

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
于 2015-09-16T21:15:53.890 回答