Skip to content

Instantly share code, notes, and snippets.

@gMan1990
Created September 8, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gMan1990/31486c62170cd535bb91addd7725bc63 to your computer and use it in GitHub Desktop.
Save gMan1990/31486c62170cd535bb91addd7725bc63 to your computer and use it in GitHub Desktop.
[Index of /]页面递归搜索文件
/**
* @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);
}
}
@gMan1990
Copy link
Author

gMan1990 commented Mar 20, 2019

递归下载文件

    /**
     * @param fromFtp
     *            endsWith("/")
     * @param toDir
     *            endsWith("/")
     */
    private static void download(String fromFtp, String toDir) {
        new File(toDir).mkdirs();
        try {
            Elements elements = Jsoup.connect(fromFtp).get().select(Commons.builders(',',
                    "img[alt=\"[DIR]\"]", "img[alt=\"[   ]\"]", "img[alt=\"[TXT]\"]")).next();
            for (Element element : elements) {
                String href = element.attr("href");
                //System.out.println(element.outerHtml());
                if (href.endsWith("/")) {
                    download(fromFtp + href, toDir + href);
                } else {
                    FileUtils.copyURLToFile(new URL(fromFtp + href), new File(toDir + href));
                }
            }
        } 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