我使用的是Java SFTP库,叫做JSch,这是我的代码:
package client;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class SFTPCode_2 {
public SFTPCode_2() {
super();
}
public static void main (String[] args) {
SFTPCode_2 fileTransfer = new SFTPCode_2();
try {
JSch jsch = new JSch();
String host = "127.0.0.1";
int port = 22;
String user = "username";
Session sessionRead = jsch.getSession(user, host, port);
sessionRead.connect();
Session sessionWrite = jsch.getSession(user, host, port);
sessionWrite.connect();
ChannelSftp channelRead = (ChannelSftp)sessionRead.openChannel("sftp");
channelRead.connect();
ChannelSftp channelWrite = (ChannelSftp)sessionWrite.openChannel("sftp");
channelWrite.connect();
PipedInputStream pin = new PipedInputStream(2048);
PipedOutputStream pout = new PipedOutputStream(pin);
/* channelRead.rename("C:/Users/IBM_ADMIN/Desktop/Work/ConnectOne_Bancorp/Java_Work/SFTP_1/house.bmp",
"C:/Users/IBM_ADMIN/Desktop/Work/ConnectOne_Bancorp/Java_Work/SFTP_2/house.bmp"); */
channelRead.get("C:/Users/IBM_ADMIN/Desktop/Work/ConnectOne_Bancorp/Java_Work/SFTP_1/house.bmp", pout);
channelWrite.put(pin , "C:/Users/IBM_ADMIN/Desktop/Work/ConnectOne_Bancorp/Java_Work/SFTP_2/house.bmp");
channelRead.disconnect();
channelWrite.disconnect();
sessionRead.disconnect();
sessionWrite.disconnect();
} catch( JSchException e) {
e.printStackTrace(); }
catch (SftpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我正在尝试使用JSch库将文件从一个目录(在我的本地计算机中)复制到另一个目录。
但它给了我这个错误:
com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: 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 client.SFTPCode_2.main(SFTPCode_2.java:43)
Caused by: java.net.ConnectException: Connection refused: 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:343)
... 3 more
Process exited with exit code 0.
我尝试添加Cygwin(from link here),但它仍无效。
答案 0 :(得分:2)
不清楚什么行抛出异常。尝试用try / catch包围每个Jsch动作,如下所示:
Session sessionRead = null;
try {
sessionRead = jsch.getSession(user, host, port);
} catch (JSchException e) {
System.out.println("Issue getting session.");
e.printStackTrace();
}
try {
sessionRead.connect(); // do you need to set properties first?
} catch (JSchException e) {
System.out.println("Issue connecting to session.");
e.printStackTrace();
}
对我来说,当我使用Jsch时,我必须添加会话属性来建立连接,例如,在sessionRead.connect()上。
sessionRead.setPassword("password");
sessionRead.setConfig("StrictHostKeyChecking", "no");
sessionRead.connect();