Get list of files from FTP server : Ftp « Network Protocol « Java






Get list of files from FTP server

 
 
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.io.FileUtils;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    FTPClient client = new FTPClient();

    client.connect("ftp.domain.com");
    client.login("admin", "secret");

    String[] names = client.listNames();
    for (String name : names) {
      System.out.println("Name = " + name);
    }

    FTPFile[] ftpFiles = client.listFiles();
    for (FTPFile ftpFile : ftpFiles) {
      // Check if FTPFile is a regular file
      if (ftpFile.getType() == FTPFile.FILE_TYPE) {
        System.out.println("FTPFile: " + ftpFile.getName() + "; "
            + FileUtils.byteCountToDisplaySize(ftpFile.getSize()));
      }
    }
    client.logout();
    client.disconnect();
  }
}
 

   
  








Related examples in the same category

1.Upload file to FTP server
2.Connect to FTP server
3.Download file from FTP server
4.Delete file from FTP server
5.Running the edtFTPj demo
6.Implements a Java FTP client from socket and RFC
7.Graphical Ftp clientGraphical Ftp client
8.Ftp client demonstration
9.Ftp client gets server file size
10.Establish ftp connection
11.Use the FTP Client
12.Use the FTPClient: server file transfer
13.A simple Java tftp client