简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM