【问题标题】:commons-net is compatible with ssh-2.0 protocolcommons-net 兼容 ssh-2.0 协议
【发布时间】:2014-01-07 18:14:35
【问题描述】:

我尝试使用库 commons.net 创建一个项目,以便通过 ftp 发送一些文件。但是我创建了与我的服务器的连接我收到了这个错误。

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

我已关注此 article 来创建我的连接,并使用 official examples 我已控制文章。

我的java代码在这里:

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

我不明白我哪里出错了。

【问题讨论】:

    标签: java ssh apache-commons-net ftps


    【解决方案1】:

    将端口更改为 21 以连接到 FTP 服务器而不是 SFTP(端口 22)

    【讨论】:

      【解决方案2】:
      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.");
      }
      

      试试这个希望这能有所帮助。Session.setConfig 省略了哈希码检查并允许用户连接到主机。

      【讨论】:

        【解决方案3】:

        FTPS 协议不通过 SSH 运行。你需要的是SFTP。为此,您可以查看Jsch

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

        【讨论】:

        • 谢谢尼克。很有用
        【解决方案4】:

        SFTP(通过 SSH 连接作为 SSH 流运行的文件传输)与 FTPS(使用 SSL/TLS 的 FTP)不同。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-12-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-16
          • 2017-10-23
          • 2021-11-16
          • 1970-01-01
          相关资源
          最近更新 更多