使用JSch从SFTP服务器检索数据

我正在使用JSch通过SFTP从远程机器检索文件。 这是代码

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(); } } } 

现在,我有两个问题:

  • sftpChannel.get("remote-data.txt"); 抛出exception:

    没有这样的文件
    在com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2297)
    在com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:1750)
    在com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1020)
    在com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:995)
    在TestSFTPinJava.main(TestSFTPinJava.java:29)

  • 我不知道如何在本地系统中指定保存文件的位置。 sftpChannel.put("C:\\Users\\mona\\Documents\\localCopy.txt"); 看起来不对我。

请帮忙提出建议,谢谢!

关于你的观点1,我怀疑连接后的默认目录不是你所期望的。 尝试使用绝对远程路径。 sftpChannel.pwd()是否返回远程机器上的文件remote-data.txt所在的目录?

关于你的观点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上的Jsch sftp示例了吗?

应用程序的简单示例。 我从远程服务器(从/ tmp / qtmp)获取文件并将其保存在当前路径的本地计算机中

 package connector; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; 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 Fetcher { public void fetchFile(String username, String host, String passwd) throws JSchException, SftpException, IOException { JSch conn = new JSch(); Session session = null; session = conn.getSession(username, host, 22); session.setPassword(passwd); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelSftp channel = (ChannelSftp)session.openChannel("sftp"); channel.connect(); //change folder on the remote server channel.cd("/tmp/qtmp"); InputStream in = channel.get("testScp"); // set local file String lf = "OBJECT_FILE"; FileOutputStream tergetFile = new FileOutputStream(lf); // read containts of remote file to local int c; while ( (c= in.read()) != -1 ) { tergetFile.write(c); } in.close(); tergetFile.close(); channel.disconnect(); session.disconnect(); } } 

我有一个类似的问题,我试图从一切都很好的服务器获取一些文件,但我总是得到这个错误:

 sftpChannel.get("fil1.txt","file1.txt") error message: 2: No such file at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846) at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2198) at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2215) at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:913) at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:873) ... 

我通过使用正确列出了目录的所有元素

 java.util.Vector v1 = sftpChannel.ls(dir); 

我认为这让我感到困惑,我能够通过使用ls命令读取目录的内容,当你想获取/放置文件时,请确保先使用“cd”移动。

解决方案是使用下一个命令使用简单的cd命令移动到包含我的文件的目录:

 sftpChannel.cd(dir); 

希望这会有所帮助,我花了一些时间来弄明白。 乔乔。

得到教训:

1.- ls可以读取任何目录,无论你是否在其中。 2.-要获取和放置文件,请始终使用cd确保您位于包含文件的目录中。