0

I want to send a file (server accepts only multipart/form-data) using RestTemplate. But, on the client side, I just have a byte array of the JSON object. I do not want to convert it to file creating the file as it requires disk space. Can I send this data to the server?

Deb
  • 2,922
  • 1
  • 16
  • 32
Jacob
  • 420
  • 5
  • 15
  • you want to send json data over network of you want json data into file and want to send that file on server .? – Tejal Jul 26 '18 at 10:30
  • Server accepts a file which should have a json data. From my client side, i want to consume that service. But, I do not have a file containing json data in client side. I do have json object. Now, how can I send that to server? Again, server just accepts file that should have Json data – Jacob Jul 26 '18 at 10:35

2 Answers2

0

You can try this

public void uploadDocument(byte[] fileContents, final String filename) {
RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl = "http://localhost:8080/spring-rest/foos"; // Dummy URL.
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();

map.add("name", filename);
map.add("filename", filename);

// Here we 
ByteArrayResource contentsAsResource = new ByteArrayResource(fileContents) {
    @Override
    public String getFilename() {
        return filename; // Filename has to be returned in order to be able to post.
    }
};

map.add("file", contentsAsResource);

// Now you can send your file along.
String result = restTemplate.postForObject(fooResourceUrl, map, String.class);

// Proceed as normal with your results.
}
KuldeeP ChoudharY
  • 446
  • 1
  • 6
  • 22
  • Already tried that. This would have the file content as application/octate-stream but the content should be application/json. When I send json file from POSTMAN, the server code works fine. – Jacob Jul 26 '18 at 10:48
  • Can you please post your Server code, accordingly i can look ! – KuldeeP ChoudharY Jul 26 '18 at 10:54
  • my problem is similar to https://stackoverflow.com/questions/51527666/415-unsupported-media-type-while-sending-json-file-over-rest-template. – Jacob Jul 26 '18 at 11:00
0

This should probably do what you want:

  1. First of all, create an instance of the restTemplate, I usually let spring boot inject one automatically with @Autowired:

    @Autowired
    RestTemplate restTemplate;
    
  2. Now, having your byte array - which I will call jsonAsByteArray for the example- and assuming fileName as the name of your file, assign the URL of the targeted server to a String - which I will call url for the following example-; at this point, you can proceed like this :

    String requestJson = new String(jsonAsByteArray);
    
    MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
    
    HttpHeaders jsonHeaders = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    
    HttpEntity<String> entity = new HttpEntity<String>(requestJson,jsonHeaders);
    
    multipartRequest.add("file", entity);
    multipartRequest.add("fileName", fileName);
    
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
    requestHeaders.setAccept(Arrays.asList((new MediaType[] {MediaType.MULTIPART_FORM_DATA})));
    
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(multipartRequest, requestHeaders);
    
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, multipartRequest, String.class);
    
  3. Check the outcome inside the response; you can simply print it on your console like this:

    System.out.println(out.getBody());
    System.out.println(out.getStatusCode());
    

EDIT: i have modified the original answer according to your last comments. Basically you must encapsulate the http entity containing the json into another and bigger http entity containing the multipart request. All liberally taken from POSTing multipart requests with RestTemplate

Luca Tampellini
  • 1,759
  • 17
  • 23