小声的说一句,其实每次只能25张图片,因为图片库多选只能最多选择25张图片。别问我,去问阿里巴巴。图片库为什么不能大量下载,可能是害怕你删库跑路吧~。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class test {

public static String file_path = "C:\\Users\\wangs\\Downloads\\";
public static String picture_path = "C:\\picture_download";

public static void main(String[] args) {
File searchDir = new File(picture_path);

//check the download address whether exist
if (!searchDir.exists() && !searchDir.isDirectory()) {
searchDir.mkdirs();
System.out.println("Create this folder");
} else {
System.out.println("The download address is exist");
}

//try to read the excel file and download the picture from the online.
try {
List<Map<String, String>> maps = redExcel(args[0]);
maps.forEach(System.out::println);

//download the picture here
for(int i = 0; i < maps.size(); i++){
//System.out.println(maps.get(i).get("Image Link"));
downloadPicture(maps.get(i).get("Image Link"), picture_path+"\\"+maps.get(i).get("Image Name")+".jpg");
System.out.println("Successfully download: "+ maps.get(i).get("Image Name"));
}

}catch (Exception e){
System.out.println(e);
}
}


public static List<Map<String, String>> redExcel(String filePath) throws Exception {
File file = new File(filePath);
if (!file.exists()){
throw new Exception("no file exist");
}
InputStream in = new FileInputStream(file);

// read the whole Excel
XSSFWorkbook sheets = new XSSFWorkbook(in);
// get the first Sheet
XSSFSheet sheetAt = sheets.getSheetAt(0);
ArrayList<Map<String, String>> list = new ArrayList<>();

//first row is title row i = 0
XSSFRow titleRow = sheetAt.getRow(0);
// for loop read all data
for (int i = 1; i < sheetAt.getPhysicalNumberOfRows(); i++) {
XSSFRow row = sheetAt.getRow(i);
LinkedHashMap<String, String> map = new LinkedHashMap<>();
// read one
for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {
XSSFCell titleCell = titleRow.getCell(index);
XSSFCell cell = row.getCell(index);
// cell.setCellType(XSSFCell.CELL_TYPE_STRING); outdated, use the content below instaed
cell.setCellType(CellType.STRING);

if (cell.getStringCellValue().equals("")) {
continue;
}
map.put(getString(titleCell), getString(cell));

}
if (map.isEmpty()) {
continue;
}
list.add(map);
}
return list;
}


public static String getString(XSSFCell xssfCell) {
if (xssfCell == null) {
return "";
}
if (xssfCell.getCellTypeEnum() == CellType.NUMERIC) {
return String.valueOf(xssfCell.getNumericCellValue());
} else if (xssfCell.getCellTypeEnum() == CellType.BOOLEAN) {
return String.valueOf(xssfCell.getBooleanCellValue());
} else {
return xssfCell.getStringCellValue();
}
}

//download the picture from the website
public static void downloadPicture(String urlList,String path) {
URL url = null;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());

FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];
int length;

while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}