Retrieving data from an SFTP server using JSch

≡放荡痞女 提交于 2019-12-03 08:12:43

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 ?

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

    }

}

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!