Sample code for 30+ languages & platforms
Java

Get FTP File Permissions

See more FTP Examples

_LANGUAGE_ example showing how to retrieve file permissions (via FTP) for each file in a remote directory.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Ftp2;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Ftp2 ftp = new Ftp2();

        // Set the various properties to establish the connection.
        ftp.setHostname("ftp.example.com");
        ftp.setUsername("login");
        ftp.setPassword("password");

        // If SSL/TLS is required..
        // See http://www.cknotes.com/determining-ftp2-connection-settings/
        // for more information.
        ftp.setAuthTls(true);
        ftp.setPassiveUseHostAddr(true);

        // Connect to the FTP server:
        try {
            ftp.connectOnly();

            // Authenticate with the FTP server.
            ftp.loginAfterConnectOnly();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // To get file permissions in UNIX format, disallow MSLD:
        ftp.setAllowMlsd(false);

        // To get file and sub-directory information, simply
        // loop from 0 to ftp.GetDirCount() - 1
        int i = 0;
        int n = ftp.getDirCount();
        if (n < 0) {
            System.out.println(ftp.getLastErrorText());
            return;
        }

        // Show the type of permissions information that is available.
        // The two most common types are "mlsd" and "unix".  See the
        // online reference documentation for more information.
        if (n > 0) {
            System.out.println("The permissions format is: " + ftp.getPermType(0));
        }

        while (i < n) {
            // Display the permissions and filename
            System.out.println(ftp.getPermissions(i) + " " + ftp.getFilename(i));

            i = i + 1;
        }

        // Assuming MLSD was possible, let's see the file permissions in MLSD format:
        ftp.setAllowMlsd(true);

        // Clear the directory cache so we're forced to re-fetch the directory listing:
        ftp.clearDirCache();

        n = ftp.getDirCount();
        if (n < 0) {
            System.out.println(ftp.getLastErrorText());
            return;
        }

        // Show the type of permissions information that is available.
        // The two most common types are "mlsd" and "unix".  See the
        // online reference documentation for more information.
        if (n > 0) {
            System.out.println("----");
            System.out.println("The permissions format is: " + ftp.getPermType(0));
        }

        i = 0;
        while (i < n) {
            // Display the permissions and filename
            System.out.println(ftp.getPermissions(i) + " " + ftp.getFilename(i));

            i = i + 1;
        }

        ftp.disconnect();
    }
}