Example case, when I get file from remote server and save it in local machine
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 Main {
public static void main(String[] args) throws JSchException, SftpException, IOException {
// TODO Auto-generated method stub
String username = "XXXXXX";
String host = "XXXXXX";
String passwd = "XXXXXX";
JSch conn = new JSch();
Session session = null;
session = conn.getSession(username, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channel = null;
channel = (ChannelSftp)session.openChannel("sftp");
channel.connect();
channel.cd("/tmp/qtmp");
InputStream in = channel.get("testScp");
String lf = "OBJECT_FILE";
FileOutputStream tergetFile = new FileOutputStream(lf);
int c;
while ( (c= in.read()) != -1 ) {
tergetFile.write(c);
}
in.close();
tergetFile.close();
channel.disconnect();
session.disconnect();
}
}