Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring启动+文件上载不起作用_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

Spring启动+文件上载不起作用

Spring启动+文件上载不起作用,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,我最近在我的应用程序中启动了Spring Boot。 在使用tomcat8时,有一个文件上传实用程序可以正常工作,但在切换到Spring boot之后,它就不工作了 下面是代码 JSP Spring启动配置: @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, MultipartAutoConfiguration.class }) @Configuration @ComponentScan @Impo

我最近在我的应用程序中启动了Spring Boot。 在使用tomcat8时,有一个文件上传实用程序可以正常工作,但在切换到Spring boot之后,它就不工作了

下面是代码

JSP

Spring启动配置:

@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, MultipartAutoConfiguration.class })
@Configuration  
@ComponentScan
@ImportResource(value = "classpath:/context.xml")
public class MyApplication {

public MyApplication() {
    super();
}

public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
}
}
我的Mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="messageSource"     class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages/messages"/>
</bean>

<context:component-scan base-package="com.my">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
    <mvc:message-converters register-defaults="true" >
        <ref bean="jsonHttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

<mvc:resources location="/css/**" mapping="/css/" />
<mvc:resources location="/js/**" mapping="/js/" />
<mvc:resources location="/img/**" mapping="/img/" />
<mvc:resources location="/fonts/**" mapping="/fonts/" />

<mvc:default-servlet-handler/>

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.Exception">error</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="0"></property>
</bean>

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="-1" />    
</bean>

<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="prefixJson" value="false" />
    <property name="supportedMediaTypes" value="application/json" />
    <property name="objectMapper" ref="objectMapper"/> 
</bean>
我已经在我的启动应用程序类上尝试了@SpringBootApplication,但是没有用。我正在使用嵌入式tomcat

我搜索了各种各样的博客等问题,但都没能解决。
请提供帮助。

Spring启动和文件上载工作正常。文件是可用的。在默认配置下,它的多部分文件上传是开箱即用的


从您发布的代码来看,我认为您没有正确配置Spring Boot。开始例如,我看到MyApplication中没有@SpringBootApplication注释,该注释触发Spring Boot来实现标准配置。

他有@EnableAutoConfiguration、@ComponentScan和@configuration,其中@SpringBootApplication只是一个较短的版本。对。。。这让我怀疑他是否正确配置了Spring Boot。常见的问题解决策略是从已知的良好配置开始——仅@SpringBootApplication——确认功能,然后添加变量。添加@SpringBootApplication不会改变任何事情,它只是3个注释的简写。它不会改变任何东西。不,我的建议是删除所有其他注释,并按照我提供给Spring Boot教程的链接中指定的@SpringBootApplication开始。这将使您处于已知的良好状态,从该状态可以确认文件上载至少正常工作,然后根据需要开始更改配置。无论如何,这就是你应该如何使用Spring Boot的。从基本配置开始,直到基本配置不适合为止,此时您开始调整配置。这同样不会改变任何内容,因为这只不过是他已有的3个注释而已。。。另外,你所说的Spring Boot对于一个新的应用程序来说也是正确的。他正在移植一个完全不同的应用程序……你是在运行嵌入式tomcat还是仍在部署?
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, MultipartAutoConfiguration.class })
@Configuration  
@ComponentScan
@ImportResource(value = "classpath:/context.xml")
public class MyApplication {

public MyApplication() {
    super();
}

public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="messageSource"     class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages/messages"/>
</bean>

<context:component-scan base-package="com.my">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
    <mvc:message-converters register-defaults="true" >
        <ref bean="jsonHttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

<mvc:resources location="/css/**" mapping="/css/" />
<mvc:resources location="/js/**" mapping="/js/" />
<mvc:resources location="/img/**" mapping="/img/" />
<mvc:resources location="/fonts/**" mapping="/fonts/" />

<mvc:default-servlet-handler/>

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.Exception">error</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="0"></property>
</bean>

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="-1" />    
</bean>

<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="prefixJson" value="false" />
    <property name="supportedMediaTypes" value="application/json" />
    <property name="objectMapper" ref="objectMapper"/> 
</bean>
org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'files' is not present
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:251)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:94)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)