【发布时间】:2013-12-03 20:07:33
【问题描述】:
我正在尝试使用 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)
【问题讨论】:
-
我想用这个 charset utf-8 代替 charset = binary replce
-
我确实尝试过,但我得到了同样的错误。
标签: java