Apache Commons Net FTPClient和listFiles()

Vla*_*nco 36 java ftp-client apache-commons

任何人都可以解释我以下代码有什么问题吗?我尝试了不同的主机,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);
Run Code Online (Sandbox Code Playgroud)

Pap*_*eud 89

找到了!

你想在连接之后但在登录之前进入被动模式.你的代码没有给我任何回报,但这对我有用:

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

给我这个输出:

c128
c64
c64.hu
incoming
plus4


suk*_*tup 9

只有使用enterLocalPassiveMode()对我不起作用.

我使用了以下代码,它起作用了.

    ftpsClient.execPBSZ(0);
    ftpsClient.execPROT("P");
    ftpsClient.type(FTP.BINARY_FILE_TYPE);
Run Code Online (Sandbox Code Playgroud)

完整的例子如下,

    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());
    }
Run Code Online (Sandbox Code Playgroud)