简体   繁体   English

无法使用 Apache Commons FTPClient 访问 FTP 服务器上的子文件夹

[英]Can't access subfolders on FTP server using Apache Commons FTPClient

I want to download files which are in subfolder on FTP server but I see that my client can't access subfolders.我想下载 FTP 服务器上子文件夹中的文件,但我发现我的客户端无法访问子文件夹。

I wrote simple code to list all files in subfolder:我编写了简单的代码来列出子文件夹中的所有文件:

 public static void main(String[] args) throws IOException {
    FTPClient ftpClient = new FTPClient();

    ftpClient.connect(HOST, PORT);
    ftpClient.login(USER, PASS);
    ftpClient.enterLocalPassiveMode();
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

    FTPFile[] files = ftpClient.listFiles("/archive");
    int length = files.length;
    System.out.println("Number of files: " + length);

    ftpClient.logout();
    ftpClient.disconnect();
}

But length of files array is zero.但是files数组的长度为零。

When I run this program on main directory files are listed properly.当我在主目录上运行此程序时,文件会正确列出。

Your code is ok.你的代码没问题。 It should work on most servers .它应该适用于大多数服务器 Your code will send this command to the server:您的代码会将此命令发送到服务器:

LIST /archive

That's correct (assuming the server uses this path syntax [slash] to refer to a subfolder of the root folder).这是正确的(假设服务器使用此路径语法 [斜杠] 来引用根文件夹的子文件夹)。


But some (obscure) servers have problems with arguments passed to the LIST command.但是一些(晦涩的)服务器在传递给LIST命令的参数方面存在问题。

Try this code instead:试试这个代码:

ftpClient.cwd("archive");
FTPFile[] files = ftpClient.listFiles();

The above will send:以上将发送:

CWD archive
LIST

Another approach worth trying is removing the leading slash, as the server may use a different path delimiter:另一种值得尝试的方法是删除前导斜杠,因为服务器可能使用不同的路径分隔符:

FTPFile[] files = ftpClient.listFiles("archive");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Apache Commons Net FTPClient 从 FTP 服务器循环读取多个文件 - Reading multiple files in loop from FTP server using Apache Commons Net FTPClient 使用apache commons将文件存储到ftp服务器中不起作用 - store a file into a ftp server using apache commons doesn't work 使用org.apache.commons.net.ftp.FTPClient从AsyncTask类登录FTP时出错 - Error when logging into FTP from AsyncTask class using org.apache.commons.net.ftp.FTPClient 使用org.apache.commons.net.ftp.FTPClient保护FTP - Secure FTP with org.apache.commons.net.ftp.FTPClient 在Java上下载FTP文件夹(使用FTPClient Apache Commons Net 3.6 API) - Download FTP folder on Java (Using FTPClient Apache Commons Net 3.6 API) 使用apache FTPClient从FTP服务器下载文件 - File Download from a FTP Server using apache FTPClient 使用Apache FTPClient通过代理连接到FTP服务器 - Connecting to an FTP server via a proxy using Apache FTPClient 使用 Apache Commons FTPClient 监控进度 - Monitoring progress using Apache Commons FTPClient apache.commons.net.ftp.FTPClient未将文件上传到所需的文件夹 - apache.commons.net.ftp.FTPClient not uploading the file to the required folder 如何使用“ org.apache.commons.net.ftp.FTPClient”解决异常 - How to solve exception with “org.apache.commons.net.ftp.FTPClient”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM