Ondrej Tokar
Ondrej Tokar

Reputation: 5080

SFTP download didn't finish

I am trying to download file from SFTP using JSCH library 0.1.51 (latest version). During while Loop I get the exception. File is successfully downloaded to data folder of my project, it also has approximately the same size as the one on SFTP, but program never stops executing.

After few minutes of leaving it like that it throws that exception.

EDIT: I found out that if it is just small file then everything is ok. If it is aroung 1MB, but I need to download 20MB and I wouldn't say that might be a problem.

EDIT3: I also used apache library and it does the same thing. Another thing I found out: It downloads almost the whole file except last lines ... Why it is pausing downloading very close to the end - and it isn't size issue.

EDIT4: I tried different SFTP and it has worked... Why?

Another thing, when I upload back to SFTP file, which was downloaded by program and then try to download it again it works ... What the ...?

Could it be something with encoding, or something in the end of the file was wrong? Its just simple csv file so .. I have no idea so far.

CODE:

try {
        JSch jsch = new JSch();
        session = jsch.getSession(SFTPUSER, SFTPHOST);
        session.setPassword(SFTPPASS);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd(SFTPWORKINGDIR);
        byte[] buffer = new byte[1024];
        BufferedInputStream bis = new BufferedInputStream(channelSftp.get("ELQ_contacts.csv"));
        File newFile = new File("data\\ELQ_contacts.csv");
        OutputStream os = new FileOutputStream(newFile);
        BufferedOutputStream bos = new BufferedOutputStream(os);
        int readCount;

        while ((readCount = bis.read(buffer)) > 0) {
            bos.write(buffer, 0, readCount);
            os.flush();
            bos.flush();
        }
        System.out.println("Done.");
        os.close();
        bis.close();
        bos.close();
        session.disconnect();
        channel.disconnect();
        channelSftp.quit();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

ERROR:

    at java.io.PipedInputStream.read(Unknown Source)
at java.io.PipedInputStream.read(Unknown Source)
at java.io.InputStream.skip(Unknown Source)
at com.jcraft.jsch.ChannelSftp.skip(ChannelSftp.java:2894)
at com.jcraft.jsch.ChannelSftp.access$600(ChannelSftp.java:36)
at com.jcraft.jsch.ChannelSftp$RequestQueue.cancel(ChannelSftp.java:1246)
at com.jcraft.jsch.ChannelSftp$2.read(ChannelSftp.java:1402)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at SFTP.SFTP.null(Unknown Source)
at Main.Main.main(Main.java:14)

Upvotes: 2

Views: 1819

Answers (2)

Babatunde Adeyemi
Babatunde Adeyemi

Reputation: 14448

As suggested by vzamanillo, it's better you use the provided method for downloading files using Sftp

channelSftp.get(String src, OutputStream dst);

Below is the updated code that shows it's actual usage:

try {
    JSch jsch = new JSch();
    session = jsch.getSession(SFTPUSER, SFTPHOST);
    session.setPassword(SFTPPASS);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    channel = session.openChannel("sftp");
    channel.connect();
    channelSftp = (ChannelSftp) channel;

    channelSftp.cd(SFTPWORKINGDIR);

    File newFile = new File("data\\ELQ_contacts.csv");
    OutputStream os = new FileOutputStream(newFile);
    channelSftp.get("ELQ_contacts.csv", os);

    os.close();

    session.disconnect();
    channel.disconnect();
    channelSftp.quit();

} catch (Exception ex) {
    ex.printStackTrace();
}

Upvotes: 0

vzamanillo
vzamanillo

Reputation: 10564

I think is better using

channelSftp.get(String src, OutputStream dst);

it is more easy and the method implementation controls the server streams for you.

Hope this helps.

Upvotes: 5

Related Questions