Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

FTP Connect, Examine Server Certificate, and then Authenticate

See more FTP Examples

Demonstrates how to connect to an FTP server, examine the server's SSL/TLS certificate, and then, if it meets the application's security requirements, proceed to authenticate.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes Chilkat Ftp2 to have been previously unlocked.
        // See Unlock Ftp2 for sample code.

        Ftp2 ftp = new Ftp2();

        ftp.setHostname("www.authtls-ftps-server.com");
        ftp.setUsername("FTP_LOGIN");
        ftp.setPassword("FTP_PASSWORD");
        ftp.setAuthTls(true);
        ftp.setPort(21);

        // Connect to the FTP server using explicit TLS (AUTH TLS).
        try {
            ftp.connectOnly();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the FTP server's certificate.
        Cert serverCert = new Cert();
        try {
            ftp.getServerCert(serverCert);

            // Now that we have the certificate, we can check it in any way we desire.
            // (See the online reference documentation for the certificate object's methods
            // and properties)...

            // Assuming the certificate is OK, proceed to authenticate with the FTP server.
            ftp.loginAfterConnectOnly();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Proceed with uploading/download files, etc...
        //

        ftp.disconnect();
        System.out.println("Success.");
    }
}