POST InputStream RestTemplate

Viewed 634

I want to POST an InputStream to the Server. I'm using Spring and therefore RestTemplate to execute my HTTP Requests.

Client

    public void postSomething(InputStream inputStream) {
          String url = "localhost:8080/example/id
          RestTemplate restTemplate = new RestTemplate();
          restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
          restTemplate.postForLocation(url, inputStream, InputStream.class); 
    }

Server

@PostMapping("/example/{id}")
public void uploadFile(@RequestBody InputStream inputStream, @PathVariable String id) { 
           InputStream inputStream1 = inputStream;
}

On Client side I get No HttpMessageConverter for [java.io.ByteArrayInputStream] And on Server side I get Cannot construct instance of 'java.io.InputStream' (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

2 Answers

You cannot transfer streams over HTTP. HTTP is a stateless protocol and it won't support byte transfer. What you can do is

  1. Read the whole input stream and output it as a string or any other form
  2. Use any UDP form of data transfer (SOAP or sockets)
Related