我需要帮助连接到SFTP服务器?有人有工作代码吗?
我发现了这样的事情
package test.JSch;
import com.jcraft.jsch.*;
public class TestJSch {
public static void main(String args[]) {
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("USSERNAME", "HOST", 22);
//session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("PASSWORD");
System.out.println("1");
session.connect();
System.out.println("2");
Channel channel = session.openChannel("sftp");
System.out.println("3");
channel.connect();
System.out.println("4");
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.get("remotefile.txt", "localfile.txt");
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SftpException e) {
e.printStackTrace();
}
}
}
但这是输出
com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect
at com.jcraft.jsch.Util.createSocket(Util.java:258)
at com.jcraft.jsch.Session.connect(Session.java:186)
at com.jcraft.jsch.Session.connect(Session.java:145)
at test.JSch.TestJSch.main(TestJSch.java:17)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(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:252)
... 3 more
这段代码有什么问题?
解决方法:
您可以尝试增加Jsch Framework的超时.
session.connect(int Timeout)
session.connect(30000);
更多关于Jcsh javadoc