我有一个以下面的格式处理Multipart Request的应用程序。
POST .... HTTP/1.1
. . .
Accept:multipart/form-data
...
---boundary123 Content-type:application/octet-stream content-Disposition:
form-data filenale="payload.txt" name="someuniquename"
...
[paylaod content](this is in xml format)
---boundary123 content-type:application/json content-Disposition:form-data
name="someuniquname1"
{
...
ID:"999"
}
--- boundary123
这是我的控制器部分。
@Restcontroller
Class A{
@RequestMapping(value = "/a/b/c", method = RequestMethod.POST, consumes=
MediaType.MULTIPART_FORM_DATA_VALUE,
produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody static void MyController(@RequestParam("file")
List<MultipartFile> files) {
}
如果我正在接收单个多部分文件,该控制器是否可以通过识别内容类型(xml和json,无订单)来解析这两个部分。如果不能,您可以建议控制器的格式。
答案 0 :(得分:0)
实现此目的的方法是使用部分边界名称和RequestPart
注释:
@Restcontroller
Class A {
@RequestMapping(
value = "/a/b/c",
method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody void myController(@RequestPart("someuniquname") SomePojo xmlPart, @RequestPart("someuniquname1") SomeOtherPojo jsonPart) {
// ...
}
// ...
}
答案 1 :(得分:0)
使用以下命令获取Controller中的FormData。
RequestMapping(value = "/yourPath", method = RequestMethod.POST)
public @ResponseBody Object upload(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
//Get your form fields...
final String ID= request.getParameter('ID');
//and so on......
//Get your files.
Iterator<String> iterator = request.getFileNames();
MultipartFile multipartFile = null;
while (iterator.hasNext()) {
multipartFile = request.getFile(iterator.next());
//do something with the file.....
}
}