2017-06-14 152 views
0

我有一个应用程序以下面的格式处理多部分请求。多部分请求

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 = "https://stackoverflow.com/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

使用以下命令获取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..... 
    } 
} 
+0

请求和响应包含什么? – phalco

+0

非常感谢您的帮助,我的情况下不需要响应参数。 – phalco

0

实现,这将是与RequestPart注释使用部分boundry 方式:

@Restcontroller 
Class A { 

    @RequestMapping(
      value = "https://stackoverflow.com/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) { 
     // ... 
    } 
// ... 
} 
+0

它需要双方的努力参数,我只发送1多文件有两个MIME附件,也@RequestPart(“someuniqname”)名称可能是anything.Do你觉得这真的有效? – phalco

相关问题