【发布时间】:2015-08-04 12:50:37
【问题描述】:
我想通过 SFTP 传输文件。我发现了how to transfer a file through SFTP in java? 提出的类似问题。我从本地主机上的帖子中尝试了建议的解决方案。但我在输出下显示以下错误。
输出
preparing the host information for sftp.
Host connected.
sftp channel opened and connected.
2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2340)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)
at ScpTo.main(ScpTo.java:69)
File transfered successfully to host.
sftp Channel exited.
Channel disconnected.
Host Session disconnected.
代码
import java.io.File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.jcraft.jsch.Channel;
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;
public class ScpTo {
/**
*
*/
public ScpTo() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
String SFTPHOST = "192.168.1.3";
int SFTPPORT = 22;
String SFTPUSER = "sam-PC";
String SFTPPASS = "";
String SFTPWORKINGDIR = "C:/Users/sam/Desktop/hi.txt";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
System.out.println("preparing the host information for sftp.");
JSch jsch = new JSch();
try {
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
try {
session.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Host connected.");
try {
channel = session.openChannel("sftp");
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
channel.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("sftp channel opened and connected.");
channelSftp = (ChannelSftp) channel;
try {
channelSftp.cd(SFTPWORKINGDIR);
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File f = new File(SFTPWORKINGDIR);
try {
channelSftp.put(new FileInputStream(f), f.getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File transfered successfully to host.");
channelSftp.exit();
System.out.println("sftp Channel exited.");
channel.disconnect();
System.out.println("Channel disconnected.");
session.disconnect();
System.out.println("Host Session disconnected.");
}
}
【问题讨论】:
-
从 not 立即捕获所有异常开始。在寻求帮助之前,先了解自己想要做什么,自己缩小问题范围。
-
帖子已更新。这似乎是 session.connect() 的问题;
-
服务器启动了吗?尝试运行
netstat -ano并检查是否有进程在监听22端口。 -
@龙华。我会试试看。感谢您的建议
-
尝试使用任何其他客户端(如 FireFox + FireFTP)首先访问 localhost:22。