JSch session.connect() 与 CoreFTP 挂起
我为 localhost 配置了 CoreFTP 和下一个代码:
JSch.setLogger(new MyJschLogger()); //class for console output
Session session = jsch.getSession("user", "localhost", 21);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword("password");
session.connect();
当程序实现 connect()
时,控制台输出中会出现两条消息:
INFO: Connecting to localhost port 21
INFO: Connection established
...并且没有其他情况发生。几分钟后,出现连接被外部主机关闭
异常。
为什么?
感谢大家!
I have CoreFTP configured for localhost and the next code:
JSch.setLogger(new MyJschLogger()); //class for console output
Session session = jsch.getSession("user", "localhost", 21);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword("password");
session.connect();
when program achieves connect()
, two messages appear at console output:
INFO: Connecting to localhost port 21
INFO: Connection established
...and nothing more happens. After some minutes, connection is closed by foreign host
exception appears.
Why?
Thanks for all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
端口 21 是 FTP 的普通端口。 JSch 只是一个 SSH 客户端,支持 SFTP 在 ChannelSFTP 类。 JSch 对 FTP 一无所知(SFTP 与 FTP 无关,除了名称以及它允许类似的事情之外)。
您需要将服务器设置为使用 SSH 协议(通常在端口 22 上,但您可以使用任何端口,只要在客户端上使用相同的端口即可)。请参阅文档 - 我认为您必须选中 SSH 复选框。
另外,如果您的代码只不过是您发布的代码,那么除了连接之外什么也不会发生。要传输文件,您需要打开 ChannelSFTP,并发出正确的命令(例如调用一个或多个 put/get 方法)。
Port 21 is the normal port for FTP. JSch is only an SSH client, with support for SFTP in the ChannelSFTP class. JSch knows nothing about FTP (and SFTP is unrelated to FTP, other than by name and that it allows similar things).
You need to setup your server to use the SSH protocol (usually on port 22, but you can use any port, as long as you use the same port on the client). See the documentation - I think you have to check the SSH check box.
Also, if your code is nothing more than what you posted, then nothing more than connecting will happen. To transfer files, you will need to open a ChannelSFTP, and issue the right commands (e.g. call one or more of the put/get methods).
我也遇到了类似的问题:
就我而言,频道被随机关闭。当我们尝试重新连接通道时,它没有重新连接并且失败。
这是由于连接时的循环逻辑而发生的,因此我尝试通过调用方法
connectWithoutOpenChannel
而不是connectinternal()
来连接没有通道的会话。这解决了我的问题。I also faced the similar issue:
In my case, channel was getting closed randomly. And when we are trying to re-connect the channel then it was not re-connecting and failing.
This was happening due to looping logic while connecting, so I tried to connect the session without channel by calling method
connectWithoutOpenChannel
instead ofconnectinternal()
. This resolved my issue.