本文介绍了Apache Commons Net FTPClient和listFiles()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以解释以下代码有什么问题吗?我尝试了不同的主机FTPClientConfigs,可以通过firefox/filezilla正确访问它.问题是我总是得到空的文件列表,没有任何异常(files.length == 0).我使用随Maven安装的commons-net-2.1.jar.
Can anyone explain me what's wrong with the following code? I tried different hosts, FTPClientConfigs, it's properly accessible via firefox/filezilla... The problem is I always get empty filelist without any exceptions (files.length == 0). I use commons-net-2.1.jar installed with Maven.
FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_L8);
FTPClient client = new FTPClient();
client.configure(config);
client.connect("c64.rulez.org");
client.login("anonymous", "anonymous");
client.enterRemotePassiveMode();
FTPFile[] files = client.listFiles();
Assert.assertTrue(files.length > 0);
推荐答案
找到了!
问题是您想在连接后但登录之前 进入被动模式 .您的代码对我没有任何回报,但这对我有用:
The thing is you want to enter passive mode after you connect, but before you log in.Your code returns nothing for me, but this works for me:
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPFile;
public class BasicFTP {
public static void main(String[] args) throws IOException {
FTPClient client = new FTPClient();
client.connect("c64.rulez.org");
client.enterLocalPassiveMode();
client.login("anonymous", "");
FTPFile[] files = client.listFiles("/pub");
for (FTPFile file : files) {
System.out.println(file.getName());
}
}
}
给我这个输出:
c128
c64
c64.hu
incoming
plus4
这篇关于Apache Commons Net FTPClient和listFiles()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!