最近和外部系统进行对接,对方提供了一个下载接口,返回Content-Type:image/jpeg格式文件,用restTemplate接收byte[]一个报错,后查询大量资料,可用org.springframework.core.io.Resource接口接收image/jpeg格式文件流,然后再重新写回到HttpServletResponse 达到下载功能
问题
Could not extract response: no suitable HttpMessageConverter found for response type [class java.io.OutputStream] and content type [image/jpeg]
解决
ResponseEntity<org.springframework.core.io.Resource> result = restTemplate.exchange(requestEntity, org.springframework.core.io.Resource.class);
org.springframework.core.io.Resource body = result.getBody();
if (body != null) {
InputStream inputStream = body.getInputStream();
byte[] data = read(inputStream);
HttpServletResponse response = RequestHolder.getResponse();
response.getOutputStream().write(data);
response.getOutputStream().flush();
response.getOutputStream().close();
}