gpt4 book ai didi

java - 在我的 Java SFTP 类中,为什么会收到此 java.net.ConnectException?

转载 作者:行者123 更新时间:2023-12-01 11:06:58 25 4
gpt4 key购买 nike

我正在使用名为 JSch 的 Java SFTP 库,这是我的代码:

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 ),但仍然不起作用。

最佳答案

尚不清楚哪一行抛出异常。尝试用 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 时,我必须添加 session 属性来建立连接,例如在 sessionRead.connect() 上。

        sessionRead.setPassword("password");
sessionRead.setConfig("StrictHostKeyChecking", "no");
sessionRead.connect();

关于java - 在我的 Java SFTP 类中,为什么会收到此 java.net.ConnectException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32848818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com