无法在Spring4中配置CommonsMultipartResolver

时间:2017-02-15 15:27:49

标签: java spring

我在applicationContext.xml中配置了CommonsMultipartResolver,如下所示:

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

但它会引发异常,如下所示:

15-Feb-2017 22:17:35.049 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
15-Feb-2017 22:17:35.107 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization started
15-Feb-2017 22:17:35.172 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing Root WebApplicationContext: startup date [Wed Feb 15 22:17:35 CST 2017]; root of context hierarchy
15-Feb-2017 22:17:35.240 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
15-Feb-2017 22:17:35.563 WARNING [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multipartResolver' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.commons.CommonsMultipartResolver]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
15-Feb-2017 22:17:35.575 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multipartResolver' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.commons.CommonsMultipartResolver]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    ...

我不知道为什么CommonsMultipartResolver无法实例化。我可以在spring-web-4.3.6.RELEASE.jar中找到CommonsMultipartResolver。 但抛出了这个异常:

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

我的配置有什么问题?(我使用的是Intellij IDEA)

更新
这是我的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>
</beans>

我尝试通过将commons-fileupload添加到IDEA项目设置来解决问题 - &gt;库。但它确实没有帮助,问题仍然存在 project structure setting of IDEA

project structure setting of IDEA

2 个答案:

答案 0 :(得分:4)

下载commons-fileupload并将其保存在容器的modules文件夹中。显然,类org.springframework.web.multipart.commons.CommonsMultipartResolverorg.apache.commons.fileupload.FileItemFactory具有硬依赖性,function findSolution(target) { function find(start, history) { // <--- NOTICE DECLARATIONS HERE /* SNIP */ return find(start + 5, "(" + history + " + 5)") || find(start * 3, "(" + history + " * 3)"); } // ^--- FUNCTION CALLS HERE return find(1, "1"); // <--- AND HERE } console.log(findSolution(24)); 是上面jar中包含的类。容器类加载器加载此类后,您将不会收到此错误。

答案 1 :(得分:0)

如果您正在使用maven,请尝试


import pandas as pd

pandas_data_frame = pd.read_csv('path_to_csv_file')

pandas_data_frame.groupby(['ProductID']).sum()