Search code examples
javasftpjsch

JSch sftp job summary


private final String host;
    private final String userAccount;
    private final String keyDir;
    private ChannelSftp sftpChannel;
    private Session session;
    private Channel channel


public void send(List<Path> filesToSend, String destination) throws SftpException, IOException {

    if (sftpChannel == null) {
        logger.error("Failed to create SFTP channel");
    }
    for (Path file : filesToSend) {
        send(file, destination);
    }
     //summary of sent files over sftpchannel
} 

public void send(Path file, String destination) throws SftpException, IOException {

    if (sftpChannel == null) {
        logger.error("Failed to create SFTP channel");
    }
    sftpChannel.put(Files.newInputStream(file), destination + File.separator + file.getFileName());

} //end send



}//end class

After all the files have been sent, can somebody show me how I can get a count of how many number of files have successfully been sent. Not important but also if any failed or any kind of monitoring. How can I do this with Jsch library.

I would like something in my log such as :

Preparing to send [14] files

Number of files sent is [14]...


Solution

  • public void send(List<Path> filesToSend, String destination) {
            logger.debug("Preparing to send ["+filesToSend.size()+"] files");
    
            if (sftpChannel == null) {
                logger.error("Failed to create SFTP channel");
            }
    
            int successCount = 0;
            int failedCount = 0;
    
            for (Path file : filesToSend) {
                try {
                  send(file, destination);
                  successCount++;
                } catch (Exception e) {
                  failedCount++;
                }
            }
            //summary of sent files over sftpchannel
            logger.debug("Successfully sent " + successCount);
            logger.debug("Failed to sent " + failedCount);
        }