How to download image using rest template?
25,015
Solution 1
Image is a byte array, so you need to use byte[].class
object as a second argument for 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);
To make it work, you will need to configure a ByteArrayHttpMessageConverter
in your application config:
@Bean
public RestTemplate restTemplate(List<HttpMessageConverter<?>> messageConverters) {
return new RestTemplate(messageConverters);
}
@Bean
public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
return new ByteArrayHttpMessageConverter();
}
I've tested this in a Spring Boot project and the image is saved to a file as expected.
Solution 2
If you simply need to get an image from a URL, Java comes with the javax.imageio.ImageIO class, which contains this method signature:
public static BufferedImage read(URL var0) throws IOException;
example use:
try {
BufferedImage image = ImageIO.read(new URL("http://www.foo.com/icon.png"));
int height = image.getHeight();
int width = image.getWidth();
} catch (IOException e) {}
Author by
gstackoverflow
Updated on June 29, 2020Comments
-
gstackoverflow almost 4 years
I have the following code:
restTemplate.getForObject("http://img.championat.com/news/big/l/c/ujejn-runi_1439911080563855663.jpg", File.class);
I especially took image which doesn't require authorization and available absolutely for all.
when following code executes I see the following stacktrace:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.io.File] and content type [image/jpeg] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:559) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:243) at com.terminal.controller.CreateCompanyController.handleFileUpload(CreateCompanyController.java:615)
what do I wrong?
-
gstackoverflow over 8 yearsI didn't catch which technolohy can I use to download file
-
gstackoverflow over 8 yearsResponseEntity<File> responseEntity = restTemplate.getForObject("img.championat.com/news/big/l/c/…", ResponseEntity.class) doesn't work too
-
pioto over 8 yearsI did... HttpURLConnection + IOUtils
-
pioto over 8 yearsThe downside if this approach is that the entire file has to be stored in memory. For smaller images that may be fine, but you could still risk blowing the stack.
-
mzc over 8 yearsI understood from @gstackoverflow's question that this is the goal - convert response body to an object and go from there. It's not clear if having the file object in memory is acceptable for his use case. By the way, can you clarify on blowing the stack, I'm not sure I follow (AFAIK objects should always be created on the heap, only references can be local)
-
pioto over 8 yearssorry, your right, I meant the heap... but yeah, either way... it's best to use streaming APIs when possible, IMHO, to avoid keeping stuff like this in memory long-term. Since it seems his goal is simply to save the remote file to a local file, I don't see a need for an intermediate
byte[]
? -
mzc over 8 yearsI agree, if you want to handle larger files or large traffic, streaming to a file is the only way. More Spring examples: stackoverflow.com/questions/5673260/…