SftpClient extends FtpClient { @Override protected FtpMessage retrieveFile(GetCommand command, TestContext context) { try { String remoteFilePath = context.replaceDynamicContentInString(command.getFile().getPath()); String localFilePath = addFileNameToTargetPath(remoteFilePath, context.replaceDynamicContentInString(command.getTarget().getPath())); try (InputStream inputStream = sftp.get(remoteFilePath)) { byte[] bytes = FileCopyUtils.copyToByteArray(inputStream); Path localFilePathObj = Paths.get(localFilePath); Files.createDirectories(localFilePathObj.getParent()); Files.write(localFilePathObj, bytes); } catch (SftpException e) { throw new CitrusRuntimeException(String.format("Failed to get file from FTP server. Remote path: %s. Local file path: %s. Error: %s", remoteFilePath, localFilePath, e.getMessage())); } if (getEndpointConfiguration().isAutoReadFiles()) { String fileContent; if (command.getFile().getType().equals(DataType.BINARY.name())) { fileContent = Base64.encodeBase64String(FileCopyUtils.copyToByteArray(FileUtils.getFileResource(localFilePath).getInputStream())); } else { fileContent = FileUtils.readToString(FileUtils.getFileResource(localFilePath)); } return FtpMessage.result(FTPReply.CLOSING_DATA_CONNECTION, "Transfer complete", localFilePath, fileContent); } else { return FtpMessage.result(FTPReply.CLOSING_DATA_CONNECTION, "Transfer complete", localFilePath, null); } } catch (IOException e) { throw new CitrusRuntimeException("Failed to get file from FTP server", e); } } SftpClient(); protected  SftpClient(SftpEndpointConfiguration endpointConfiguration); @Override SftpEndpointConfiguration getEndpointConfiguration(); @Override void afterPropertiesSet(); @Override void destroy(); JSch getSsh(); void setSsh(JSch ssh);  }
@Test public void testRetrieveFile() { FtpMessage ftpMessage = sftpClient.storeFile(putCommand(localFilePath, remoteFilePath), context); verifyMessage(ftpMessage, PutCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete"); assertTrue(Paths.get(remoteFilePath).toFile().exists()); FtpMessage response = sftpClient.retrieveFile(getCommand(remoteFilePath), context); verifyMessage(response, GetCommandResult.class, CLOSING_DATA_CONNECTION, "Transfer complete"); Assert.assertEquals(response.getPayload(GetCommandResult.class).getFile().getData(), inputFileAsString); }