使用 Spring mina SFTP 覆盖文件

问题描述 投票:0回答:1

我已经根据Spring网页上的示例创建了一个客户端

https://docs.spring.io/spring-integration/reference/sftp/inbound.html#configuring-with-java-configuration

我确实将 setMaxFetchSize 更改为 -1 以从远程站点传输所有文件,并将 SftpSimplePatternFileListFilter 更改为“*.html”,因为我想传输网页。

问题是代码的第一次运行会传输文件,但后续运行会显示以下消息

远程文件“xxxxx.html”尚未传输到现有的 本地文件“sftp-inbound\xxxxxx.html”。考虑删除本地 文件。

每 5 分钟生成一次网页并传输到 SFTP 服务器。如果服务器上存在文件的新版本,我想覆盖本地文件。我查看了 Spring 文档,但没有看到定义的方法来执行此操作。如何覆盖本地文件?

spring client sftp apache-mina
1个回答
0
投票

我是 Spring 的初学者,我确实发现该文档很有用,但其中有很多未提及的内容,我需要从其他地方找到。

我最终得到了使用 Java 配置进行配置使用 Java DSL 进行配置(下面的链接)的混搭,因为我想保留来自服务器的文件的时间戳。 @Poller注释似乎没有保留时间戳的选项:

https://docs.spring.io/spring-integration/reference/sftp/inbound.html#configuring-with-the-java-dsl

使用 Java DSL 进行配置似乎在类中缺少以下声明:

private DefaultSftpSessionFactory sftpSessionFactory;

Sftp.inboundAdapter 中,我添加了以下过滤器 - 默认的 AcceptOnceFileListFilter 似乎只在第一次传输文件。如果文件在远程系统上更新,则不会再次下载。

                                .filter(new AcceptAllFileListFilter<>())

因为我想传输所有更新的文件,所以我将 maxFetchSize 设置为 -1(负数表示所有文件)

                                .maxFetchSize(-1)

这仍然没有传输所有更新的文件,所以我还必须将以下内容添加到轮询器

                                        .maxMessagesPerPoll(-1)))

最终的 IntegrationFlow 如下 - 请注意 remote_dir 是课程中前面设置的 String

        return IntegrationFlow
                .from(Sftp.inboundAdapter(this.sftpSessionFactory)
                                .preserveTimestamp(true)
                                .maxFetchSize(-1)
                                .deleteRemoteFiles(false)
                                .filter(new AcceptAllFileListFilter<>())
                                .remoteDirectory(remote_dir)
                                .patternFilter("*.html")
                                .localDirectory(new File("html")),
                        e -> e.id("sftpInboundAdapter")
                                .autoStartup(true)
                                .poller(Pollers
                                        .fixedDelay(300000)
                                        .maxMessagesPerPoll(-1)))
                .handle(m -> System.out.println("Transferring updated file " + m.getPayload()))
                .get();
© www.soinside.com 2019 - 2024. All rights reserved.