如何使用休息模板下载图像

如何使用休息模板下载图像

本文介绍了如何使用休息模板下载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  restTemplate.getForObject(http://img.championat.com/news/ big / l / c / ujejn-runi_1439911080563855663.jpg,File.class); 

我特别拍摄了不需要授权且绝对可用的图片。



当下面的代码执行时,我会看到下面的堆栈跟踪:

  org.springframework.web。 client.RestClientException:无法提取响应:没有找到合适的HttpMessageConverter用于响应类型[类java.io.File]和内容类型[image / jpeg] 
在org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor .java:108)
在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:559)
在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)

我错了什么?

byte []。class 对象作为<$ c的第二个参数$ c> RestTemplate.getForObject :

  String url =http://img.championat。 COM /新闻/大/ L / C / ujejn-runi_1439911080563855663.jpg; 
byte [] imageBytes = restTemplate.getForObject(url,byte []。class);
Files.write(Paths.get(image.jpg),imageBytes);

为了使它工作,您需要配置一个 ByteArrayHttpMessageConverter

  @Bean 
public RestTemplate restTemplate(List< HttpMessageConverter<?> > messageConverters){
返回新的RestTemplate(messageConverters);
}

@Bean
public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter(){
return new ByteArrayHttpMessageConverter();
}

我已经在Spring Boot项目中测试过了,图像被保存到一个预期的文件。


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?

解决方案

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.

这篇关于如何使用休息模板下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:21