JSch sftp工作总结

时间:2014-06-26 11:14:43

标签: java sftp jsch

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

发送完所有文件后,有人可以告诉我如何计算已成功发送的文件数量。不重要但也有任何失败或任何类型的监控。如何使用Jsch库执行此操作。

我想在我的日志中添加一些内容,例如:

准备发送[14]个文件

发送的文件数是[14] ......

2 个答案:

答案 0 :(得分:0)

鉴于jsch在发生错误时抛出异常,为什么不只是实现自己的计数器(只是一个int可以工作)?

答案 1 :(得分:0)

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);
    }