I am working on apis which is developed in spring boot. Now I have one API in which I have to send response which contains one binary file and and xml. Both will be seperated by multipart boundary. So is there any way to do this?
Asked
Active
Viewed 6,755 times
7
-
did you get any solution? – Ganesh Gudghe Oct 15 '18 at 04:15
-
yes i got the solution – Darshan Sathwara Oct 16 '18 at 05:22
-
can you pls share the link – Ganesh Gudghe Oct 16 '18 at 05:23
-
I did not get any solution if you have link or code pls share – Ganesh Gudghe Oct 16 '18 at 05:29
-
Corrently I dnt have code But I can give you idea You can create your own response using string and concatenate that string with boundary and then append remaining response. So the receiver will be able to separate the response using the boundary. – Darshan Sathwara Oct 17 '18 at 07:00
-
@Darsshan Sathwara finally I did using spring boot I am posting my code. – Ganesh Gudghe Oct 23 '18 at 05:16
1 Answers
11
In spring boot try following the way to send a response in multipart.
@RequestMapping(method = { RequestMethod.GET },value = "/multipartdata",produces=MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<MultiValueMap<String, Object>> gerMultipartData()
throws Exception {
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
formData.add("first_name", "ganesh");
formData.add("last_name", "patil");
formData.add("file-data_1", new FileSystemResource("C:\Users\ganesh\img\logo.png"));
formData.add("file-data_2", new FileSystemResource("C:\Users\ganeshg\Desktop\download.jpg"));
formData.add("file-data_3", new FileSystemResource("C:\Users\ganeshg\Desktop\odstext.txt"));
formData.add("file-data_4", new FileSystemResource("D:\Agent\152845.docx"));
formData.add("file-data_5", new FileSystemResource("D:\testxls.xlsx"));
return new ResponseEntity<MultiValueMap<String, Object>>(formData, HttpStatus.OK);
}

Ganesh Gudghe
- 1,327
- 1
- 17
- 41
-
2FYI. If you want to customize your boudary. then you can see [here](https://stackoverflow.com/questions/45401043/boundary-in-contenty-type-is-overwritten-by-formhttpmessageconverter/60348928). – kissLife Feb 22 '20 at 04:07
-
2And if you want to stream your response. you can write the multipart body manually in outputstream. [here](https://dzone.com/articles/streaming-data-with-spring-boot-restful-web-servic) is a demo. – kissLife Feb 24 '20 at 03:10
-
Does it have to be `MultiValueMap`? Can't regular Map do the job, assuming each name has only one value? – f.khantsis Oct 22 '21 at 13:20
-
Isn't this MULTIPART_FORM_DATA related to the HTTP request and not the HTTP response? This is better suited for file upload with form data. Cant, we use StreamingResponseBody to compose multipart responses? – Rajendra Jun 13 '23 at 10:23