Created
September 8, 2018 16:24
-
-
Save gMan1990/31486c62170cd535bb91addd7725bc63 to your computer and use it in GitHub Desktop.
[Index of /]页面递归搜索文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param url | |
* endsWith("/") | |
* @param str | |
* searchStr | |
*/ | |
private static void search(String url, String str) { | |
try { | |
Document doc = Jsoup.connect(url).get(); | |
Elements elements = doc.select("pre > a"); | |
for (int i = 1; i < elements.size(); i++) { | |
Element element = elements.get(i); | |
String href = element.attr("href"); | |
href = URLDecoder.decode(href, doc.charset().name()); | |
if (href.endsWith("/")) { | |
search(url + href, str); | |
} else if (StringUtils.containsIgnoreCase(href, str)) { | |
System.out.println(url + href); | |
} | |
} | |
} catch (HttpStatusException e) { | |
// 403 Forbidden | |
if (403 == e.getStatusCode()) { | |
return; | |
} | |
// 500 error | |
if (500 == e.getStatusCode()) { | |
System.err.println(e.toString()); | |
return; | |
} | |
throw new RuntimeException(e); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
递归下载文件