Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving data from an SFTP server using JSch

Tags:

java

sftp

jsch

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!

like image 357
mona Avatar asked Aug 24 '11 19:08

mona


People also ask

What is JSch in SFTP?

With SFTP, you connect using SSH and then transfer files with SFTP. To do this, you can use the JSch (Java secure channel) library.

How do I transfer files using JSch?

JSch library provides the get() and put() method to transfer file between server and client. The put() method is used to transfer files from a local system to a remote server. Add the jsch dependency to the pom. xml file.

What is Jcraft JSch?

JSch is a pure Java implementation of SSH2. JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs. JSch is licensed under BSD style license.

What is JSch connection?

JSch. JSch is the Java implementation of SSH2 that allows us to connect to an SSH server and use port forwarding, X11 forwarding, and file transfer. Also, it is licensed under the BSD style license and provides us with an easy way to establish an SSH connection with Java.


2 Answers

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

    }

}
like image 35
Mikro Koder Avatar answered Sep 28 '22 09:09

Mikro Koder


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 ?

like image 119
Andre Holzner Avatar answered Sep 28 '22 11:09

Andre Holzner