@Test
public void test() {
String url_1 = "http://s7ondemand6.scene7.com/is/image/MothercareASE/";
String url_2 = "?&$dw_large_mc$&wid=800&hei=800&fit=fit,1";
List<String> styleIds = Arrays.asList(
"mj245",
"mj227",
"mj235",
"mj240",
"mb519"
);
List<String> failIds = Lists.newArrayList();
int count =1 ;
for (String str:styleIds) {
for (int j=1;j<20;j++) {
String styleIdStr = "l"+str + "_" + j;
String url = url_1 + styleIdStr + url_2;
// 網絡請求所需變量
try {
URL urlIn = new URL(url);
InputStream inputStream = urlIn.openStream();
//獲取輸入流
BufferedInputStream in = new BufferedInputStream(inputStream);
//創建文件流
File file = new File("D:/myPic/" + styleIdStr + ".jpg");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] data = new byte[4096];
int c;
while ((c =in.read(data)) > 0 ) {
out.write(data, 0, c);
}
System.out.println("正在下載第"+count+"個圖片,圖片名爲" + styleIdStr + ".jpg");
count++;
out.close();
in.close();
} catch (MalformedURLException e) {
if(j==1){
failIds.add(str);
}
} catch (IOException e) {
if(j==1){
failIds.add(str);
}
break;
}
}
}
System.out.println(failIds);
}
補充:這個只是最初的版本,因爲很多時候因爲網絡波動導致連接超時,目前只能記錄下載失敗的ID,再運行一次,這裏等待後續優化!
批量下載圖片的代碼實現
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.