Java SFTP 连接错误:连接超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25502222/
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-11 00:31:16  来源:igfitidea点击:

SFTP connection error: connection timeout

javasftpjsch

提问by Tony

I'm trying to connect to SFTP server using jschlibrary. This is how I do:

我正在尝试使用jsch库连接到 SFTP 服务器。这就是我的做法:

public class SFTPConnection {

    private String sftpHost;
    private String sftpUser;
    private String sftpPass;
    private String sftpWorkDir;
    private Session session = null;
    private Channel channel = null;
    private ChannelSftp channelSftp = null;

    static final String SFTPHOST = "prod-sftp.host.ru";
    static final String SFTPUSER = "user";
    static final String SFTPPASS = "pass";
    static final String SFTPWORKINGDIRINC = "/dir/Port_Increment/";
    static final String SFTPWORKINGDIRFULL = "/dir/Port_All_Full/";

    public  SFTPConnection(String sftpHost, String sftpUser, String sftpPass, String sftpWorkDir) throws JSchException, SftpException {
        this.sftpHost = sftpHost;
        this.sftpUser = sftpUser;
        this.sftpPass = sftpPass;
        this.sftpWorkDir = sftpWorkDir;

        JSch jsch = new JSch();

        session = jsch.getSession(this.sftpUser, this.sftpHost, 3232);
        session.setPassword(this.sftpPass);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd(this.sftpWorkDir);
    }

    public SFTPConnection() throws JSchException, SftpException {

        sftpWorkDir = SFTPWORKINGDIRINC;
        JSch jsch = new JSch();
        session = jsch.getSession(SFTPUSER, SFTPHOST, 3232);
        session.setPassword(SFTPPASS);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd(SFTPWORKINGDIRINC);
    }

    public static void main(String[] args) {
        try {
            SFTPConnection con=new SFTPConnection();
        } catch (JSchException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        }
    }
}

This code works on my machine, but now I need to deploy it on another server with high security level. And this is what I've got. The exception is:

这段代码在我的机器上工作,但现在我需要将它部署在另一台具有高安全级别的服务器上。这就是我所拥有的。例外是:

com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect
    at com.jcraft.jsch.Util.createSocket(Util.java:344)
    at com.jcraft.jsch.Session.connect(Session.java:215)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at SFTPConnection.<init>(SFTPConnection.java:53)
    at SFTPConnection.main(SFTPConnection.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at com.jcraft.jsch.Util.createSocket(Util.java:338)
    ... 9 more

I was trying to connect using WinSCP with same parameters. I was trying to change port from 3232to 22, but still this error. What's wrong?

我试图使用具有相同参数的 WinSCP 进行连接。我试图将端口从 更改323222,但仍然出现此错误。怎么了?

UPD:I can not ping prod-sftp.host.ru:

UPD:我无法ping通prod-sftp.host.ru

Pinging #ipaddress# with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

But telnet can connect to 22-nd port.

但是 telnet 可以连接到第 22 个端口。

PS

聚苯乙烯

Most parameters are not real. Don't try to ping it yourself. I just don't wanna share host, username and password with you.

大多数参数不是真实的。不要试图自己ping它。我只是不想和你分享主机、用户名和密码。

回答by Hannes

As your stacktrace told you, the client cannot establish a connection with the server. You are using ftp which does not cope too well with nowadays network infrastructure. You should probably take a look at the RFCs for FTP connections.

正如您的堆栈跟踪告诉您的那样,客户端无法与服务器建立连接。您使用的 ftp 不能很好地应对当今的网络基础设施。您可能应该查看 FTP 连接的 RFC。

In short, you client connects to the control port of the server just to negotiate another port of communication.

简而言之,您的客户端连接到服务器的控制端口只是为了协商另一个通信端口。

  • So first check, that your server is up using ping.
  • Then try to connect to the control port using telnet.
  • If it works so far check whether it is sFTP or FTPs.
  • Since the real communication will be negotiated one another port check you nat/firewall. The handshake from the FTP server might not go through it.
  • 因此,首先检查您的服务器是否已使用 ping 启动。
  • 然后尝试使用 telnet 连接到控制端口。
  • 如果到目前为止它有效,请检查它是 sFTP 还是 FTPs。
  • 由于真正的通信将通过另一个端口进行协商,因此请检查您的 nat/防火墙。来自 FTP 服务器的握手可能不会通过它。

You can always use another FTP client to check the connection first like lftp.

您始终可以使用另一个 FTP 客户端先检查连接,例如lftp.

回答by Magic-Mouse

Have you tried to disable the firewall on either side, it is a well known fact that Java FTP is blocked by a lot of firewalls.

您是否尝试过禁用任一侧的防火墙,Java FTP 被许多防火墙阻止是众所周知的事实。

Have you tried to check if the host is available on the given host with another piece of software like Filezilla, just to make sure that the error is in your code and not some other place, like host name misspelling etc.

您是否尝试使用另一款软件(如 Filezilla)检查给定主机上的主机是否可用,只是为了确保错误在您的代码中而不是其他地方,例如主机名拼写错误等。

回答by Konki

Have you tried to specify timeout? You can do it before:

您是否尝试过指定超时?你可以在之前这样做:

session.connect();

session.connect();

by adding:

通过添加:

session.setTimeout(20000);

session.setTimeout(20000);

In my code I've also added this:

在我的代码中,我还添加了以下内容:

jsch.setKnownHosts(System.getProperty("user.home")+"/.ssh/known_hosts");

jsch.setKnownHosts(System.getProperty("user.home")+"/.ssh/known_hosts");

Regards,

问候,

Konki

今机