[英]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"
}
--- 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) {
}
如果我收到單個多部分文件,此控制器是否可以通過識別content-type(xml和json,無順序)來解析這兩個部分。如果不可以,請為同一個控制器建議格式
實現此目的的方法是將零件邊界名稱與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) {
// ...
}
// ...
}
使用以下命令在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.....
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.