Java根據圖片url下載圖片
java 天生在網絡編程方面具有獨特的優勢,封裝了許多網絡的api.下面寥寥數句就實現了爬取網絡圖片的功能。
String imageUrl
URL url = new URL(imageUrl);
//打開網絡輸入流
DataInputStream dis = new DataInputStream(url.openStream());
String newImageName=“C:/2.jpg”;
//建立一個新的文件
FileOutputStream fos = new FileOutputStream(new File(newImageName));
byte[] buffer = new byte[1024];
int length;
//開始填充數據
while( (length = dis.read(buffer))>0){
fos.write(buffer,0,length);
}
dis.close();
fos.close();
您正在看的文章來自瘋狂軟件教育中心 www.fkjava.org
信息咨詢:Q564205990 星老師