Apache Commons FTPClient.listFiles
我正在使用 org.apache.commons .net.ftp.FTPClient
在我的一个应用程序中与 FTP 服务器一起使用。我能够连接
、登录
、pwd
和cwd
。但是,当我尝试列出文件时,它不会返回该目录中的文件列表,而我确信该目录中存在文件。我正在使用方法 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这似乎与我遇到(并解决)的问题相同,请参阅此答案:
Apache Commons Net FTPClient 和 listFiles()
This seems like the same issue I had (and solved), see this answer:
Apache Commons Net FTPClient and listFiles()
当我将模式设置为
PASV
后,它现在工作正常!感谢您的所有努力和建议!
After I set the mode as
PASV
it is working fine now!Thanks for all your efforts and suggestions!
我添加了
client.enterLocalPassiveMode()
并且它有效:I added
client.enterLocalPassiveMode()
and it works:只是一个愚蠢的建议...您可以使用普通的 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.
首先,确保该列表在其他程序中有效。如果是这样,一种可能是文件列表没有被正确解析。您可以尝试显式指定要与 启动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.
我遇到了同样的问题,结果是它无法解析服务器为文件列表返回的内容。连接到 ftp 服务器 ftpClient.setParserFactory(new MyFTPFileEntryParserFactory()); 后我这条线
}
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());
}
就我而言,除了应用 enterLocalPassiveMode 并指示正确的操作系统之外,我还需要将 UnparseableEntries 设置为 true 以使 listFile 方法起作用。
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.