【发布时间】:2018-03-11 06:38:36
【问题描述】:
我正在创建 api,我需要在一个请求中发送 Json(作为对象)和图像文件。所以这就是我的做法。
@PostMapping("/upload")
public String singleFileUpload(@RequestBody Item item, @RequestParam("file") MultipartFile file) {
// logic
}
以及我如何使用 Postman 测试此方法
这也是我的物品对象
{
"title": "useruser",
"description": "Woodfsdfw",
"img": "www.gosdfsdfsdfsdfog.elt",
"lat": 45.48745,
"lng": 96.5651655,
"user": {
"name": "Jerry",
"img" : "sdfsdfdfsdf",
"email": "jrry@gmail.com"
}
}
所以这是我从服务器得到的响应
{
"timestamp": 1520750026769,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'multipart/form-data;boundary=----WebKitFormBoundaryYzp58riGtVnLl7mI;charset=UTF-8' not supported",
"path": "/api/upload"
}
我已经为此苦苦挣扎了 4 个小时。知道如何解决吗?
编辑
在我使用Content-Type multipart/form-data 之后,我得到了这个错误
{
"timestamp": 1520750811814,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.web.multipart.MultipartException",
"message": "Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found",
"path": "/api/upload"
}
【问题讨论】:
-
使用
Content-Typeofmultipart/form-data -
@ShaneCoder 我编辑了我的答案。我以前尝试过使用它。但据我所知邮递员自动设置正确
Content-Type
标签: java spring multipartform-data