springboot2 MultipartFile 多文件上传 file 为null解决方案

1.引入UploadConfig配置

@Configuration
public class UploadConfig {
//显示声明CommonsMultipartResolver为mutipartResolver
@Bean(name = “multipartResolver”)
public MultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding(“UTF-8”);
//resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常
resolver.setResolveLazily(true);
resolver.setMaxInMemorySize(40960);
//上传文件大小 5M 510241024
resolver.setMaxUploadSize(5 * 1024 * 1024);
return resolver;
}
}

2.在springboot启动类上加入注解

@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
@SpringBootApplication

3.设置请求头Content-Type

Content-Type=‘multipart/form-data’

4.Controller设置

@RequestParam(“file”) MultipartFile[] multipartFiles

你可能感兴趣的:(springboot2 MultipartFile 多文件上传 file 为null解决方案)