需要一个 Spring Rest 服务,该服务需要将输入作为 JSON 内容和多个多部分文件。
最佳答案
下面是我在 Spring MVC Controller 中处理 JSON 内容+多个多部分的方法
后端实现:
@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = {"multipart/form-data"})
public
@ResponseBody
List<String> handleFileUpload(MultipartHttpServletRequest multipartHttpServletRequest) {
InputStream jsonSteam = multipartHttpServletRequest.getFile("json").getInputStream();
InputStream fileStream1 = multipartHttpServletRequest.getFile("file1").getInputStream();
InputStream fileStream2 = multipartHttpServletRequest.getFile("file2").getInputStream();
}
前端实现:
请求负载:
------WebKitFormBoundaryhKn3wrSAw57pRAso
Content-Disposition: form-data; name="file1"; filename="deleme_bkup.sql"
Content-Type: text/x-sql
------WebKitFormBoundaryhKn3wrSAw57pRAso
Content-Disposition: form-data; name="file2"; filename="source.sql"
Content-Type: text/x-sql
------WebKitFormBoundaryhKn3wrSAw57pRAso
Content-Disposition: form-data; name="json"; filename="blob"
Content-Type: application/json
------WebKitFormBoundaryhKn3wrSAw57pRAso--
Response Headersview source
请求 header :
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:5533
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryhKn3wrSAw57pRAso
Host:localhost:8080
Origin:http://localhost:8080
Pragma:no-cache
关于具有 JSON 内容 + 多个多部分文件的 Spring MVC Rest 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26398515/