java 如何使用rest模板下载图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32080207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download image using rest template?
提问by gstackoverflow
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?
我怎么了?
回答by mzc
Image is a byte array, so you need to use byte[].class
object as a second argument for RestTemplate.getForObject
:
Image 是一个字节数组,所以你需要使用byte[].class
object 作为第二个参数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:
要使其工作,您需要ByteArrayHttpMessageConverter
在应用程序配置中配置一个:
@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.
我已经在 Spring Boot 项目中对此进行了测试,并且图像已按预期保存到文件中。
回答by mancini0
If you simply need to get an image from a URL, Java comes with the javax.imageio.ImageIO class, which contains this method signature:
如果您只需要从 URL 获取图像,Java 附带了 javax.imageio.ImageIO 类,其中包含以下方法签名:
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) {}
回答by pioto
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:
的RestTemplate
期待一个类(例如,一些在内存中的表示)从服务器到响应转换。例如,它可以转换如下响应:
{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).
但是,您似乎真正想做的是从服务器获取响应并将其直接流式传输到文件。有多种方法可以将您的 HTTP 请求的响应正文获取为类似于InputStream
您可以增量读取的内容,然后写出到一个OutputStream
(例如您的文件)。
This answershows 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 tutorialwith more information.
这个答案显示了如何使用IOUtils.copy()
fromcommons-io
来做一些肮脏的工作。但是您需要获取文件的 InputStream... 一个简单的方法是使用HttpURLConnection
. 有一个包含更多信息的教程。