使用RestTemplate下载图像文件的问题

时间:2013-11-20 01:28:07

标签: java

我正在尝试使用REST get调用下载图像文件(我尝试使用.jpg和.png)。当我使用restTemplate上传文件时,标题响应中的Content-Type表示“content-type:application / json; charset = UTF-8”。当我尝试使用restTemplate下载相同的文件时,我得到以下异常。当我将restTemplate正在使用的URL复制到浏览器中时,它会正确下载该文件但显示“Content-Type:image / jpeg; charset = binary”。这是我用来尝试下载的代码。

try{

    // Prepare acceptable media type
    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    acceptableMediaTypes.add(MediaType.APPLICATION_JSON);

    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Type", "image/jpeg; charset=binary");


    HttpEntity<String> entity = new HttpEntity<String>(headers);

    // Send the request as GET
    ResponseEntity<String> result= restTemplate.exchange(downloadURL, HttpMethod.GET, entity, String.class);

    String body = result.getBody();
    System.out.println("DownloadFile: "+ result.getBody());
}
catch(Exception e){
    e.printStackTrace();
}



java.nio.charset.UnsupportedCharsetException: binary
    at java.nio.charset.Charset.forName(Charset.java:543)
    at org.springframework.http.MediaType.checkParameters(MediaType.java:299)
       at org.springframework.http.MediaType.<init>(MediaType.java:263)
       at org.springframework.http.MediaType.parseMediaType(MediaType.java:584)
       at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:286)
       at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:64)
       at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:619)
       at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1)
       at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446)
       at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
       at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:377)

2 个答案:

答案 0 :(得分:0)

您需要使用自定义转换器来使用restTemplate。

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

public class BufferedImageHttpMessageConverter implements HttpMessageConverter<BufferedImage> {

    public List<MediaType> getSupportedMediaTypes() {
        return Collections.singletonList(new MediaType("image", "jpeg"));
    }

    public boolean supports(Class<? extends BufferedImage> clazz) {
        return BufferedImage.class.equals(clazz);
    }

    public BufferedImage read(Class<BufferedImage> clazz, HttpInputMessage inputMessage) throws IOException {
        return ImageIO.read(inputMessage.getBody());
    }

    public void write(BufferedImage image, HttpOutputMessage message) throws IOException {
        throw new UnsupportedOperationException("Not implemented");
    }

}

以XML方式添加转换器

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <bean class="BufferedImageHttpMessageConverter"/>
        </list>
    </property>
</bean>

然后是电话

    restTemplate.getForObject(downloadURL, BufferedImage.class, variables);

这取自https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate

答案 1 :(得分:0)

现在Spring已经有了这样的转换器,因此您无需创建一个转换器,只需将其添加到配置中

matchMedia