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

Verify FTP SSL Server Certificate

See more FTP Examples

This example demonstrates how to verify the FTP server's certificate and authenticity. The intent is to verify the authenticity of the server before passing a login/password to it.

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 requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Ftp2 ftp = new Ftp2();

        ftp.setHostname("ftp.myftpserver.com");
        ftp.setUsername("myUsername");
        ftp.setPassword("myPassword");

        // Establish an AUTH SSL secure channel after connection
        // on the standard FTP port 21.
        ftp.setAuthSsl(true);

        // The Ssl property is for establishing an implicit SSL connection
        // on port 990.  Do not set it.
        ftp.setSsl(false);

        // Indicate that the FTP server must have a verifiable SSL certificate.
        // Do not accept self-signed certs or certificates that are
        // expired, revoked, or cannot be verified to a root authority:
        ftp.setRequireSslCertVerify(true);

        // You may also set a requirement.  In this example,
        // the certificate's Common Name (CN) must match the
        // required string exactly:
        ftp.setSslCertRequirement("subjectcn", "Chilkat Software, Inc.");

        // Connect and login to the FTP server.
        try {
            ftp.connect();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // After logging on, you may examine the FTP server's cert:
        Cert cert = new Cert();
        try {
            ftp.getServerCert(cert);
            // Display the distinguished name of the SSL cert.
            System.out.println(cert.getSubjectDN());
        } catch (ChilkatException e) {
            System.out.println("No server certificate!");
        }

        System.out.println("Secure FTP Channel Established!");

        // Do whatever you're doing to do ...
        // upload files, download files, etc...

        ftp.disconnect();
    }
}