当前位置: 首页 > 知识库问答 >
问题:

Spring与SFTP集成

元英朗
2023-03-14

我正在构建一个小微服务来访问来自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与基目录连接在一个字符串中,并通过接口传递它。

共有1个答案

唐海阳
2023-03-14

这是正确的做法吗?

是的,方法是正确的。虽然我建议不要对网关使用isSharedSession,因为它可能被不同的用户从不同的线程使用。

我需要两个接口吗?

不,你的做法是正确的。没有像工作目录这样的东西可以自动切换,就像我们在FTP中可以做的那样。

 类似资料:
  • 我正在使用Spring集成文件/sftp模块,如何避免下载部分文件?我无法控制将文件推送到ftp/sftp的外部进程。

  • 我使用Spring Integration的SFTP和出站通道适配器将文件上传到远程位置。当它将文件发送到一个SFTP位置时,它可以正常工作。然而,在我的代码中,我试图基于不同的标准发送到多个SFTP位置。 以下是我的设置-从Spring集成文件以下 我的问题是: 有人知道如何配置多个SFTP会话吗 谢谢

  • 我在springboot项目1.5.10版中工作。释放 我正在为sftp使用Spring集成。以下gradle依赖项对我来说一切都很好 我还将普罗米修斯整合到模块中。 我已经完成了普罗米修斯的所有其他要求。但是我没有得到普罗米修斯的指标。 项目中的所有其他服务都在使用prometheus,但没有使用spring integration sftp,prometheus正在为所有这些服务工作。 我尝试

  • 我有一个用例,用户将多个csv文件放到远程目录中,然后放置ready.txt来指示文件已准备好使用。当我们的applcation在远程目录中看到ready.txt文件时,它应该开始使用sftp文件入站通道适配器将所有文件复制到本地目录,包括ready.txt。是否有办法确保readt.txt文件是最后一个要复制到本地目录的文件? 因为当文件从远程目录复制到本地目录时,我有另一个文件入站通道适配器在

  • 我需要将不同的文件sftp到服务器上。 在传递范围内的文件位于Windows服务器中。 为此,我考虑使用Spring集成适配器,因为我不仅需要交付文件,而且还需要为每个文件交付将元数据信息写入数据库。 您知道如何使用Spring集成将Windows文件共享上的多个文件sftp到其他服务器吗?

  • 我有几个hickup设置私钥为Spring集成SFTP。 我想我可以在这里分享我的发现。 我在别处读到,我应该用私钥参数化JSch对象。然而,这并不奏效: 结果异常: