当尝试使用端口22上的SFTP连接到我的FTP服务器时,我得到以下异常。
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
我使用以下代码与JDK7u79和JSch库进行连接,使用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);
}
非常奇怪的是我可以使用WinSCP客户端连接到服务器(在端口22上)。所以我很好奇为什么它无法使用Java代码连接?我在代码中遗漏了什么吗?我使用的是Windows7,我的FTP服务器可能是Windows Server 2012上的WingFTP。
更新
关于其中一条评论:我的FTP服务器支持SFTP。使用WinSCP,我可以使用SFTP连接到服务器。这是日志:请注意,出于安全原因,我已经屏蔽了IP。
. 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.
答案 0 :(得分:1)
我在设置密码时遇到了错误。输入正确的密码后即可修复。我们不应该发布这个问题。