在Spring 4 MVC上传文件java.lang.NoClassDefFoundError:org / apache / commons / fileupload / FileItemFactory

时间:2022-11-05 15:36:55

I try to write a file upload program in Spring 4 MVC. But the following exception is constantly thrown.

我尝试在Spring 4 MVC中编写一个文件上传程序。但是不断抛出以下异常。

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

I reviewed all the questions related to this issue and claim that that I made none of their mistakes.

我回顾了与此问题相关的所有问题,并声称我没有犯错误。

I downloaded the commons-fileupload:1.3.1 library from maven and added the following line to my .iml file

我从maven下载了commons-fileupload:1.3.1库,并将以下行添加到我的.iml文件中

<orderEntry type="library" name="commons-fileupload:commons-fileupload:1.3.1" level="project" />

This bean is added to spring-mvc-servlet.xml

这个bean被添加到spring-mvc-servlet.xml中

<bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- setting maximum upload size -->
    <property name="maxUploadSize" value="100000" />

</bean>

Moreover I noticed that the class in the library that can not be found is crossed out(see the picture below)在Spring 4 MVC上传文件java.lang.NoClassDefFoundError:org / apache / commons / fileupload / FileItemFactory

此外,我注意到库中无法找到的类被划掉(见下图)

Could not find out whether this is the main reason of the exception or these these classes are nevertheless available.

无法确定这是否是例外的主要原因或这些类仍然可用。

3 个解决方案

#1


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

the NoClassDefFoundError exception says that FileItemFactory class found but a exception is thrown while creating instance, this type of problems occurs when duplicate dependency jar files in classpath OR version mismatch.

NoClassDefFoundError异常表示发现了FileItemFactory类但在创建实例时抛出了异常,当类路径或版本不匹配时重复的依赖jar文件时会发生此类问题。

#2


I had the same issue and just solved it. 1) make sure commons-fileupload and commons-io both are listed as your dependencies. 2) make sure no other dependency overrides the functionality. For example Spring by default uses Servlet-api 3 upload mechanism... remove that from your project module dependencies

我有同样的问题,只是解决了它。 1)确保将commons-fileupload和commons-io列为依赖项。 2)确保没有其他依赖项覆盖该功能。例如,Spring默认使用Servlet-api 3上传机制......从项目模块依赖项中删除它

3) The final point that was the key solution for me is , make sure both dependencies are added to tomcat deployment. I am not able to post screen shots so I will give you the path to double check if jar are there or nor. in Itellij Idea Project Structure --> Artifacts --> compare Available Elements with output layout.

3)对我来说关键解决方案的最后一点是,确保将两个依赖项添加到tomcat部署中。我无法发布屏幕截图,所以我会给你一条双重检查罐子是否存在的路径。在Itellij Idea项目结构 - >工件 - >将可用元素与输出布局进行比较。

Hope this will help.

希望这会有所帮助。

#3


I've had the same issue, in my case I discovered that commons-io dependency was in conflict, after resolve it, everything works great!

我有同样的问题,在我的情况下,我发现公共 - 依赖是冲突,解决后,一切都很好!

To discover conflicts, I've run:

为了发现冲突,我跑了:

mvn dependency:tree -Dverbose -Dincludes=commons-io

mvn依赖:tree -Dverbose -Dincludes = commons-io

The result was:

结果是:

[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ smartplan ---
[INFO] com.claro:smartplan:war:1.15.0
[INFO] +- commons-fileupload:commons-fileupload:jar:1.3.1:compile
[INFO] |  \- commons-io:commons-io:jar:2.2:compile
[INFO] \- org.hibernate:hibernate-search-orm:jar:4.5.1.Final:test
[INFO]    \- org.hibernate:hibernate-search-analyzers:jar:4.5.1.Final:test
[INFO]       \- org.apache.solr:solr-analysis-extras:jar:3.6.2:test
[INFO]          \- org.apache.solr:solr-core:jar:3.6.2:test
[INFO]             +- org.apache.solr:solr-solrj:jar:3.6.2:test
[INFO]             |  \- (commons-io:commons-io:jar:2.1:test - omitted for conflict with 2.2)
[INFO]             \- (commons-io:commons-io:jar:2.1:test - omitted for conflict with 2.2)
[INFO] ------------------------------------------------------------------------

After that, I removed conflict dependency on pom.xml:

之后,我删除了对pom.xml的冲突依赖:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-search-orm</artifactId>
    <version>4.5.1.Final</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </exclusion>
    </exclusions>
</dependency>

#1


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

the NoClassDefFoundError exception says that FileItemFactory class found but a exception is thrown while creating instance, this type of problems occurs when duplicate dependency jar files in classpath OR version mismatch.

NoClassDefFoundError异常表示发现了FileItemFactory类但在创建实例时抛出了异常,当类路径或版本不匹配时重复的依赖jar文件时会发生此类问题。

#2


I had the same issue and just solved it. 1) make sure commons-fileupload and commons-io both are listed as your dependencies. 2) make sure no other dependency overrides the functionality. For example Spring by default uses Servlet-api 3 upload mechanism... remove that from your project module dependencies

我有同样的问题,只是解决了它。 1)确保将commons-fileupload和commons-io列为依赖项。 2)确保没有其他依赖项覆盖该功能。例如,Spring默认使用Servlet-api 3上传机制......从项目模块依赖项中删除它

3) The final point that was the key solution for me is , make sure both dependencies are added to tomcat deployment. I am not able to post screen shots so I will give you the path to double check if jar are there or nor. in Itellij Idea Project Structure --> Artifacts --> compare Available Elements with output layout.

3)对我来说关键解决方案的最后一点是,确保将两个依赖项添加到tomcat部署中。我无法发布屏幕截图,所以我会给你一条双重检查罐子是否存在的路径。在Itellij Idea项目结构 - >工件 - >将可用元素与输出布局进行比较。

Hope this will help.

希望这会有所帮助。

#3


I've had the same issue, in my case I discovered that commons-io dependency was in conflict, after resolve it, everything works great!

我有同样的问题,在我的情况下,我发现公共 - 依赖是冲突,解决后,一切都很好!

To discover conflicts, I've run:

为了发现冲突,我跑了:

mvn dependency:tree -Dverbose -Dincludes=commons-io

mvn依赖:tree -Dverbose -Dincludes = commons-io

The result was:

结果是:

[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ smartplan ---
[INFO] com.claro:smartplan:war:1.15.0
[INFO] +- commons-fileupload:commons-fileupload:jar:1.3.1:compile
[INFO] |  \- commons-io:commons-io:jar:2.2:compile
[INFO] \- org.hibernate:hibernate-search-orm:jar:4.5.1.Final:test
[INFO]    \- org.hibernate:hibernate-search-analyzers:jar:4.5.1.Final:test
[INFO]       \- org.apache.solr:solr-analysis-extras:jar:3.6.2:test
[INFO]          \- org.apache.solr:solr-core:jar:3.6.2:test
[INFO]             +- org.apache.solr:solr-solrj:jar:3.6.2:test
[INFO]             |  \- (commons-io:commons-io:jar:2.1:test - omitted for conflict with 2.2)
[INFO]             \- (commons-io:commons-io:jar:2.1:test - omitted for conflict with 2.2)
[INFO] ------------------------------------------------------------------------

After that, I removed conflict dependency on pom.xml:

之后,我删除了对pom.xml的冲突依赖:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-search-orm</artifactId>
    <version>4.5.1.Final</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </exclusion>
    </exclusions>
</dependency>