Java Apache Commons Net FTPClient 和 listFiles()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2712967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache Commons Net FTPClient and listFiles()
提问by Vladimir Mihailenco
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.
任何人都可以向我解释以下代码有什么问题吗?我尝试了不同的主机,FTPClientConfigs,它可以通过 firefox/filezilla 正确访问......问题是我总是得到空文件列表,没有任何例外(files.length == 0)。我使用随 Maven 安装的 commons-net-2.1.jar。
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);
采纳答案by PapaFreud
Found it!
找到了!
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());
}
}
}
Gives me this output:
给我这个输出:
c128 c64 c64.hu incoming plus4
回答by Gelvis
usually the annonymous user doesn't need a password, try
通常匿名用户不需要密码,试试
client.login("anonymous", "");
回答by suketup
Only using enterLocalPassiveMode()
did not work for me.
仅使用enterLocalPassiveMode()
对我不起作用。
I used following code, which worked.
我使用了以下代码,该代码有效。
ftpsClient.execPBSZ(0);
ftpsClient.execPROT("P");
ftpsClient.type(FTP.BINARY_FILE_TYPE);
Complete example is as below,
完整的例子如下,
FTPSClient ftpsClient = new FTPSClient();
ftpsClient.connect("Host", 21);
ftpsClient.login("user", "pass");
ftpsClient.enterLocalPassiveMode();
ftpsClient.execPBSZ(0);
ftpsClient.execPROT("P");
ftpsClient.type(FTP.BINARY_FILE_TYPE);
FTPFile[] files = ftpsClient.listFiles();
for (FTPFile file : files) {
System.out.println(file.getName());
}