Question

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.

Was it helpful?

Solution

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 );

OTHER TIPS

SFTP (file transfer running as an SSH stream over an SSH connection) is not the same as FTPS (FTP using SSL/TLS).

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class FTPConnectAndLoginDemo {

public static void main(String[] args) throws Exception {

    String user = "username";
    String host = "hostadress";
    int port = 22;
    String pass = "password";
    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host, port);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setPassword(pass);
    session.connect();
    System.out.println("Connection established.");
    System.out.println("Creating SFTP Channel.");
}

try this instead hope this can help.The Session.setConfig ommits the hashcode check and allows user to connect to host.

Change the port to 21 to connect to FTP server instead of SFTP(port 22)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top