Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading files from an SFTP server using JSch

Tags:

java

sftp

jsch

I am using jsch to download files from server, my code below.

public static void downloadFile(TpcCredentialsDTO dto) {
        logger.trace("Entering downloadFile() method");

    Session session = null;
    Channel channel = null;
    ChannelSftp channelSftp = null;
    boolean success = false;

    try {
        JSch jsch = new JSch();
        session = jsch.getSession(dto.getUsername(), dto.getHost(),
                dto.getPort());
        session.setPassword(dto.getPassword());

        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
        logger.info("Connected to " + dto.getHost() + ".");

        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;

        List<String> filesToDownload = getFilesToDownload(dto,channelSftp);

        if (!filesToDownload.isEmpty()) {
            for (String fileDownloadName : filesToDownload) {
                success = false;
                OutputStream output = new FileOutputStream(
                    "C:\Download\BLT_03112012");

                channelSftp.get("BLT_03112012",output);
                success = true;
                if (success)
                    logger.info(ServerConstants.DOWNLOAD_SUCCESS_MSG
                                    + fileDownloadName);
                output.close();
            }

        }else {
            logger.info(ServerConstants.NO_FILES_TO_DOWNLOAD
                    + ServerUtils.getDateTime());
            success = true;
        }

    } catch (JSchException ex) {
        logger.error( ServerConstants.SFTP_REFUSED_CONNECTION, ex);
    } catch (SftpException ex) {
        logger.error(ServerConstants.FILE_DOWNLOAD_FAILED, ex);
    } catch (IOException ex) {
        logger.error(ServerConstants.FILE_NOT_FOUND, ex);
    }catch (Exception ex) {
        logger.error(ServerConstants.ERROR, ex);
    }finally {
        if (channelSftp.isConnected()) {
            try {
                session.disconnect();
                channel.disconnect();
                channelSftp.quit();
                logger.info( ServerConstants.FTP_DISCONNECT);
            } catch (Exception ioe) {
                logger.error(ServerConstants.FTP_NOT_DISCONNECT, ioe);
            }
        }
    }
    logger.trace("Exiting downloadFile() method");
}


sftpChannel.get(filename, outputstream) is throwing an error.
  2: File not found
      at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2629)
      at com.jcraft.jsch.ChannelSftp._get(ChannelSftp.java:977)
      at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:946)
      at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:924)
      at za.co.tpc.sftpserver.SftpConnection.downloadFile(SftpConnection.java:72)
      at za.co.tpc.server.execute.FtpMtn.main(FtpMtn.java:44)

The same code does download Text Document file types but fails for 'File' filetype

like image 874
FaithN Avatar asked Dec 03 '12 09:12

FaithN


People also ask

How do I connect SFTP to JSch?

JSch jsch = new JSch(); Session session = jsch. getSession(user, host); session. setPassword(password); session. connect(); ChannelSftp sftpChannel = (ChannelSftp) session.

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.


1 Answers

Try using paths instead of stream:

String destPath = "filename.txt";        

if (!filesToDownload.isEmpty()) {
    for (String fileDownloadName : filesToDownload) {
        success = false;
        sftpChannel.get(fileDownloadName , destPath);  

If you wanna use file and streams check this example:
http://kodehelp.com/java-program-for-downloading-file-from-sftp-server/

like image 132
Carlos Landeras Avatar answered Sep 25 '22 10:09

Carlos Landeras