通过使用spring引导项目使用spring Integration从SFTP服务器下载符合条件的文件



我当前的项目是基于Spring Integration的。我正在使用springboot开发这个项目。

我的目标是使用Spring Integration来完成以下任务。

  1. 连接到SFTP

  2. 检查目录是否在本地特定文件夹中创建

  3. 检查特定于(CSV&XLSX(的文件的合格文件扩展名

  4. 将所有内容从SFTP远程目录下载到本地目录&需要跟踪文件传输开始时间和文件传输结束时间。

  5. 逐行读取本地目录中的文件并提取特定的列信息。

你能给我一些建议吗?

我该如何获得转账开始时间?注意:这个需求我必须作为一个rest api来开发。请提供一些指导,我如何使用弹簧集成来实现这一点?

谢谢。:(

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;

}

如果您转而查看SftpStreamingMessageSource:https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-流式传输,再加上使用FileSplitter逐行读取该文件(它也支持"first-like as header"(,您不需要担心将文件传输到本地目录。您将根据需要对远程内容执行所有操作。

另一方面,由于您谈论的是REST API,您可能会有一些@RestController或Spring Integration HTTP Inbound Gateway:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-inbound,则需要考虑使用SftpOutboundGatewayMGET命令:https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-出站网关。

如果仍然需要跟踪每个文件的下载时间,则需要考虑使用该网关两次:使用LIST命令和NAME_ONLY,第二次使用GET命令。此时,您可以为第二个网关的inputoutput通道添加ChannelInterceptor,因此您将有一个文件名信息来关联并捕获此网关前后的开始和停止时间。

最新更新