Apache Commons Net FTPClient and listFiles()

42,281

Solution 1

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

Solution 2

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

Solution 3

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

client.login("anonymous", "");
Share:
42,281
Vladimir Mihailenco
Author by

Vladimir Mihailenco

Updated on December 27, 2020

Comments

  • Vladimir Mihailenco
    Vladimir Mihailenco over 3 years

    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);
    
  • Jonik
    Jonik about 11 years
    (Regarding the BTW comment: Assert.assertTrue is from JUnit or TestNG; Java's assert would simply be assert. Anyway, I guess the point was just to illustrate desired outcome to readers of the question.)
  • PapaFreud
    PapaFreud about 11 years
    @Jonik Oh, that's right. I wasn't paying attention. I removed that bit.
  • Junchen Liu
    Junchen Liu almost 10 years
    there are localpassive remotepassive, localactive, remoteactive i tried them one after another, and yeh localpassive worked
  • Kamil Nękanowicz
    Kamil Nękanowicz almost 7 years
    Cannot find methods: ftpClient.execPBSZ(0); ftpClient.execPROT("P")
  • suketup
    suketup almost 7 years
    Are you using FTPClient or FTPSClient? Those methods only exist in FTPSClient.
  • Rajesh Kumar Yadav
    Rajesh Kumar Yadav almost 5 years
    Thanks a ton my dear friend, i searched all the website but unable to find any solution to this issue. You code saved me! Just cool
  • Tobias
    Tobias over 2 years
    Thank you so much! You just saved my Friday evening!