CloseableHttpResponse response = null;
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(“图片URL”);
try {
response = httpClient.execute(httpGet);// 由客户端执行(发送)Get请求
HttpEntity httpEntity = response.getEntity();// 从响应模型中获取响应实体
System.out.println(response.getStatusLine().getStatusCode()); // 获取响应状态码
InputStream inputStream=httpEntity.getContent(); //获取链接返回的流
FileUtils.copyInputStreamToFile(inputStream, new File(“保存路径及文件名”));
//文件名后缀 可以从图片url中截取
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {// 释放资源
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}