当前位置: 首页 > 知识库问答 >
问题:

所需的MultipartFile参数“file”不存在

汪明德
2023-03-14
----------------------------------------------------------------------------
<!DOCTYPE html>

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html lang="en">

<body>
    <br>Message: ${message}
    <h2>List Of Objects</h2>

    <br>
    <h2>Upload New File to this Bucket</h2>
    <form action="uploadObject" method="post" enctype="multipart/form-data">
        <table width="60%" border="1" cellspacing="0">
            <tr>
                <td width="35%"><strong>File to upload</strong></td>
                <td width="65%"><input type="file" name="file" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" name="submit" value="Add" /></td>
            </tr>
        </table>
    </form>
</body>
</html>
@RequestMapping(value = "/uploadObject", method = RequestMethod.POST)
    @Override
    public String upload(@RequestParam("file") MultipartFile file,
            @RequestParam("bucketName") String bucketName,
            Map<String, Object> model) {

        LOG.info("Object name parameter is " + file.getOriginalFilename());
        LOG.info("Bucket name parameter is " + bucketName);
....
}
package com.phi.piranha.client.config;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.MultiPartConfigFactory;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@Configuration
@EnableAutoConfiguration
@ComponentScan("com.phi.piranha")
public class PiranhaStorageServiceClientApplication extends
        SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(
            final SpringApplicationBuilder application) {
        return application
                .sources(PiranhaStorageServiceClientApplication.class);
    }

    @SuppressWarnings("PMD.SignatureDeclareThrowsException")
    public static void main(final String[] args) throws Exception {
        SpringApplication.run(PiranhaStorageServiceClientApplication.class,
                args);
    }

    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultiPartConfigFactory factory = new MultiPartConfigFactory();
        factory.setMaxFileSize("1024KB");
        factory.setMaxRequestSize("1024KB");
        return factory.createMultipartConfig();
    }

    @Bean
    CommonsMultipartResolver multipartResolver(){
        return new CommonsMultipartResolver();
    }
}
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::            (v1.0.0.RC4)

2014-03-28 14:41:01.216  INFO 2852 --- [           main] c.PiranhaStorageServiceClientApplication : Starting PiranhaStorageServiceClientApplication on USDSFOSFC1NBOPY with PID 2852 (C:\a\workspace\sandbox\s3-poc-client\target\classes started by 310152252)
2014-03-28 14:41:01.220 DEBUG 2852 --- [           main] o.s.boot.SpringApplication               : Loading source class com.phi.piranha.client.config.PiranhaStorageServiceClientApplication
2014-03-28 14:41:01.270  INFO 2852 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6f434304: startup date [Fri Mar 28 14:41:01 PDT 2014]; root of context hierarchy
2014-03-28 14:41:01.273 DEBUG 2852 --- [           main] ationConfigEmbeddedWebApplicationContext : Bean factory for org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6f434304: org.springframework.beans.factory.support.DefaultListableBeanFactory@62b08658: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,piranhaStorageServiceClientApplication]; root of factory hierarchy
2014-03-28 14:41:01.897  INFO 2852 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'multipartResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=piranhaStorageServiceClientApplication; factoryMethodName=multipartResolver; initMethodName=null; destroyMethodName=(inferred); defined in class com.phi.piranha.client.config.PiranhaStorageServiceClientApplication] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration; factoryMethodName=multipartResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.class]]
2014-03-28 14:41:02.188 DEBUG 2852 --- [           main] ationConfigEmbeddedWebApplicationContext : Using MessageSource [org.springframework.context.support.ResourceBundleMessageSource: basenames=[messages]]
2014-03-28 14:41:02.189 DEBUG 2852 --- [           main] ationConfigEmbeddedWebApplicationContext : Using ApplicationEventMulticaster [org.springframework.context.event.SimpleApplicationEventMulticaster@58a1c2bf]
2014-03-28 14:41:02.495 DEBUG 2852 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Code archive: C:\Users\310152252\.m2\repository\org\springframework\boot\spring-boot\1.0.0.RC4\spring-boot-1.0.0.RC4.jar
2014-03-28 14:41:02.495 DEBUG 2852 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Code archive: C:\Users\310152252\.m2\repository\org\springframework\boot\spring-boot\1.0.0.RC4\spring-boot-1.0.0.RC4.jar
2014-03-28 14:41:02.496 DEBUG 2852 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Document root: C:\a\workspace\sandbox\s3-poc-client\src\main\webapp
2014-03-28 14:41:02.546  INFO 2852 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 9090
2014-03-28 14:41:02.884  INFO 2852 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2014-03-28 14:41:02.885  INFO 2852 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-03-28 14:41:03.077  INFO 2852 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-03-28 14:41:03.077  INFO 2852 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1811 ms
2014-03-28 14:41:03.810  INFO 2852 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2014-03-28 14:41:03.813  INFO 2852 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2014-03-28 14:41:04.356  INFO 2852 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-03-28 14:41:04.483  INFO 2852 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-03-28 14:41:04.483  INFO 2852 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-03-28 14:41:04.744  INFO 2852 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2014-03-28 14:41:04.763 DEBUG 2852 --- [           main] ationConfigEmbeddedWebApplicationContext : Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@11a91abc]
2014-03-28 14:41:04.765 DEBUG 2852 --- [           main] utoConfigurationReportLoggingInitializer : 


=========================
AUTO-CONFIGURATION REPORT
=========================


Positive matches:
-----------------

   MessageSourceAutoConfiguration
      - @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: all) found no beans (OnBeanCondition)

   PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer
      - @ConditionalOnMissingBean (types: org.springframework.context.support.PropertySourcesPlaceholderConfigurer; SearchStrategy: current) found no beans (OnBeanCondition)

   JmxAutoConfiguration
      - @ConditionalOnClass classes found: org.springframework.jmx.export.MBeanExporter (OnClassCondition)
      - SpEL expression on org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration: ${spring.jmx.enabled:true} (OnExpressionCondition)
      - @ConditionalOnMissingBean (types: org.springframework.jmx.export.MBeanExporter; SearchStrategy: all) found no beans (OnBeanCondition)

   DispatcherServletAutoConfiguration
      - found web application StandardServletEnvironment (OnWebApplicationCondition)
      - @ConditionalOnClass classes found: org.springframework.web.servlet.DispatcherServlet (OnClassCondition)

   DispatcherServletAutoConfiguration.DispatcherServletConfiguration
      - no DispatcherServlet found (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)
      - @ConditionalOnClass classes found: javax.servlet.ServletRegistration (OnClassCondition)

   EmbeddedServletContainerAutoConfiguration
      - found web application StandardServletEnvironment (OnWebApplicationCondition)

   EmbeddedServletContainerAutoConfiguration.EmbeddedTomcat
      - @ConditionalOnClass classes found: javax.servlet.Servlet,org.apache.catalina.startup.Tomcat (OnClassCondition)
      - @ConditionalOnMissingBean (types: org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; SearchStrategy: current) found no beans (OnBeanCondition)

   HttpMessageConvertersAutoConfiguration
      - @ConditionalOnClass classes found: org.springframework.http.converter.HttpMessageConverter (OnClassCondition)

   HttpMessageConvertersAutoConfiguration#messageConverters
      - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.web.HttpMessageConverters; SearchStrategy: all) found no beans (OnBeanCondition)

   HttpMessageConvertersAutoConfiguration.ObjectMappers
      - @ConditionalOnClass classes found: com.fasterxml.jackson.databind.ObjectMapper (OnClassCondition)

   HttpMessageConvertersAutoConfiguration.ObjectMappers#jacksonObjectMapper
      - @ConditionalOnMissingBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) found no beans (OnBeanCondition)

   HttpMessageConvertersAutoConfiguration.ObjectMappers#mappingJackson2HttpMessageConverter
      - @ConditionalOnMissingBean (types: org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; SearchStrategy: all) found no beans (OnBeanCondition)

   MultipartAutoConfiguration
      - @ConditionalOnClass classes found: javax.servlet.Servlet,org.springframework.web.multipart.support.StandardServletMultipartResolver (OnClassCondition)
      - @ConditionalOnBean (types: javax.servlet.MultipartConfigElement; SearchStrategy: all) found the following [multipartConfigElement] (OnBeanCondition)

   ServerPropertiesAutoConfiguration
      - found web application StandardServletEnvironment (OnWebApplicationCondition)

   ServerPropertiesAutoConfiguration#serverProperties
      - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.web.ServerProperties; SearchStrategy: current) found no beans (OnBeanCondition)

   WebMvcAutoConfiguration
      - found web application StandardServletEnvironment (OnWebApplicationCondition)
      - @ConditionalOnClass classes found: javax.servlet.Servlet,org.springframework.web.servlet.DispatcherServlet,org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter (OnClassCondition)
      - @ConditionalOnMissingBean (types: org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; SearchStrategy: all) found no beans (OnBeanCondition)

   WebMvcAutoConfiguration#hiddenHttpMethodFilter
      - @ConditionalOnMissingBean (types: org.springframework.web.filter.HiddenHttpMethodFilter; SearchStrategy: all) found no beans (OnBeanCondition)

   WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#defaultViewResolver
      - @ConditionalOnMissingBean (types: org.springframework.web.servlet.view.InternalResourceViewResolver; SearchStrategy: all) found no beans (OnBeanCondition)

   WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#requestContextListener
      - @ConditionalOnMissingBean (types: org.springframework.web.context.request.RequestContextListener; SearchStrategy: all) found no beans (OnBeanCondition)


Negative matches:
-----------------

   RabbitAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.amqp.rabbit.core.RabbitTemplate,com.rabbitmq.client.Channel (OnClassCondition)

   AopAutoConfiguration
      - required @ConditionalOnClass classes not found: org.aspectj.lang.annotation.Aspect,org.aspectj.lang.reflect.Advice (OnClassCondition)

   BatchAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.batch.core.launch.JobLauncher,org.springframework.jdbc.core.JdbcOperations (OnClassCondition)

   JpaRepositoriesAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.data.jpa.repository.JpaRepository (OnClassCondition)

   MongoRepositoriesAutoConfiguration
      - required @ConditionalOnClass classes not found: com.mongodb.Mongo,org.springframework.data.mongodb.repository.MongoRepository (OnClassCondition)

   MongoTemplateAutoConfiguration
      - required @ConditionalOnClass classes not found: com.mongodb.Mongo,org.springframework.data.mongodb.core.MongoTemplate (OnClassCondition)

   DataSourceAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType (OnClassCondition)

   DataSourceTransactionManagerAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.jdbc.core.JdbcTemplate,org.springframework.transaction.PlatformTransactionManager (OnClassCondition)

   JmsTemplateAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.jms.core.JmsTemplate,javax.jms.ConnectionFactory (OnClassCondition)

   DeviceResolverAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.mobile.device.DeviceResolverHandlerInterceptor,org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver (OnClassCondition)

   MongoAutoConfiguration
      - required @ConditionalOnClass classes not found: com.mongodb.Mongo (OnClassCondition)

   HibernateJpaAutoConfiguration
      - did not find HibernateEntityManager class (HibernateJpaAutoConfiguration.HibernateEntityManagerCondition)

   ReactorAutoConfiguration
      - required @ConditionalOnClass classes not found: reactor.spring.context.config.EnableReactor (OnClassCondition)

   RedisAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.data.redis.connection.lettuce.LettuceConnection,org.springframework.data.redis.core.RedisOperations,com.lambdaworks.redis.RedisClient (OnClassCondition)

   SecurityAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.security.authentication.AuthenticationManager (OnClassCondition)

   ThymeleafAutoConfiguration
      - required @ConditionalOnClass classes not found: org.thymeleaf.spring4.SpringTemplateEngine (OnClassCondition)

   EmbeddedServletContainerAutoConfiguration.EmbeddedJetty
      - required @ConditionalOnClass classes not found: org.eclipse.jetty.server.Server,org.eclipse.jetty.util.Loader (OnClassCondition)

   WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#beanNameViewResolver
      - @ConditionalOnBean (types: org.springframework.web.servlet.View; SearchStrategy: all) found no beans (OnBeanCondition)

   WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#viewResolver
      - @ConditionalOnBean (types: org.springframework.web.servlet.View; SearchStrategy: all) found no beans (OnBeanCondition)

   WebSocketAutoConfiguration
      - required @ConditionalOnClass classes not found: org.springframework.web.socket.WebSocketHandler (OnClassCondition)



2014-03-28 14:41:04.821  INFO 2852 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port: 9090
2014-03-28 14:41:04.829  INFO 2852 --- [           main] c.PiranhaStorageServiceClientApplication : Started PiranhaStorageServiceClientApplication in 4.231 seconds (JVM running for 5.249)

共有1个答案

龚国源
2023-03-14

我发现spring Boot中的MultipartAutoConfiguration存在一个问题。我禁用了它,并在web配置中添加了@bean,它工作了。基于这个异常,我看到Jackson映射器试图解析多部分内容,而不是多部分解析器本身。

这是片段。

@EnableAutoConfiguration(exclude={MultipartAutoConfiguration.class}

在webconfig类中添加以下内容

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    logger.info("Loading the multipart resolver");
    CommonsMultipartResolver multipartResolver 
            = new CommonsMultipartResolver();
    return multipartResolver;
}
 类似资料:
  • 这是我的控制器: 我的mvc-dispatcher-servlet.xml pom.xml:

  • 背景 我正在Java8上构建一个Spring MVC web应用程序,并在Tomcat8上运行它。除了这些信息之外,Spring版本是和我给您介绍了环境背景,因为一些问题解决者提到该版本与此错误有关。 下面是我的表格 奇怪的事 是通过上传文件没有问题。它就像一个魅力!!对于我没有什么可抱怨的!! 我已经用这个关键字搜索了谷歌,并尽可能多地搜索,就像我在发布关于的问题之前经常做的那样 我真的不明白为

  • 问题内容: 我正在尝试使用上传文件。下面是我的代码如何工作,但是如果我尝试使用它,则会得到以下信息: HTTP状态400-所需的MultipartFile参数’file’不存在 我不明白错误是什么。 我正在使用高级Rest Client进行测试,并且正在将文件作为附件上传。 我的Java代码: 问题答案: 春天需要 bean处理文件上载。 您应该在文件中注册该bean 。 Content-Type

  • 简单示例:jsp中的表单 我的控制器的方法 我的上下文xml文件 描述由于被认为是客户端错误(例如,错误的请求语法、无效的请求消息帧或欺骗性的请求路由),服务器无法或不会处理请求。 Apache Tomcat/8.5.12

  • 我想从我的Android应用程序向Spring Boot发送post请求。我使用okhttp以JSON格式发送HTTP post请求。代码如下: 每次我使用Android请求发送post请求时,我都会收到400个错误的请求参数“name”不存在,“path”:“/newcustomer”。但当我使用postman时,它可以工作。

  • 我试图向我的控制器发送POST请求,但除非我决定使用JSON,否则无法以任何类型传递任何参数。我的目标是将一个字符串和一个文件传递给我的控制器,但我不断得到错误。 我不能在这里有文件。同样,如果我尝试: 同样的事情,我在这里找不到名字。 我通过邮递员发送请求,如下所示: 唯一的标题标记用于授权。我没有任何内容类型标题,我试图添加,但没有帮助。 传递字符串参数的唯一方法是向URL添加。所以下面的有效