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

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

问题描述

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

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

至于您的示例网址 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天全站免登陆