Shy*_*yam 20 java ftp-client apache-commons
我org.apache.commons.net.ftp.FTPClient
在我的一个应用程序中使用FTP服务器.我能够connect
,login
,pwd
和cwd
.但是,当我尝试list
文件时,它不会返回该目录中的文件列表,我知道确实存在文件.我正在使用该方法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();
Run Code Online (Sandbox Code Playgroud)