JSch session.connect() 与 CoreFTP 挂起

发布于 2024-11-17 10:47:15 字数 581 浏览 3 评论 0原文

我为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城°AllureLove 2024-11-24 10:47:15

端口 21 是 FTP 的普通端口。 JSch 只是一个 SSH 客户端,支持 SFTPChannelSFTP 类。 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).

濫情▎り 2024-11-24 10:47:15

我也遇到了类似的问题:

"ERROR 2016-04-27 15:05:16,489 [CollectionThreadPool-0] com.dell.supportassist.collector.cli.executor.SSHExecutor: com.jcraft.jsch.JSchException: connection is closed by foreign host"

就我而言,频道被随机关闭。当我们尝试重新连接通道时,它没有重新连接并且失败。

这是由于连接时的循环逻辑而发生的,因此我尝试通过调用方法 connectWithoutOpenChannel 而不是 connectinternal() 来连接没有通道的会话。这解决了我的问题。

I also faced the similar issue:

"ERROR 2016-04-27 15:05:16,489 [CollectionThreadPool-0] com.dell.supportassist.collector.cli.executor.SSHExecutor: com.jcraft.jsch.JSchException: connection is closed by foreign host"

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 of connectinternal(). This resolved my issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文