[英]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.