我需要幫助連接到SFTP服務器?有人有工作代碼嗎?使用java連接到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
什麼是錯的代碼?
當你說它不起作用時,你可以更具體嗎?你有什麼異常?它是否編譯?你在用什麼庫? – 2011-06-09 09:22:09