多部分请求

Multi-Part 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"
}

---边界123

这是我的控制器部分。

@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,无顺序)来解析这两个部分,如果我收到单个多部分 file.If 不是你能建议吗相同的控制器格式。

实现此目的的方法是使用带有 RequestPart 注释的部分边界 name

@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) {
        // ...
    }
// ...
}

使用以下方法获取控制器中的 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.....
    }
}