【问题标题】:java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactoryjava.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
【发布时间】:2011-07-07 05:13:08
【问题描述】:

我正在尝试使用 maven 在 Spring MVC 中上传文件的简单示例,我遵循了这个 tutorial

但是我收到了这个错误

java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory

我还在 pom.xml 中包含了依赖项

<!-- Apache Commons Upload --> 
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>

也在 dispatcher-servlet.xml 中

<!-- Configure the multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean> 

那么,你能帮我解决我哪里出错了吗?

提前致谢。

【问题讨论】:

    标签: java spring-mvc maven


    【解决方案1】:

    您需要添加commons-fileupload

    将此添加到您的 POM

    <dependency>
       <groupId>commons-fileupload</groupId>
       <artifactId>commons-fileupload</artifactId>
       <version>1.2.1</version> <!-- makesure correct version here -->
    </dependency>
    

    【讨论】:

    • 我没有读过印度玫瑰的教程,但它可能在某处使用它。做一件事只是删除它并检查异常或编译错误。并且随时欢迎您(也将答案标记为已接受:))
    • 我有一个类似的 tomcat 7 启动问题,通过准备好 Eclipse 控制台日志,我发现我缺少 Primefaces 文件上传所需的一些 jar。添加这些 jar 即 commons-io 和 commons-fileupload 后,我摆脱了 tomcat 启动问题... :)
    • 请注意,这些依赖项必须在最高的人工制品类加载器上进行(EE 类型,这是您的 EAR,而不是您的 WAR)。
    • 最好将范围缩小到运行时。 &lt;scope&gt;runtime&lt;/scope&gt;
    【解决方案2】:

    为了安全使用1.4版:

    <dependency>
       <groupId>commons-fileupload</groupId>
       <artifactId>commons-fileupload</artifactId>
       <version>1.4</version>
    </dependency>
    

    检查:https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload

    【讨论】: