0
@RequestMapping(value = "sendEmail", method = RequestMethod.POST,consumes = "multipart/form-data",produces="application/json")
@ResponseBody
public MessageResponse<MessageDetailVo> sendEmail(@RequestParam(value="inputRequest") String inputRequest, @RequestParam(value="file") List<MultipartFile> files) ;

The above code is the signature of my controller that expects a string and a multipart file list as request params. Now I am trying to use something like below to form the post params and am not able to form the client code. Anyother solution is also appreciated.

List<MultipartFile> files = new ArrayList<MultipartFile>();
String inputRequest=givenParameter;

    for (AttachmentDetailVo file : oldAttachmentsList) {

        MockMultipartFile firstFile = new MockMultipartFile("file",
                file.getFileName(), "text/plain", file.getByteContent());

        files.add(firstFile);
    }



    /////////////////////list <multipartfile created

    MultiValueMap<String, Object> x = new MultiValueMap<String,Object>();
    x.add("inputRequest", inputRequest);
    x.add("file", files.toString());

files is the multipart file list that I want to send along with the String inputRequest. How can I send the required params to my spring controller? What else I need to add and correct in my code to send the proper request to controller ?

tereško
  • 58,060
  • 25
  • 98
  • 150
Aditya Raman
  • 309
  • 8
  • 19
  • won't `x.add("file", files.toString());` should be `x.add("file", files);` ? – Bond - Java Bond Feb 03 '15 at 16:38
  • @Bond-JavaBond : Yes logically it should be. But after mapping that object to the string "file" how would a post request be executed to a url to controller ? – Aditya Raman Feb 05 '15 at 07:52
  • nto sure what you mean by "how"; but since your controller expect a list of files it should be set as-is in client – Bond - Java Bond Feb 05 '15 at 08:47
  • @Bond-JavaBond: See the controller accept multipart/ form which is basically strings. Later on from the key value pair the required file list will be extracted but in the first place the communication will be of strings and cant be of any custom java object. Correct me if wrong. – Aditya Raman Feb 05 '15 at 09:57
  • No its not. You will get the specs [here](http://tools.ietf.org/html/rfc1867) and further explanation [here](http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean) – Bond - Java Bond Feb 05 '15 at 10:52
  • @Bond-JavaBond From my understanding, I need to send the bytes along with some proper headers and delimiters to make it available on server side. But I am not getting any simple procedure to post a list and a string together which actually an easy problem. – Aditya Raman Feb 06 '15 at 12:44
  • have a look [here](http://stackoverflow.com/a/21805186/1910582) – Bond - Java Bond Feb 06 '15 at 16:18
  • Thats exactly what I needed. Thanks. The only doubt remaining is that how to chain the upload calls when we are dealing with a list of files and not a predetermined number of files in the request (3 in the example you showed). @Bond-JavaBond – Aditya Raman Feb 07 '15 at 12:31
  • hope [this](http://crunchify.com/spring-mvc-tutorial-how-to-upload-multiple-files-to-specific-location/) helps – Bond - Java Bond Feb 07 '15 at 16:19

0 Answers0