如何在java中获取目录url的文件/目录列表? [英] How to get list of files/directories of an directory url in java?
问题描述
假设我有网址:http://java.sun.com/j2se/1.5/pdf
我想获取所有文件/目录的列表pdf目录。我正在使用java 5.
我可以通过这个程序获取目录列表 http:// www.httrack.com/ 。但是使用java我不知道是否可能。
Let say i have a URL: http://java.sun.com/j2se/1.5/pdf
i want to get list of all files/directories under the pdf directory.I'm using java 5.
I can get the list of dir with this program http://www.httrack.com/ . but with java i don't know if it is possible.
是否有任何机构知道如何在java中获取它或者该程序如何完成java无法完成的工作?
Does any body know how to get it in java or how this program does the job that java can't?
推荐答案
有一些条件:
- 服务器必须启用目录列表才能看到它的内容。
- 我无法知道(没有API或HTTP动词)来检索列表,因此列表通常显示为普通的HTML页面
- 您必须解析此HTML页面才能找到条目。
使用像 JSoup 这样的lib可以轻松完成解析。
The parsing can be done easily using a lib like JSoup.
例如,使用JSoup,您可以在url http://howto.unixdev.net/
中获取文档,如下所示:
For example, using JSoup you can fetch the documents at url http://howto.unixdev.net/
like this:
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Sample {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://howto.unixdev.net").get();
for (Element file : doc.select("td.right td a")) {
System.out.println(file.attr("href"));
}
}
}
将输出:
beignets.html
beignets.pdf
bsd-pam-ldap.html
ddns-updates.html
Debian_on_HP_dv6z.html
dextop-slackware.html
dirlist.html
downloads/
ldif/
Linux-SharePoint.html
rhfc3-apt.html
rhfc3-apt.tar.bz2
SUNWdsee-Debian.html
SUNWdtdte-b69.html
SUNWdtdte-b69.tar.bz2
tcshrc.html
Test_LVM_Trim_Ext4.html
Tru64-CS20-HOWTO.html
至于你的样本网址 http://java.sun.com/j2se/1.5/pdf
这是一个找不到的页面,所以我觉得你运气不好。
As for your sample url http://java.sun.com/j2se/1.5/pdf
this is a page not found, so I think you're out of luck.
这篇关于如何在java中获取目录url的文件/目录列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!