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

Java Spring:不支持内容类型“multipart/form-data;boundary;charset=utf-8”

米裕
2023-03-14

我创建了一个控制器:

@RequestMapping(value = "/photo/" , method = RequestMethod.POST)
public @ResponseBody
void addPhotoData(@RequestBody Photo photo, @RequestParam("data")
        MultipartFile photoData) {

    InputStream in = null;
    try {
        in = photoData.getInputStream();
        photoService.save(photo, in);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我不明白为什么我会收到错误415不支持。救命啊!

共有1个答案

苏彭薄
2023-03-14

尝试将请求正文包装到对象中。

 public class Payload {
   private String name;
   private String url;
   private MultipartFile data;
...
}

添加消耗={“multipart/form-data”}

@RequestMapping(value = "/photo/" , method = RequestMethod.POST, consumes = { "multipart/form-data" })
public @ResponseBody void addPhotoData(@ModelAttribute Payload payload) {
...

}

还有mediatype.multipart_form_data_value常量,而不是使用该字符串

 类似资料: