Apache Commons Net FTPClient and listFiles()

感情迁移 提交于 2019-11-27 18:59:23
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

Only using enterLocalPassiveMode() did not work for me.

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());
    }
Gelvis

usually the annonymous user doesn't need a password, try

client.login("anonymous", "");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!