使用JSch从SFTP服务器检索数据 [英] Retrieving data from an SFTP server using JSch
问题描述
我正在使用JSch通过SFTP从远程计算机检索文件。这是代码
公共类TestSFTPinJava {
public static void main(String args []) {
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(username,sftp.abc.com,22);
session.setConfig(StrictHostKeyChecking,no);
session.setPassword(password);
session.connect();
频道频道= session.openChannel(sftp);
channel.connect();
ChannelSftp sftpChannel =(ChannelSftp)频道;
System.out.println(目录:+ sftpChannel.pwd());
sftpChannel.cd(remoteDirectory /);
System.out.println(cd后的目录:+ sftpChannel.pwd());
sftpChannel.get(remote-data.txt);
sftpChannel.put(C:\\Users \\ mona \\ Documents \\ local-copy.txt);
sftpChannel.exit();
session.disconnect();
} catch(JSchException e){
e.printStackTrace();
} catch(SftpException e){
e.printStackTrace();
}
}
}
现在,我有两个问题:
-
sftpChannel.get(remote-data.txt);
引发异常:
没有这样的文件
com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp)中的
.java:2297)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:1750)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java :1020)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:995)
at TestSFTPinJava.main(TestSFTPinJava.java:29)
-
我不知道如何在本地系统中指定保存文件的位置。
sftpChannel.put(C:\\Users\\mona \\Documents\\localCopy.txt);
确实不适合我。
请帮助提出建议,谢谢!
关于你的观点1,我怀疑连接后的默认目录不是你所期望的。尝试使用绝对远程路径。 sftpChannel.pwd()
是否返回远程机器上的文件 remote-data.txt
所在的目录? / p>
关于你的观点2,看 http://grepcode.com/file/repo1.maven.org/maven2/com.jcraft/jsch/0.1.42/ com / jcraft / jsch / ChannelSftp.java#290 人们看到 ChannelSftp
中有以下方法:
public void put(String src,String dst)
确实有一个源和目标文件名参数。
我想你已经看过了 http://www.jcraft.com/jsch/examples/Sftp.java ?
I am using JSch for retrieving a file from a remote machine by SFTP. Here is the code
public class TestSFTPinJava {
public static void main(String args[]) {
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("username", "sftp.abc.com", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("password");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
System.out.println("Directory:" + sftpChannel.pwd());
sftpChannel.cd("remoteDirectory/");
System.out.println("Directory after cd:" + sftpChannel.pwd());
sftpChannel.get("remote-data.txt");
sftpChannel.put("C:\\Users\\mona\\Documents\\local-copy.txt");
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
Now, I have two questions:
sftpChannel.get("remote-data.txt");
throws an exception:no such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2297)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:1750)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1020)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:995)
at TestSFTPinJava.main(TestSFTPinJava.java:29)I am not sure how to specify the location in my local system where the file will be saved.
sftpChannel.put("C:\\Users\\mona\\Documents\\localCopy.txt");
does not look right to me.
Please help with suggestions, Thanks!
Concerning your point 1, I suspect that the default directory after connecting is not what you expect. Try using an absolute remote path. Does sftpChannel.pwd()
return the directory the file remote-data.txt
is in on the remote machine ?
Concerning your point 2, looking at http://grepcode.com/file/repo1.maven.org/maven2/com.jcraft/jsch/0.1.42/com/jcraft/jsch/ChannelSftp.java#290 one sees that there is the following method in ChannelSftp
:
public void put(String src, String dst)
which indeed has a source and destination file name argument.
I guess you had already a look the Jsch sftp example at http://www.jcraft.com/jsch/examples/Sftp.java ?
这篇关于使用JSch从SFTP服务器检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!