问题描述
我当前的项目基于 Spring Integration。我正在使用 spring Boot 开发这个项目。
我的目标是使用 Spring Integration 来完成以下任务。
-
连接到 SFTP
-
检查是否在本地的特定文件夹中创建了目录
你能给我一些建议吗?
我怎样才能获得转移开始时间? 注意:这个要求我必须开发为一个休息 api。请提供一些指导,我如何通过使用 spring 集成来实现这一目标?
谢谢。 :)
public class SftpConfig {
@Value("${sftp.host}")
private String sftpHost;
@Value("${sftp.port:22}")
private int sftpPort;
@Value("${sftp.user}")
private String sftpUser;
@Value("${sftp.password:#{null}}")
private String sftpPasword;
@Value("${sftp.remote.directory:/}")
private String sftpRemoteDirectory;
@Value("${sftp.privateKey:#{null}}")
private Resource sftpPrivateKey;
@Value("${sftp.privateKeyPassphrase:}")
private String privateKeyPassphrase;
@Value("${sftp.remote.directory.download.filter:*.*}")
private String sftpRemoteDirectoryDownloadFilter;
@Value("${sftp.remote.directory.download:/}")
private String sftpRemoteDirectoryDownload;
@Value("${sftp.local.directory.download:${java.io.tmpdir}/localDownload}")
private String sftpLocalDirectoryDownload;
/*
* The SftpSessionFactory creates the sftp sessions. This is where you define
* the host,user and key information for your sftp server.
*/
// Creating session for Remote Destination SFTP server Folder
@Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(sftpHost);
factory.setPort(sftpPort);
factory.setUser(sftpUser);
if (sftpPrivateKey != null) {
factory.setPrivateKey(sftpPrivateKey);
factory.setPrivateKeyPassphrase(privateKeyPassphrase);
} else {
factory.setPassword("sftpPassword");
}
factory.setAllowUnkNownKeys(true);
return new CachingSessionFactory<ChannelSftp.LsEntry>(factory);
}
/*
* The SftpInboundFileSynchronizer uses the session factory that we defined above.
* Here we set information about the remote directory to fetch files from.
* We Could also set filters here to control which files get downloaded
*/
@Bean
public SftpInboundFileSynchronizer SftpInboundFileSynchronizer () {
SftpInboundFileSynchronizer synchronizer = new SftpInboundFileSynchronizer();
return null;
}
解决方法
如果您查看 .highcharts-tooltip > span {
padding: 10px;
border: 2px solid red;
border-radius: 30px 30px 30px 0;
}
代替:https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-streaming,加上使用 SftpStreamingMessageSource
逐行读取该文件(它也支持“first like as header”) ),您无需担心将文件传输到本地目录。您将按需处理远程内容。
另一方面,既然您谈论 REST API,您可能会有一些 FileSplitter
或 Spring Integration HTTP 入站网关:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-inbound,那么您需要考虑使用 { {1}} 和 @RestController
命令:https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-outbound-gateway。
如果您仍然需要跟踪每个文件的下载时间,您需要考虑使用该网关两次:使用 SftpOutboundGateway
命令和 MGET
,第二次使用 {{1 }} 命令。此时,您可以为第二个网关的 LIST
和 NAME_ONLY
通道添加 GET
,因此您将有一个文件名信息来关联并捕获此网关前后的开始和停止时间.