//java 通過url下載圖片保存到本地
public static void download(String urlString, int i) throws Exception {
// 構造URL
URL url = new URL(urlString);
// 打開連接
URLConnection con = url.openConnection();
// 輸入流
InputStream is = con.getInputStream();
// 1K的數據緩沖
byte[] bs = new byte[1024];
// 讀取到的數據長度
int len;
// 輸出的文件流
String filename = "D:\\圖片下載/" + i + ".jpg"; //下載路徑及下載圖片名稱
File file = new File(filename);
FileOutputStream os = new FileOutputStream(file, true);
// 開始讀取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
System.out.println(i);
// 完畢,關閉所有鏈接
os.close();
is.close();
}