public class Demo {
public static void main(String[] args) throws IOException {
String url = "https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%B6%AB%B7%BD%BD%F0%B9%DD%B3%A4&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111";
InputStream is = getConnection(url,null).getInputStream();
String html = IOUtils.toString(is, "GBK");
List<String> picPaths = parseHtml(html);
if(picPaths.size() > 0) {
ExecutorService es = Executors.newFixedThreadPool(picPaths.size() < 50 ? picPaths.size() : 50);
for (final String picPath : picPaths) {
es.execute(()->{downLoad(pathHandle(picPath), "F:\\pic",url);});
}
es.shutdown();
}
}
/**
* 获取唯一序列号,做为文件名
* @return
*/
private static String getUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString().replaceAll("-", "");
}
/**
* 处理获取到的图片链接
* @param picPath
* @return
*/
private static String pathHandle(String picPath) {
if(!picPath.startsWith("http")) {
picPath = "http:" + picPath;
}
if(picPath.indexOf("_") != picPath.lastIndexOf("_")) {
picPath = picPath.substring(0,picPath.lastIndexOf("_"));
}
return picPath;
}
/**
* 下载图片
* @param picPath
* @param dir
* @param referer
*/
private static void downLoad(String picPath, String dir,String referer){
try {
String name = getUUID()+"."+FilenameUtils.getExtension(picPath);
FileUtils.copyToFile(getConnection(picPath,referer).getInputStream(), new File(new File(dir),name));
System.out.println(picPath + "下载完毕!");
} catch (IOException e) {
System.err.println(picPath+ "下载失败!");
}
}
/**
* 使用正则表达式解析html内容,获取图片链接
* @param html
* @return
*/
private static List<String> parseHtml(String html) {
String regex = "\"[^\"^(^)^}^>^<^{]+\\.(jpg|png|jpeg|gif)";
List<String> list = new ArrayList<>();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(html);
while(m.find()) {
list.add(m.group().substring(1));
}
return list;
}
/**
* 根据url地址,获取一个连接对象,同时设置请求头,避免服务器防盗链,以及模拟浏览器请求
* @param url
* @param referer
* @return
*/
private static URLConnection getConnection(String url,String referer) {
try {
URLConnection uc = new URL(url).openConnection();
uc.setRequestProperty("referer", referer==null?"http://www.baidu.com/":referer);
uc.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
return uc;
} catch (IOException e) {
e.printStackTrace();
System.out.println(url);
return null;
}
}
}