spring - Spring 与 SFTP 的集成
问题描述
我正在构建一个小微服务来从 SFTP 文件服务器访问文件。我决定使用 Spring Integration SFTP 来完成工作。我是 Spring Integration 的新手,对它的工作原理感到困惑。
我的目标是在 SFTP 服务器上的目录中获取文件列表并将它们呈现给用户界面。从那里,用户将选择一个文件进行下载,我将使用文件名将文件从 SFTP 服务器流式传输到用户界面。
我正在使用以下确实有效的代码。
Entire class to handle SFTP with SSH
@Slf4j
@Configuration
public class SftpConfig {
@Value("${sftp.host}")
private String sftpHost;
@Value("${sftp.port:22}")
private int sftpPort;
@Value("${sftp.user}")
private String sftpUser;
@Value("${sftp.privateKey:#{null}}")
private Resource sftpPrivateKey;
@Value("${sftp.privateKeyPassphrase:}")
private String sftpPrivateKeyPassphrase;
@Value("${sftp.password:#{null}}")
private String sftpPasword;
@Value("${sftp.remote.directory:/}")
private String sftpRemoteDirectory;
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(sftpHost);
factory.setPort(sftpPort);
factory.setUser(sftpUser);
if (sftpPrivateKey != null) {
factory.setPrivateKey(sftpPrivateKey);
factory.setPrivateKeyPassphrase(sftpPrivateKeyPassphrase);
} else {
factory.setPassword(sftpPasword);
}
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<>(factory);
}
@ServiceActivator(inputChannel = "ftpLS")
@Bean
public SftpOutboundGateway getFtpLS() {
SftpOutboundGateway gateway = new SftpOutboundGateway(sftpSessionFactory(), "ls", "'" + sftpRemoteDirectory + "' + payload");
gateway.setOption(AbstractRemoteFileOutboundGateway.Option.NAME_ONLY);
return gateway;
}
@ServiceActivator(inputChannel = "ftpGet")
@Bean
public SftpOutboundGateway getFtpGet() {
SftpOutboundGateway gateway = new SftpOutboundGateway(sftpSessionFactory(), "get", "'" + sftpRemoteDirectory + "' + payload");
gateway.setOption(AbstractRemoteFileOutboundGateway.Option.STREAM);
return gateway;
}
@MessagingGateway(defaultRequestChannel = "ftpLS")
public interface FtpLS {
List list(String directory);
}
@MessagingGateway(defaultRequestChannel = "ftpGet")
public interface FtpGet {
InputStream get(String fileName);
}
}
跑
@Bean
public ApplicationRunner runner(SftpConfig.FtpLS ftpLS, SftpConfig.FtpGet ftpGet) {
return args -> {
List<String> list = ftpLS.list("139");
System.out.println("Result:" + list);
InputStream is = ftpGet.get("139/" + list.get(0));
String theString = IOUtils.toString(is,"UTF-8");
System.out.println("Result:" + theString);
};
}
我的第一个问题是这是正确的方法吗?
其次,我是否需要两个接口才能使用两个不同的 SftpOutboundGateway?
最后,在执行 FtsGet 时是否有更好的方法来传递动态目录名称?现在我正在传递我正在将 139 与基本目录连接在一个字符串中,并通过接口传递它。
解决方案
这是正确的方法吗?
是的,方法是正确的。虽然我建议不要使用isSharedSession
网关,因为它可能会被不同的用户从不同的线程中使用。
我需要两个接口吗?
不,你真的可以有一个@MessagingGateway
,但有几个方法标有自己的@Gateway
注释。
有没有更好的方法来传递动态目录?
不,你的方法是正确的。没有working directory
像我们在 FTP 中那样自动切换的东西。
推荐阅读
- plsql - 如何在pl-sql中获取csv文件的行数
- archer - 如何使用 RSA Archer 中的 Web API 获取交叉引用字段中记录的 ID?
- c++ - 我正在尝试创建一个函数“InOrder”,它在二分搜索中进行中序遍历
- validation - 验证器 textarea 不起作用并始终返回 false
- c# - 即使页面尚未加载并且出现“错误:500 服务器错误”,页面加载也不会引发错误
- php - 以下是一个动态表单代码,它可以通过在行中添加来自用户的多个输入来插入数据,但我希望它在列中。我该怎么做?
- python - 预测时间序列数据中的多个特征(使用键)
- c - 不使用算术运算符减去两个数字
- highcharts - Highcharts JS v3.0.10 的 allowOverlap 属性
- php - 如何获取在 MySQL 中创建的生成外键以用于插入函数