JSCH:SFTP.使用端口21挂在session.connect()上 [英] JSCH: SFTP. Hangs at session.connect() using the port 21

查看:411
本文介绍了JSCH:SFTP.使用端口21挂在session.connect()上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过sftp(JSCH)连接到FTP服务器.

I am connecting to FTP server via sftp(JSCH).

每次我使用端口21连接到FTP服务器时,它始终挂在session.connect()上.

Evertime i connect to the FTP server using the port 21, it always hangs at session.connect().

它不会引发任何异常.但是当我使用其他端口时.它可以工作,并且会引发异常.

It does not throw any exception. But when i use other ports. It works and it throws exception.

有什么办法可以捕捉到错误吗?

Is there any way i could catch the error?

这是我的代码的示例.

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

    JSch jsch = new JSch();

    try {

        Session session = jsch.getSession("username", "host", 21);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword("password");
        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp channelSftp = (ChannelSftp) channel;
        session.disconnect();
        channelSftp.disconnect();

    } catch (JSchException e) {
        log("Cannot make connection to FTP server ");
        e.printStackTrace();

    }

}

推荐答案

端口22是SFTP的默认端口吗?并且在端口21上运行的FTP服务器将不知道如何协商安全FTP的会话.基本上,SFTP是通过SSH的FTP.

Could it be that port 22 is the default port for SFTP? And a FTP server running on port 21 won't know how to negotiate the conversation for secure FTP. Basically, SFTP is FTP over SSH.

问题是,它无限期地等待谈判完成.这是墨西哥的僵局,双方都没有放弃.在session.connect()之前调用session.setTimeout(),或以一些合适的值(3-5秒)调用session.connect(timeout).我相信超时时间以毫秒为单位.

EDITED: The issue is, it is waiting indefinitely for the negotiation to complete. It is a Mexican stand-off with neither side giving up. Call session.setTimeout() before session.connect(), or call session.connect(timeout), with some suitable value (3-5 seconds). I believe the timeout is in milliseconds.

这篇关于JSCH:SFTP.使用端口21挂在session.connect()上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆