commons-net is compatible with ssh-2.0 protocol

Ivan Persiani picture Ivan Persiani · Dec 19, 2013 · Viewed 22.2k times · Source

i have tried create a project with library commons.net for send via ftp some files. But i created a connection with my server i have received this error.

org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Server Reply: SSH-2.0-OpenSSH_5.3

i have followed this article for create my connection, and with official examples i've controlled article.

my java code here:

  private void connect(String host, String user, String pwd) {
        try{
        ftp = new FTPSClient(false);
        //ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        int reply;

        ftp.connect(host,22);//error is here
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new Exception("Exception in connecting to FTP Server");
        }
        ftp.login(user, pwd);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

I do not understand where I went wrong.

Answer

Nick Wilson picture Nick Wilson · Dec 19, 2013

The FTPS protocol does not run over SSH. What you need is SFTP. For this you could look at the Jsch library

    JSch jsch = new JSch();
    Session session = jsch.getSession( user, host, port );
    session.setConfig( "PreferredAuthentications", "password" );
    session.setPassword( pass );
    session.connect( FTP_TIMEOUT );
    Channel channel = session.openChannel( "sftp" );
    ChannelSftp sftp = ( ChannelSftp ) channel;
    sftp.connect( FTP_TIMEOUT );