I/O error on POST request for “https://some-service/” Stream is closed; nested exception is java.io.IOException: Stream is closed

  Kiến thức lập trình

I am trying to upload a file to an external service using post method.

//final InputStream data = .... some imput stream;

 HttpHeaders headers = new HttpHeaders();
      headers.add(AUTHORIZATION_HEADER, String.format(AUTH_TOKEN_HEADER, "my-service-token"));
      headers.setContentType(MediaType.MULTIPART_FORM_DATA);

      LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
      map.add("data", new MultipartInputStreamFileResource(data, fileName));

      HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
      return getRestTemplate().postForObject("https://external-service/", requestEntity, MyResponse.class);

However, I am getting the following error, and honestly I could not find anything for it.

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://external-service/": Stream is closed; nested exception is java.io.IOException: Stream is closed
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:414)

LEAVE A COMMENT