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