我在我的一个应用程序中使用org.apache.commons.net.ftp.FTPClient
来与FTP服务器一起工作。我可以使用connect
,login
,pwd
和cwd
。但是,当我尝试list
文件时,它不会返回该目录中的文件列表,因为我确定该目录中有文件。我使用的方法是[FTPFile[] listFiles()
](http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html#listFiles(%29),它返回一个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();
发布于 2011-03-04 00:12:58
这似乎和我曾经(并解决)的问题是一样的,请看下面的答案:
发布于 2010-02-26 15:49:20
在我将模式设置为PASV
后,它现在工作得很好!感谢您的努力和建议!
发布于 2017-06-30 13:52:38
我添加了client.enterLocalPassiveMode()
,它可以工作了:
client.connect("xxx.com");
boolean login = client.login("xxx", "xxx");
client.enterLocalPassiveMode();
https://stackoverflow.com/questions/2339855
复制相似问题