public class Image {
private static Map<String, String> config;
static {
if (null == config) {
config = new HashMap<>();
}
config.put("url", "http://xxx.com/images/upload/image/");
config.put("dest", "D:/image");
}
public static void main(String[] args) {
recursion(config.get("url"));
}
private static void recursion(String url) {
//HttpClient 请求
String string = HttpUtil.get(url);
//正则表达式,找到<a>标签的 href里面的值
Pattern p = Pattern.compile("<a[^>]*>([^<]*)</a>");
Matcher m = p.matcher(string);
while (m.find()) {
//根目录。需要结束当前循环。
if (m.group(1).equals("../")) {
continue;
}
//如果href 里面的值 包含了.png|.jpg等
if (m.group(1).contains(".")) {
HttpUtil.downloadFile(url + (m.group(1)) + "", config.get("dest"));
continue;
}
//否则的话 递归进行下一个文件寻找
recursion(url + m.group(1));
}
}
}