从SFTP访问文件而不将其下载到本地使用Spring Integration

发布于 2025-01-21 20:00:55 字数 1822 浏览 0 评论 0原文

我目前有以下配置:

    @Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(sftpHost);
    factory.setPort(sftpPort);
    factory.setUser(sftpUser);
    if (null != sftpPrivateKey) {
        factory.setPrivateKey(sftpPrivateKey);
        factory.setPrivateKeyPassphrase(sftpPrivateKeyPassphrase);
    } else {
        factory.setPassword(sftpPassword);
    }
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<>(factory);
}

@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
    SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
    // fileSynchronizer.setDeleteRemoteFiles(true);
    fileSynchronizer.setRemoteDirectory(sftpRemoteDirectory);
    fileSynchronizer
            .setFilter(new SftpSimplePatternFileListFilter(sftpRemoteDirectoryFilter));
    return fileSynchronizer;
}

@Bean
@InboundChannelAdapter(channel = "fromSftpChannel", poller = @Poller(cron = "0/5 * * * * *"))
public MessageSource<File> sftpMessageSource() {
    SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
            sftpInboundFileSynchronizer());
    source.setLocalDirectory(new File(sftpLocalDirectory));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<>());
    return source;
}

@Bean
@ServiceActivator(inputChannel = "fromSftpChannel")
public MessageHandler resultFileHandler() {
    return message -> System.err.println(message.getPayload());
}

该配置将任何内容从远程目录下载到本地目录。但是我有一个REST控制器,我想从SFTP服务器中返回文件的字节数组,而不是将其下载到本地计算机。在Spring Integration/Boot中是否有可能?请问您有一些代码示例吗?

I currently have the following configuration:

    @Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(sftpHost);
    factory.setPort(sftpPort);
    factory.setUser(sftpUser);
    if (null != sftpPrivateKey) {
        factory.setPrivateKey(sftpPrivateKey);
        factory.setPrivateKeyPassphrase(sftpPrivateKeyPassphrase);
    } else {
        factory.setPassword(sftpPassword);
    }
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<>(factory);
}

@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
    SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
    // fileSynchronizer.setDeleteRemoteFiles(true);
    fileSynchronizer.setRemoteDirectory(sftpRemoteDirectory);
    fileSynchronizer
            .setFilter(new SftpSimplePatternFileListFilter(sftpRemoteDirectoryFilter));
    return fileSynchronizer;
}

@Bean
@InboundChannelAdapter(channel = "fromSftpChannel", poller = @Poller(cron = "0/5 * * * * *"))
public MessageSource<File> sftpMessageSource() {
    SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
            sftpInboundFileSynchronizer());
    source.setLocalDirectory(new File(sftpLocalDirectory));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<>());
    return source;
}

@Bean
@ServiceActivator(inputChannel = "fromSftpChannel")
public MessageHandler resultFileHandler() {
    return message -> System.err.println(message.getPayload());
}

This one downloads anything from the remote directory to a local directory. But I have a rest controller and I would like to stream back a byte array of the file from the SFTP server instead of downloading it to a local machine. Is it possible in Spring Integration/Boot? Do you have some code examples, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洋洋洒洒 2025-01-28 20:00:55

由于您说您有一个REST控制器来提出SFTP文件的请求,因此我建议您查看sftpoutboundgateway,这确实是为请求和答复而设计的。请参阅其command.getoption.stream

    /**
     * (-stream) Streaming 'get' (returns InputStream); user must call {@link Session#close()}.
     */
    STREAM("-stream"),

请参阅文档中的更多信息: https://docs.spring.io/spring-integration/docs/current/current/referent/referent/reference/reference/html/html/sftp.html #使用the-get-command

不确定是什么导致您进入sftpinboundfilesynchronizingmessagesource用于您的请求 - 重新任务...

Since you say that you have a REST controller to make requests for SFTP files, then I would recommend to look into an SftpOutboundGateway, which indeed designed for requests and replies. See its Command.GET and Option.STREAM:

    /**
     * (-stream) Streaming 'get' (returns InputStream); user must call {@link Session#close()}.
     */
    STREAM("-stream"),

See more in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#using-the-get-command

Not sure what led you to SftpInboundFileSynchronizingMessageSource for your request-reply task...

万劫不复 2025-01-28 20:00:55

我认为您可以使用 远程filetemplate

I think you can achieve this by using RemoteFileTemplate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文