How to download image using rest template?

2020-08-17 12:36发布

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?

3条回答
可以哭但决不认输i
2楼-- · 2020-08-17 13:01

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.

查看更多
成全新的幸福
3楼-- · 2020-08-17 13:02

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) {}
查看更多
倾城 Initia
4楼-- · 2020-08-17 13:05

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.

查看更多
登录 后发表回答