首页 > 解决方案 > 将大量文件加载到 SFTP 问题

问题描述

我有 Spring 集成 sftp 配置

@Bean
  public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
    final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(false);
    factory.setHost(properties.getHost());
    factory.setPort(properties.getPort());
    factory.setUser(properties.getUser());
    factory.setPassword(properties.getPassword());
    factory.setAllowUnknownKeys(true);

    return factory;

  @Bean
  @ServiceActivator(inputChannel = "toSftpChannel")
  public MessageHandler handler(@Value("${sftp.folder}") final String remoteDirectory) {
    final SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
    handler.setRemoteDirectoryExpressionString(remoteDirectory);
    handler.setAutoCreateDirectory(true);
    return handler;
  }

我需要将 10-20 个 xml 文件上传到 SFTP,(每个文件的大小约为 150MB)

主要问题是我收到 SftpException。InputStream 关​​闭或 Socket 异常。我不能使用 Cashed 会话,因为客户限制了这一点。

另外,我将文件与并行流

Arrays.stream(files)
    .parallel()
    .forEach(sftpService::uploadFile);

那么,将所有文件上传到 sftp 最安全的方法是什么?

标签: spring-bootspring-integration-sftp

解决方案


通过删除并行流并为每次上传打开每个会话来解决


推荐阅读