我是春天的初学者,
我正在尝试从网上下载文件并将其存储在我的本地磁盘中,但是我收到了瓷砖中提到的编译错误
我试图将ResponseEntity和json作为String工作但是当我尝试打开图像时图像被损坏或不受支持
我的代码:
public static void main(String arg[]) throws IOException
{
HttpHeaders requestHeaders = new HttpHeaders();
//requestHeaders.setAcceptEncoding(ContentCodingType.IDENTITY);
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the String message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
// Make the HTTP GET request, marshaling the response to a String
ResponseEntity<byte[]> response = restTemplate.exchange("http://www.nndb.com/people/954/000354889/duke-kahanamoku-2-sized.jpg", HttpMethod.GET, requestEntity,byte[].class);
File file = new File("/Users/crohitk/Desktop/image.jpg");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
byte[] json =response.getBody();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(json);
bw.close();
System.out.println("Done");
System.out.println(response.getBody());
}