java下载网络图片
作者:互联网
场景一:指定位置批量下载图片
public static void downloadImage(List<String> urlList,String directoryName){
if(urlList.size() > 0){
File file = new File("E://"+directoryName);
if(!file.exists()){
file.mkdirs();
}
}
for(int i = 0; i< urlList.size(); ++i){
DataInputStream dis = null;
FileOutputStream fos = null;
try {
URL url = new URL(urlList.get(i));
//打开网络输入流
dis = new DataInputStream(url.openStream());
String[] urlArray = urlList.get(i).split("/");
String imageName = urlArray[urlArray.length -1];
String newImageName="E://"+directoryName+"//"+imageName;
//建立一个新的文件
fos = new FileOutputStream(new File(newImageName));
byte[] buffer = new byte[1024];
int length;
//开始填充数据
while((length = dis.read(buffer))>0){
fos.write(buffer,0,length);
}
} catch (IOException e) {
logger.error("下载图片出现异常:" + ExceptionUtils.getExceptionAllinformation(e));
wirteText(urlList.get(i),failImageTXT);
} finally {
if(dis != null){
try {
dis.close();
} catch (IOException e) {
logger.error("关闭照片的流数据异常" + ExceptionUtils.getExceptionAllinformation(e));
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
logger.error("文件关闭异常" + ExceptionUtils.getExceptionAllinformation(e));
}
}
}
}
}
标签:null,java,String,fos,urlList,new,dis,下载,图片 来源: https://blog.csdn.net/qq_41967899/article/details/89189279