Problem mit SFTP und JSch: com.jcraft.jsch.JSchException

Wenn Sie versuchen, eine Verbindung zu meinem FTP-server über SFTP auf port 22, erhalte ich folgende exception.

com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect
    at com.jcraft.jsch.Util.createSocket(Util.java:349)
    at com.jcraft.jsch.Session.connect(Session.java:215)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at com.mycompany.FTPAdapter.connect(FTPAdapter.java:246)
    at com.mycompany.FTPWriterTask.ftpTransport(FTPWriterTask.java:210)
    at com.mycompany.FTPWriterTask.doExecute(FTPWriterTask.java:183)
    at com.mycompany.AbstractTask.execute(AbstractTask.java:123)
    at com.mycompany.FTPWriterTaskTest.testExecute_testSFTP(FTPWriterTaskTest.java:203)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.jcraft.jsch.Util.createSocket(Util.java:343)
    ... 25 more

Verwende ich folgenden code mit JDK7u79 und JSch Bibliothek eine Verbindung über SFTP.

try {
        JSch jsch = new JSch();
        session = jsch.getSession(this.getLoginname(), getHostname(), this.getPortInt());
        Properties config = new Properties(); 
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); 
        session.setPassword(this.getPassword());
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();
        sftpChannel = (ChannelSftp) channel;
    } catch (Exception e) {
        e.printStackTrace();
        String errMsg = "Could not connect to the SFTP site. Reason: " + e.getMessage();
        throw new IOException(errMsg);
    }

Sehr seltsam die Sache ist, dass ich kann eine Verbindung zu dem server (auf port 22) mit WinSCP-client. So bin ich neugierig, warum es keine Verbindung herstellen kann, die mit Java-code? Bin ich etwas fehlt in dem code? Ich benutze Windows7 und mein FTP-server ist WingFTP wahrscheinlich Windows Server 2012.

UPDATE:

Bezug auf einen der Kommentare: Mein FTP-server unterstützt SFTP. Mit WinSCP kann ich die Verbindung zum server Via SFTP. Hier ist der log: Bitte beachten Sie, ich habe maskiert die IP aus Sicherheitsgründen.

. 2016-02-22 13:05:37.552 --------------------------------------------------------------------------
. 2016-02-22 13:05:37.552 WinSCP Version 5.7.6 (Build 5874) (OS 6.3.9600 - Windows Server 2012 R2 Datacenter)
. 2016-02-22 13:05:37.552 Configuration: HKCU\Software\Martin Prikryl\WinSCP 2\
. 2016-02-22 13:05:37.552 Log level: Normal, Logging passwords
. 2016-02-22 13:05:37.552 Local account: INTERNAL\zuics
. 2016-02-22 13:05:37.552 Working directory: C:\Program Files (x86)\WinSCP
. 2016-02-22 13:05:37.552 Process ID: 2956
. 2016-02-22 13:05:37.552 Command-line: "C:\Program Files (x86)\WinSCP\WinSCP.exe" 
. 2016-02-22 13:05:37.552 Time zone: Current: GMT+1, Standard: GMT+1 (W. Europe Standard Time), DST: GMT+2 (W. Europe Daylight Time), DST Start: 3/27/2016, DST End: 10/30/2016
. 2016-02-22 13:05:37.552 Login time: Monday, February 22, 2016 1:05:37 PM
. 2016-02-22 13:05:37.552 --------------------------------------------------------------------------
. 2016-02-22 13:05:37.552 Session name: ICS_Test_SFTP (Site)
. 2016-02-22 13:05:37.552 Host name: xx.xxx.xx.xxx (Port: 22)
. 2016-02-22 13:05:37.552 User name: ICS_Test (Password: PbhDMi912, Key file: No)
. 2016-02-22 13:05:37.552 Tunnel: No
. 2016-02-22 13:05:37.552 Transfer Protocol: SFTP (SCP)
. 2016-02-22 13:05:37.552 Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
. 2016-02-22 13:05:37.552 Disable Nagle: No
. 2016-02-22 13:05:37.552 Proxy: none
. 2016-02-22 13:05:37.552 Send buffer: 262144
. 2016-02-22 13:05:37.552 SSH protocol version: 2; Compression: No
. 2016-02-22 13:05:37.552 Bypass authentication: No
. 2016-02-22 13:05:37.552 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2016-02-22 13:05:37.552 Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2016-02-22 13:05:37.552 KEX: dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN
. 2016-02-22 13:05:37.552 SSH Bugs: A,A,A,A,A,A,A,A,A,A,A,A
. 2016-02-22 13:05:37.552 Simple channel: Yes
. 2016-02-22 13:05:37.552 Return code variable: Autodetect; Lookup user groups: A
. 2016-02-22 13:05:37.552 Shell: default
. 2016-02-22 13:05:37.552 EOL: 0, UTF: 2
. 2016-02-22 13:05:37.552 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
. 2016-02-22 13:05:37.552 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2016-02-22 13:05:37.552 SFTP Bugs: A,A
. 2016-02-22 13:05:37.552 SFTP Server: default
. 2016-02-22 13:05:37.552 Local directory: C:\Users\zuics\Documents, Remote directory: /, Update: Yes, Cache: Yes
. 2016-02-22 13:05:37.552 Cache directory changes: Yes, Permanent: Yes
. 2016-02-22 13:05:37.552 DST mode: 1
. 2016-02-22 13:05:37.552 --------------------------------------------------------------------------
. 2016-02-22 13:05:37.624 Looking up host "xx.xxx.xx.xxx"
. 2016-02-22 13:05:37.624 Connecting to xx.xxx.xx.xxx port 22
. 2016-02-22 13:05:37.672 Server version: SSH-2.0-WeOnlyDo-WingFTP
. 2016-02-22 13:05:37.672 We believe remote version has SSH-2 rekey bug
. 2016-02-22 13:05:37.672 Using SSH protocol version 2
. 2016-02-22 13:05:37.672 We claim version: SSH-2.0-WinSCP_release_5.7.6
. 2016-02-22 13:05:37.710 Doing Diffie-Hellman group exchange
. 2016-02-22 13:05:37.905 Doing Diffie-Hellman key exchange with hash SHA-1
. 2016-02-22 13:05:37.959 Verifying host key rsa2 0x10001,0xb0ba835d0c1ec6a2 11c75258f75f1433 ff5881c58f62a609 4d7f82a9d7666936 4fdc87ab95940ca3 28fbd48e1d9294fa db00b8d56173420d dff1e9e93827e4c1 64e8b615f670db33 6be0bddfc130b136 4f490f351f64def8 480e8e360f8f0cc8 55b8022b7bdebde3 79ad43b862fd1d3c d896f44dc6561dbb af983f7a78399af7  with fingerprint ssh-rsa 1024 fc:92:54:e6:06:04:1a:97:8c:8c:cb:a7:72:e0:86:52
. 2016-02-22 13:05:37.976 Host key matches cached key
. 2016-02-22 13:05:37.976 Host key fingerprint is:
. 2016-02-22 13:05:37.976 ssh-rsa 1024 fc:92:54:e6:06:04:1a:97:8c:8c:cb:a7:72:e0:86:52
. 2016-02-22 13:05:37.976 Initialised AES-128 CBC client->server encryption
. 2016-02-22 13:05:37.976 Initialised HMAC-SHA1 client->server MAC algorithm
. 2016-02-22 13:05:37.976 Initialised AES-128 CBC server->client encryption
. 2016-02-22 13:05:37.976 Initialised HMAC-SHA1 server->client MAC algorithm
! 2016-02-22 13:05:38.173 Using username "ICS_Test".
. 2016-02-22 13:05:38.206 Prompt (password, "SSH password", <no instructions>, "&Password: ")
. 2016-02-22 13:05:38.207 Using stored password.
. 2016-02-22 13:05:38.254 Sent password
. 2016-02-22 13:05:38.260 Access granted
. 2016-02-22 13:05:38.260 Opening session as main channel
. 2016-02-22 13:05:38.261 Opened main channel
. 2016-02-22 13:05:38.481 Started a shell/command
. 2016-02-22 13:05:38.488 --------------------------------------------------------------------------
. 2016-02-22 13:05:38.488 Using SFTP protocol.
. 2016-02-22 13:05:38.489 Doing startup conversation with host.
> 2016-02-22 13:05:38.535 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2016-02-22 13:05:38.543 Type: SSH_FXP_VERSION, Size: 5, Number: -1
. 2016-02-22 13:05:38.543 SFTP version 3 negotiated.
. 2016-02-22 13:05:38.543 We believe the server has signed timestamps bug
. 2016-02-22 13:05:38.543 We will use UTF-8 strings until server sends an invalid UTF-8 string as with SFTP version 3 and older UTF-8 string are not mandatory
. 2016-02-22 13:05:38.543 Changing directory to "/".
. 2016-02-22 13:05:38.543 Getting real path for '/'
> 2016-02-22 13:05:38.543 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2016-02-22 13:05:38.552 Type: SSH_FXP_NAME, Size: 23, Number: 16
. 2016-02-22 13:05:38.552 Real path is '/'
. 2016-02-22 13:05:38.552 Trying to open directory "/".
> 2016-02-22 13:05:38.552 Type: SSH_FXP_LSTAT, Size: 10, Number: 263
< 2016-02-22 13:05:38.566 Type: SSH_FXP_ATTRS, Size: 37, Number: 263
. 2016-02-22 13:05:38.566 Getting current directory name.
. 2016-02-22 13:05:38.718 Listing directory "/".
> 2016-02-22 13:05:38.718 Type: SSH_FXP_OPENDIR, Size: 10, Number: 523
< 2016-02-22 13:05:38.721 Type: SSH_FXP_HANDLE, Size: 12, Number: 523
> 2016-02-22 13:05:38.721 Type: SSH_FXP_READDIR, Size: 12, Number: 780
< 2016-02-22 13:05:38.735 Type: SSH_FXP_NAME, Size: 133, Number: 780
> 2016-02-22 13:05:38.735 Type: SSH_FXP_READDIR, Size: 12, Number: 1036
< 2016-02-22 13:05:38.749 Type: SSH_FXP_STATUS, Size: 28, Number: 1036
< 2016-02-22 13:05:38.749 Status code: 1
> 2016-02-22 13:05:38.749 Type: SSH_FXP_CLOSE, Size: 12, Number: 1284
. 2016-02-22 13:05:38.749 Adaptiv 360_v3.pdf;-;577093;2016-02-17T15:09:38.000Z;"user" [0];"group" [0];rwxrwxrwx;1
. 2016-02-22 13:05:38.809 Startup conversation with host finished.
  • FTP und SFTP sind zwei völlig verschiedene Protokolle. Also Erstens, stellen Sie sicher wissen, welches Protokoll Sie bitten, über. Und aktualisieren Sie Ihre Frage entsprechend. WinSCP-log-Datei wäre auch hilfreich.
  • Hat die getHostname Gegenzug die gleiche IP-Adresse, die Sie verwenden für WinSCP? (xx.xxx.xx.xxx)? Ist es IPv4-Adresse? Tut getPortInt() Gegenzug 22? Führen Sie das WinSCP auf der gleichen Maschine und in der gleichen Umgebung wie der Java-code?
InformationsquelleAutor ParagJ | 2016-02-19
Schreibe einen Kommentar