How to download image using rest template?

旧城冷巷雨未停 提交于 2020-08-24 06:03:17

问题


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?


回答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.




回答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) {}



回答3:


The RestTemplate is expecting a class (e.g. some in-memory representation) to convert the response from the server into. For example, it could convert a response like:

{id: 1, name: "someone"}

into a class like:

class NamedThing {
    private int id;
    private String name;

    // getters/setters go here
}

By calling:

NamedThing thing = restTemplate.getForObject("/some/url", NamedThing.class);

But, what you seem to really want to do is to take the response from the server and stream it directly to a file. Various methods exist to get the response body of your HTTP request as something like an InputStream that you can read incrementally, and then write out to an OutputStream (e.g. your file).

This answer shows how to use IOUtils.copy() from commons-io to do some the dirty work. But you need to get an InputStream of your file... A simple way is to use an HttpURLConnection. There's a tutorial with more information.



来源:https://stackoverflow.com/questions/32080207/how-to-download-image-using-rest-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!