/**
* 图片下载
* @param picUrl 文件资源链接
* @param imgPath 保存路径
* @return
*/
public static String downloadImg(String picUrl,String imgPath){
CloseableHttpClient httpclient = HttpClients.createDefault();
try{
HttpGet get = new HttpGet(picUrl);
HttpResponse response = httpclient.execute(get);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
try {
FileOutputStream fout = new FileOutputStream(imgPath);
int l = -1;
byte[] tmp = new byte[1024];
while ((l = in.read(tmp)) != -1) {
fout.write(tmp, 0, l);
}
fout.flush();
fout.close();
return imgPath;
} finally {
// 关闭低层流。
in.close();
httpclient.close();
}
}catch(Exception e1){
System.out.println("下载图片出错"+picUrl);
}
return null;
}
Java实现图片文件资源下载
最新推荐文章于 2025-06-29 11:45:48 发布