commons-net与ssh-2.0协议兼容

 阿乀胜69 发布于 2023-02-09 11:56

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

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

我已经按照这篇文章创建了我的连接,并且通过官方示例我已经控制了文章.

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

我不明白我哪里出错了.

1 个回答
  • 该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 );
    

    2023-02-09 11:58 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有