public static void downloadImage(String src, String filePath) {
InputStream inputStream = null;
FileOutputStream downloadFile = null;
try {
//读取远程图片输入流
URL url = new URL(src);
inputStream = url.openStream();
int index;
byte[] bytes = new byte[1024];
//保存到本地
downloadFile = new FileOutputStream(filePath);
while ((index = inputStream.read(bytes)) != -1) {
downloadFile.write(bytes, 0, index);
downloadFile.flush();
}
downloadFile.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(downloadFile != null){
try{
downloadFile.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
Java 下载远程图片
最新推荐文章于 2023-06-18 01:51:21 发布