Apache Commons Net FTPClient 和 listFiles() [英] Apache Commons Net FTPClient and listFiles()

查看:38
本文介绍了Apache Commons Net FTPClient 和 listFiles()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下下面的代码有什么问题?我尝试了不同的主机,FTPClientConfigs,它可以通过 firefox/filezilla 正确访问......问题是我总是得到空文件列表,没有任何异常(files.length == 0).我使用的是随 Maven 一起安装的 commons-net-2.1.jar.

 FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_L8);FTPClient 客户端 = 新的 FTPClient();客户端配置(配置);client.connect("c64.rulez.org");client.login("匿名", "匿名");client.enterRemotePassiveMode();FTPFile[] 文件 = client.listFiles();Assert.assertTrue(files.length > 0);

解决方案

找到了!

问题是您希望在连接之后但在登录之前进入被动模式.您的代码对我没有任何回报,但这对我有用:

import org.apache.commons.net.ftp.FTPClient;导入 java.io.IOException;导入 org.apache.commons.net.ftp.FTP 文件;公共类 BasicFTP {public static void main(String[] args) 抛出 IOException {FTPClient 客户端 = 新的 FTPClient();client.connect("c64.rulez.org");client.enterLocalPassiveMode();client.login("匿名", "");FTPFile[] files = client.listFiles("/pub");对于(FTPFile 文件:文件){System.out.println(file.getName());}}}

给我这个输出:

<前>c128C64c64.hu传入加4

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);

解决方案

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

这篇关于Apache Commons Net FTPClient 和 listFiles()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆