简体   繁体   English

commons-net 兼容 ssh-2.0 协议

[英]commons-net is compatible with ssh-2.0 protocol

i have tried create a project with library commons.net for send via ftp some files.我尝试使用库 commons.net 创建一个项目,以便通过 ftp 发送一些文件。 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:我的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();
        }
    }

I do not understand where I went wrong.我不明白我哪里出错了。

The FTPS protocol does not run over SSH. FTPS协议不通过SSH运行。 What you need is SFTP . 你需要的是SFTP For this you could look at the Jsch library 为此,您可以查看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 );

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

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. 尝试这一点,希望这可以帮助.Session.setConfig省略哈希码检查,并允许用户连接到主机。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM