【问题标题】:Apache Commons Net FTPClient and listFiles()Apache Commons Net FTPClient 和 listFiles()
【发布时间】:2023-04-05 08:03:01
【问题描述】:

谁能解释一下下面的代码有什么问题?我尝试了不同的主机,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);

【问题讨论】:

  • 任何错误信息?不知道你的问题是什么!
  • 问题是我总是得到空文件列表,没有任何异常(files.length == 0)。问题已更新。
  • 它在我的 FTP 服务器上运行良好,除了我不调用 client.configure(...)
  • 我还尝试了 ftp.belnet.be / ftp.ccc.uba.ar 和一些私有 ftp 服务器,但无法使其工作(即使没有 client.configure)...我也尝试过禁用 windows 防火墙和杀毒软件...你能分享一些工作的 ftp 主机吗?
  • ftp4j 基本示例工作正常,但我想知道我的 commons-net 代码有什么问题...

标签: java apache-commons ftp-client


【解决方案1】:

通常匿名用户不需要密码,试试

client.login("anonymous", "");

【讨论】:

    【解决方案2】:

    找到了!

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

    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 传入 加4

    【讨论】:

    • (关于 BTW 评论:Assert.assertTrue 来自 JUnit 或 TestNG;Java 的断言将只是 assert。无论如何,我想这只是为了向问题的读者说明期望的结果。 )
    • @Jonik 哦,没错。我没注意。我删除了那个位。
    • 有localpassive remotepassive、localactive、remoteactive我一个接一个地试了,还是localpassive有效
    • 非常感谢我亲爱的朋友,我搜索了所有网站,但找不到任何解决此问题的方法。你的代码救了我!很酷
    • 非常感谢!你刚刚拯救了我的星期五晚上!
    【解决方案3】:

    只使用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.execPBSZ(0); ftpClient.execPROT("P")
    • 您使用的是 FTPClient 还是 FTPSClient?这些方法只存在于 FTPSClient 中。
    【解决方案4】:

    对我来说,当我在调用 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());
                    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-16
      • 2015-01-17
      • 2016-03-13
      • 2014-03-26
      • 2012-05-22
      • 2013-06-11
      • 2012-03-31
      • 2012-03-21
      • 2013-11-21
      相关资源
      最近更新 更多