Content type 'multipart/form-data;boundary=----WebKitF;charset=UTF-8' not supported

springboot后台接收文件和对象的时候,会报Content type 'multipart/form-data;boundary=----WebKitF;charset=UTF-8' not supported错误。 代码如下:

 @PostMapping("/test")
    public void test( @RequestBody User user,
            @RequestParam(value = "file1",required = false) MultipartFile file1,
            @RequestParam(value = "file2",required = false) MultipartFile file2) {
        try {
            //业务逻辑
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
    }

 

排查问题,在上传文件的时候,spring框架会自动装配文件类型, 使用@RequestBody接收对象,所对应的content-type :application/json。所以当使用@RequestBody和文件上传的时候,会报错。

 

解决办法一:

去掉@RequestBody注解,使用@RequestParam接收参数

 @PostMapping("/test")
    public void test(@RequestParam(value="userStr")  String userStr,
            @RequestParam(value = "file1",required = false) MultipartFile file1,
            @RequestParam(value = "file2",required = false) MultipartFile file2) {
        try {
            //将userStr转换成user
            User user=JSONObject.parseObject(userStr,User.class);
            //业务逻辑
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
    }

 

解决办法二:

去掉@RequestBody注解

 @PostMapping("/test")
    public void test(  User user,
            @RequestParam(value = "file1",required = false) MultipartFile file1,
            @RequestParam(value = "file2",required = false) MultipartFile file2) {
        try {
            
            //业务逻辑
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
    }

 

如有问题,欢迎留言

你可能感兴趣的:(框架)