Java Apache Commons Net FTPClient和listFiles()
谁能解释一下下面的代码有什么问题吗?我尝试了不同的主机,FTPClientConfigs,它可以通过firefox/filezilla正常访问。。。问题是我总是得到没有任何异常的空文件列表(files.length==0)。我使用与Maven一起安装的commons-net-2.1.jarJava Apache Commons Net FTPClient和listFiles(),java,apache-commons,ftp-client,Java,Apache Commons,Ftp Client,谁能解释一下下面的代码有什么问题吗?我尝试了不同的主机,FTPClientConfigs,它可以通过firefox/filezilla正常访问。。。问题是我总是得到没有任何异常的空文件列表(files.length==0)。我使用与Maven一起安装的commons-net-2.1.jar FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_L8); FTPClient client = new F
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);
通常,匿名用户不需要密码,请尝试
client.login("anonymous", "");
找到了
问题是,您想在连接之后,但在登录之前,进入被动模式。
您的代码没有为我返回任何内容,但这对我有效:
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
c128
c64
c64.hu
新当选的
加号
仅使用
enterLocalPassiveMode()
对我不起作用
我使用了下面的代码,这很有效
ftpsClient.execPBSZ(0);
ftpsClient.execPROT("P");
ftpsClient.type(FTP.BINARY_FILE_TYPE);
完整示例如下所示:
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());
}
对我来说,在调用
ftpClient.listFiles()
之前使用ftpClient.enterLocalPassiveMode()
时,它就起作用了。
以下是完整的代码:
final List<String> files = new ArrayList<>();
try {
ftpClient.enterLocalPassiveMode();
String[] f = null;
if (parentDir.isEmpty()) {
f = ftpClient.listNames();
}else{
f = ftpClient.listNames(parentDir);
}
for (String s :f ) {
files.add(s);
}
} catch (IOException e) {
e.printStackTrace();
postError(e.getMessage()!= null?e.getMessage():e.toString());
}
final List files=new ArrayList();
试一试{
ftpClient.enterLocalPassiveMode();
字符串[]f=null;
if(parentDir.isEmpty()){
f=ftpClient.listNames();
}否则{
f=ftpClient.listNames(parentDir);
}
for(字符串s:f){
文件。添加;
}
}捕获(IOE异常){
e、 printStackTrace();
postError(e.getMessage()!=null?e.getMessage():e.toString());
}
是否有错误消息?不知道你的问题是什么!问题是我总是得到没有任何异常的空文件列表(files.length==0)。问题已更新。它在我的FTP服务器上运行正常,只是我没有调用客户端。配置(…)我还尝试了FTP.belnet.be/FTP.ccc.uba.ar和一些私有FTP服务器,但无法使其运行(即使没有客户端。配置)。。。我还尝试禁用windows防火墙和防病毒。。。你能分享一些工作的ftp主机吗?ftp4j基本示例工作正常,但我想知道我的commons网络代码有什么问题。。。(关于BTW注释:Assert.assertTrue
来自JUnit或TestNG;Java的Assert应该是Assert
。无论如何,我想这只是为了向问题的读者说明期望的结果。)@Jonik哦,是的。我没有注意。我删除了那个部分。有localpassive remotepassive,localactive,remoteactive。我一个接一个地尝试了它们,yeh localpassive工作了很多。谢谢我亲爱的朋友,我搜索了所有的网站,但找不到任何解决这个问题的方法。你的代码救了我!只是找不到方法:ftpClient.execPBSZ(0);ftpClient.execPROT(“P”)您使用的是ftpClient还是FTPSClient?这些方法仅存在于FTPSClient中。