The image is an array of bytes, so you need to use the byte[].class object as the second argument to RestTemplate.getForObject :
String url = "http://img.championat.com/news/big/l/c/ujejn-runi_1439911080563855663.jpg"; byte[] imageBytes = restTemplate.getForObject(url, byte[].class); Files.write(Paths.get("image.jpg"), imageBytes);
For it to work, you need to configure ByteArrayHttpMessageConverter in the application configuration:
@Bean public RestTemplate restTemplate(List<HttpMessageConverter<?>> messageConverters) { return new RestTemplate(messageConverters); } @Bean public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() { return new ByteArrayHttpMessageConverter(); }
I tested this in a Spring boot project, and the image is saved in a file as expected.
source share