springboot 上传文件,MultipartFile为null

/ 爬坑指南 / 没有评论 / 485浏览
  1. 排除MultipartAutoConfiguration
@SpringBootApplication (exclude = {MultipartAutoConfiguration.class})
  1. 添加MultipartResolver
//显示声明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 5*1024*1024
        resolver.setMaxUploadSize(5 * 1024 * 1024);
        return resolver;
    }
  1. 添加@RequestParam
@PostMapping("/upload")
    public ApiResponse upload(@RequestParam("file") MultipartFile multipartFile) {
     // other code ...
    }