如何获取目录URL的文件/目录列表? [英] How to get list of files/directories of an directory URL?

查看:261
本文介绍了如何获取目录URL的文件/目录列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个URL:http://java.sun.com/j2se/1.5/pdf我想获取pdf目录下所有文件/目录的列表.

Let say I have a URL: http://java.sun.com/j2se/1.5/pdf I want to get a list of all files/directories under the pdf directory.

我正在使用Java 5.

I'm using Java 5.

我可以使用此程序 http://www.httrack.com/获取目录列表,但使用Java我不知道是否可行.

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 can this program do the job if Java can't?

推荐答案

有一些条件:

  1. 服务器必须已启用目录列表,以便您查看其内容.
  2. 我不知道(没有API或HTTP动词)检索列表,因此该列表通常显示为普通的HTML页面
  3. 您必须解析此HTML页面才能找到条目.

使用 JSoup 之类的库可以轻松完成解析.

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

至于示例URL 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.

这篇关于如何获取目录URL的文件/目录列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆