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

发布于 2024-12-01 00:54:13 字数 1731 浏览 1 评论 0原文

我正在使用 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"); 抛出异常:

    <块引用>

    没有这样的文件
    在 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"); 对我来说看起来不正确。

请大家帮忙给点建议,谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风启觞 2024-12-08 00:54:13

关于你的第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)

它确实有一个源文件名和目标文件名参数。

我想您已经看过 Jsch sftp 示例 http://www.jcraft.com /jsch/examples/Sftp.java

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 ?

李不 2024-12-08 00:54:13

应用程序的简单示例。
我从远程服务器(从 /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();   

    }

}

Simple example of app.
I get file from remote server (from /tmp/qtmp) and save it in local machine in the current path

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

    }

}
可可 2024-12-08 00:54:13

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

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 确保您位于包含文件的目录中。

i had a similar issue, i was trying to get some files from a server where everything was fine, but i was getting always this error:

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)
...

I was listing properly all the elements of the directory by using

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

I think that was that confused me, that i was able to read the content of the directory by using the ls command, when you want to get / put files make sure you move first by using "cd".

The solution was to use the next command to move to the directory that contains my files using a simple cd command:

sftpChannel.cd(dir);

Hope this helps, i took my sometime to figure it out. jojo.

Lessons learned:

1.- ls can read any directory no matter if you are not inside of it.
2.- To get and put files always make sure you are in the directory that contains the files by using cd.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文