kakashy
kakashy

Reputation: 744

How to use ftp4j with connections to linux by sftp

I have this problem when use ftp4j:

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

The ftp server has enabled connections on port 22 and I can connect with no problems filezilla.

and this is my code:

 private boolean copiarArchviosFtp(){

    FTPClient ftpDestino=new FTPClient();

    try{
        TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustManager, new SecureRandom());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        ftpDestino.setSSLSocketFactory(sslSocketFactory);
        ftpDestino.setSecurity(FTPClient.SECURITY_FTPS);

        ftpDestino.connect("172.24.1.109",22);
        ftpDestino.login("user","password");
        ftpDestino.changeDirectory("/home/");


        FTPFile[] listFilte=ftpDestino.list();
        for(FTPFile ftpFile:listFilte){
            System.out.println("nameFile: "+ftpFile.getName());
        }

    }
    catch(Exception e){
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}

Any idea about to how solve this problem?

Upvotes: 2

Views: 3058

Answers (1)

kakashy
kakashy

Reputation: 744

I am investigating how to copy files by SFTP. The ftp4j library was not working even with FTPClient.SECURITY_FTPS specified because FTPS is different from SFTP.

I found a solution using another library called JSCH.

Here is an example of JSCH. I hope this helps somebody.

Here is more information on SFTP vs FTPS.

Upvotes: 2

Related Questions