Maxime Buisson - 2013-02-07

Hello there,

I am trying to download a tar file from a SFTP server using JSch.

My method is the following:

public InputStream retrieveFileStream(String directory, String fileName) throws Exception {
    InputStream is = null;
    JSch client = null;
    Session session = null;
    ChannelSftp channelSftp;
    int exitValue;
    try {
        client = new JSch();
        session = client.getSession("...", "...", 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword("...");
        session.connect(TIME_OUT);
        channelSftp = (ChannelSftp) _session.openChannel(PROTOCOL);
        channelSftp.connect();
        channelSftp.cd(directory);
        is = channelSftp.get(fileName);
        exitValue = channelSftp.getExitStatus();
    } finally {
        if (channelSftp != null && channelSftp.isConnected()) {
            channelSftp.disconnect();
            channelSftp.exit();
        }
    }
    return is;
}

InputStream is always empty and exitValue equals -1. No exception is thrown. I don't want to use another get method because I have to send this file to another machine (using put method).

Any idea?