Search code examples
javaftp4j

Download an Entire Directory Using FTP4J


I have looked through the documentation and I can't seem to find a function which does this. So, I assume that I should code it myself. Looking further into the documentation, I found a list() function which lists all the files in a working directory. How would I download all the files while preserving the directory structure?


Solution

  • After you connect to the server:

    FTPClient client = new FTPClient();
    client.connect(host);
    client.login(user, pass);
    

    You change to the desired folder

    client.changeDirectory(ftpFolder);
    

    and then you request the list of files:

    FTPFile[] list = client.list();
    

    Iterate the file array of results and download the file. using:

    FTPFile[] list = client.list();
    for (int i = 0; i < list.length; i++)
    {
       //client.download("localFile", new java.io.File("remotefile);
        client.download(list[i].getName(), new java.io.File(list[i].getName());    
    }