Java commons-net 兼容 ssh-2.0 协议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20677268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:32:32  来源:igfitidea点击:

commons-net is compatible with ssh-2.0 protocol

javasshapache-commons-netftps

提问by Ivan Persiani

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.

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

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

i have followed this articlefor create my connection, and with official examplesi'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.

我不明白我哪里出错了。

采纳答案by Nick Wilson

The FTPSprotocol does not run over SSH. What you need is SFTP. For this you could look at the Jschlibrary

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

回答by chrylis -cautiouslyoptimistic-

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

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

回答by Bharat Dhal

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 忽略哈希码检查并允许用户连接到主机。