Apache Commons FTPClient.listFiles

发布于 2024-08-23 18:19:25 字数 1424 浏览 10 评论 0原文

我正在使用 org.apache.commons .net.ftp.FTPClient 在我的一个应用程序中与 FTP 服务器一起使用。我能够连接登录pwdcwd。但是,当我尝试列出文件时,它不会返回该目录中的文件列表,而我确信该目录中存在文件。我正在使用方法 FTPFile[] listFiles(),它返回一个 FTPFile 的空数组。

请在下面找到我正在尝试的代码片段:

        String hostname = properties.getProperty("FTP_SERVER");
        String user = properties.getProperty("FTP_USER");
        String passwd = properties.getProperty("FTP_PASSWD");
        FTPClient client = new FTPClient();
        client.connect(hostname);
        client.login(user, passwd);
        String reply = client.getStatus();
        System.out.println(reply);
        client.enterRemotePassiveMode();
        client.changeWorkingDirectory("/uploads");
        FTPFile[] files = client.listFiles();
        System.out.println(files.length);
        for (FTPFile file : files) {
            System.out.println(file.getName());
        }

        String[] fileNames = client.listNames();
        if (fileNames != null) {
            for (String file : fileNames) {
                System.out.println(file);
            }
        }
        client.disconnect();

I am using org.apache.commons.net.ftp.FTPClient in one of my applications to work with a FTP server. I am able to connect, login, pwd and cwd. However, when I try to list the files it doesn't return the list of files in that directory, where I know for sure that there are files. I am using the method FTPFile[] listFiles(), it returns an empty array of FTPFile.

Please find below the code snippet where I am trying this:

        String hostname = properties.getProperty("FTP_SERVER");
        String user = properties.getProperty("FTP_USER");
        String passwd = properties.getProperty("FTP_PASSWD");
        FTPClient client = new FTPClient();
        client.connect(hostname);
        client.login(user, passwd);
        String reply = client.getStatus();
        System.out.println(reply);
        client.enterRemotePassiveMode();
        client.changeWorkingDirectory("/uploads");
        FTPFile[] files = client.listFiles();
        System.out.println(files.length);
        for (FTPFile file : files) {
            System.out.println(file.getName());
        }

        String[] fileNames = client.listNames();
        if (fileNames != null) {
            for (String file : fileNames) {
                System.out.println(file);
            }
        }
        client.disconnect();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

放血 2024-08-30 18:19:25

这似乎与我遇到(并解决)的问题相同,请参阅此答案:

Apache Commons Net FTPClient 和 listFiles()

This seems like the same issue I had (and solved), see this answer:

Apache Commons Net FTPClient and listFiles()

叹沉浮 2024-08-30 18:19:25

当我将模式设置为 PASV 后,它现在工作正常!
感谢您的所有努力和建议!

After I set the mode as PASV it is working fine now!
Thanks for all your efforts and suggestions!

怕倦 2024-08-30 18:19:25

我添加了 client.enterLocalPassiveMode() 并且它有效:

client.connect("xxx.com");
boolean login = client.login("xxx", "xxx");
client.enterLocalPassiveMode();

I added client.enterLocalPassiveMode() and it works:

client.connect("xxx.com");
boolean login = client.login("xxx", "xxx");
client.enterLocalPassiveMode();
帅气称霸 2024-08-30 18:19:25

只是一个愚蠢的建议...您可以使用普通的 FTP 客户端在 /uploads 文件夹上列出列表吗?我问这个问题是因为某些 FTP 服务器设置为不显示上传文件夹列表。

Just a silly suggestion... can you do a listing on the /uploads folder using a normal FTP client. I ask this because some FTP servers are setup to not display the listing of an upload folder.

淑女气质 2024-08-30 18:19:25

首先,确保该列表在其他程序中有效。如果是这样,一种可能是文件列表没有被正确解析。您可以尝试显式指定要与 启动ListParsing

First, make sure the listing works in other programs. If so, one possibility is that the file listing isn't being parsed correctly. You can try explicitly specifying the parser to use with initiateListParsing.

ゃ懵逼小萝莉 2024-08-30 18:19:25

我遇到了同样的问题,结果是它无法解析服务器为文件列表返回的内容。连接到 ftp 服务器 ftpClient.setParserFactory(new MyFTPFileEntryParserFactory()); 后我这条线

public class MyFTPFileEntryParserFactory implements FTPFileEntryParserFactory {
private final static FTPFileEntryParser parser = new UnixFTPEntryParser() {
    @Override public FTPFile parseFTPEntry(String entry) {
        FTPFile ftpFile = new FTPFile();
        ftpFile.setTimestamp(getCalendar(entry));
        ftpFile.setSize(get(entry));
        ftpFile.setName(getName(entry));
        return ftpFile;
    }
};

@Override public FTPFileEntryParser createFileEntryParser(FTPClientConfig config) throws ParserInitializationException {
    return parser;
}

@Override public FTPFileEntryParser createFileEntryParser(String key) throws ParserInitializationException {
    return parser;
}

}

I had to same problem and it turned out to be that it couldn't parse what the server was returning for a file listing. I this line after connecting to the ftp server ftpClient.setParserFactory(new MyFTPFileEntryParserFactory());

public class MyFTPFileEntryParserFactory implements FTPFileEntryParserFactory {
private final static FTPFileEntryParser parser = new UnixFTPEntryParser() {
    @Override public FTPFile parseFTPEntry(String entry) {
        FTPFile ftpFile = new FTPFile();
        ftpFile.setTimestamp(getCalendar(entry));
        ftpFile.setSize(get(entry));
        ftpFile.setName(getName(entry));
        return ftpFile;
    }
};

@Override public FTPFileEntryParser createFileEntryParser(FTPClientConfig config) throws ParserInitializationException {
    return parser;
}

@Override public FTPFileEntryParser createFileEntryParser(String key) throws ParserInitializationException {
    return parser;
}

}

方圜几里 2024-08-30 18:19:25

就我而言,除了应用 enterLocalPassiveMode 并指示正确的操作系统之外,我还需要将 UnparseableEntries 设置为 true 以使 listFile 方法起作用。

FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setUnparseableEntries(true);
f.configure(conf);
boolean isLoginSuccess = client.login(username, password);

In my case, on top of applying enterLocalPassiveMode and indicating correct operation system, I also need to set UnparseableEntries to true to make the listFile method work.

FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setUnparseableEntries(true);
f.configure(conf);
boolean isLoginSuccess = client.login(username, password);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文