通过使用springboot项目的spring集成,从sftp服务器下载符合条件的文件

klh5stk1  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(683)

我当前的项目是基于spring集成的。我用springboot开发这个项目。
我的目标是使用spring集成来完成以下任务。
连接到sftp
检查是否在本地文件夹中的特定文件夹中创建了目录
检查特定于(csv&xlsx)的文件的合格文件扩展名
将所有内容从sftp远程目录下载到本地目录&需要跟踪文件传输开始时间和文件传输结束时间。
逐行从本地目录中读取文件并提取特定的列信息。
你能给我一些建议吗?
我怎样才能得到换乘开始时间?注意:这个需求我必须作为restapi开发。请提供一些指导,我如何通过使用spring集成来实现这一点?
谢谢。:)

  1. public class SftpConfig {
  2. @Value("${sftp.host}")
  3. private String sftpHost;
  4. @Value("${sftp.port:22}")
  5. private int sftpPort;
  6. @Value("${sftp.user}")
  7. private String sftpUser;
  8. @Value("${sftp.password:#{null}}")
  9. private String sftpPasword;
  10. @Value("${sftp.remote.directory:/}")
  11. private String sftpRemoteDirectory;
  12. @Value("${sftp.privateKey:#{null}}")
  13. private Resource sftpPrivateKey;
  14. @Value("${sftp.privateKeyPassPhrase:}")
  15. private String privateKeyPassPhrase;
  16. @Value("${sftp.remote.directory.download.filter:*.*}")
  17. private String sftpRemoteDirectoryDownloadFilter;
  18. @Value("${sftp.remote.directory.download:/}")
  19. private String sftpRemoteDirectoryDownload;
  20. @Value("${sftp.local.directory.download:${java.io.tmpdir}/localDownload}")
  21. private String sftpLocalDirectoryDownload;
  22. /*
  23. * The SftpSessionFactory creates the sftp sessions. This is where you define
  24. * the host , user and key information for your sftp server.
  25. */
  26. // Creating session for Remote Destination SFTP server Folder
  27. @Bean
  28. public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
  29. DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
  30. factory.setHost(sftpHost);
  31. factory.setPort(sftpPort);
  32. factory.setUser(sftpUser);
  33. if (sftpPrivateKey != null) {
  34. factory.setPrivateKey(sftpPrivateKey);
  35. factory.setPrivateKeyPassphrase(privateKeyPassPhrase);
  36. } else {
  37. factory.setPassword("sftpPassword");
  38. }
  39. factory.setAllowUnknownKeys(true);
  40. return new CachingSessionFactory<ChannelSftp.LsEntry>(factory);
  41. }
  42. /*
  43. * The SftpInboundFileSynchronizer uses the session factory that we defined above.
  44. * Here we set information about the remote directory to fetch files from.
  45. * We could also set filters here to control which files get downloaded
  46. */
  47. @Bean
  48. public SftpInboundFileSynchronizer SftpInboundFileSynchronizer () {
  49. SftpInboundFileSynchronizer synchronizer = new SftpInboundFileSynchronizer();
  50. return null;
  51. }
yrdbyhpb

yrdbyhpb1#

如果你看看 SftpStreamingMessageSource 取而代之的是:https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-流媒体,加上使用 FileSplitter 要逐行读取该文件(也支持“first like as header”),您不必担心将文件传输到local dir。您将按需使用远程内容完成所有操作。
另一方面,既然您谈到restapi,您可能会有一些 @RestController 或spring集成http入站网关:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#http-入站时,则需要考虑使用 SftpOutboundGateway 带着一个 MGET 命令:https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-出站网关。
如果仍然需要跟踪每个文件的下载时间,则需要考虑使用该网关两次:使用 LIST 指挥与控制 NAME_ONLY ,第二次是 GET 命令。此时您可以添加 ChannelInterceptor 为了 input 以及 output 第二个网关的通道,因此您将有一个文件名信息来关联并捕获该网关前后的开始和停止时间。

相关问题