1

Good day,

I write server code (java, spring) that serves GET request. The response should be in multipart-form format and should include 2 parts: string (json object) and file data.

The code is:

FileSystemResource resource = new FileSystemResource(targetFile);
String info = getInfo(targetFile);

MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();  
form.add("info", info);
form.add("file", resource); 

MediaType multiPart = 
    MediaType.parseMediaType(MediaType.MULTIPART_FORM_DATA_VALUE);
ResponseEntity <MultiValueMap<String, Object>> responseEntity = 
    ResponseEntity.ok().contentType(multiPart).body(form);

return responseEntity;

The code works OK, but I didn't find a way to define content type of parts. For first part it should be application/json, and for second part it depends on file type.

How to define this?

bw_dev
  • 775
  • 1
  • 7
  • 17
  • Look into this: https://stackoverflow.com/questions/42533237/receiving-multipart-response-on-client-side-closablehttpresponse – kleash Aug 22 '18 at 17:05

1 Answers1

0

Did you try this?

HttpHeaders headers = new HttpHeaders();
    headers.setContentDisposition(ContentDisposition.builder("inline; filename=\"" + resource.getFilename() + "\"").build());
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setContentLength(resource.contentLength());

    return new ResponseEntity<>(resource, headers, HttpStatus.OK);

Best Regards.

Renan Souza
  • 71
  • 2
  • 12