#java #sftp #jsch
#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]…
Комментарии:
1. пожалуйста, опубликуйте больше кода. Неясно, что вы пытаетесь реализовать.
Ответ №1:
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);
}
Ответ №2:
Учитывая, что jsch генерирует исключения при возникновении ошибки, почему бы вам просто не реализовать свой собственный счетчик (будет работать только int)?