在你的项目中添加一个配置类 注册CommonsMultipartResolver 解决request转成 MultipartHttpServletRequest
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver(){
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
resolver.setResolveLazily(true);//resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常
resolver.setMaxInMemorySize(40960);
resolver.setMaxUploadSize(50*1024*1024);//上传文件大小 50M 50*1024*1024
return resolver;
}
如果此时multipartRequest.getFile()此时为空,在Application.class启动类中添加
@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class}) 这个注解
在Application.properties中添加
spring.http.multipart.enabled=true