目录
问题内容
解决方法
首页 Java Spring SFTP Intergation:处理将文件移动到另一个目录

Spring SFTP Intergation:处理将文件移动到另一个目录

Feb 09, 2024 pm 02:18 PM

php小编小新介绍Spring SFTP Integration是一个强大的框架,可以帮助开发者处理将文件从一个目录移动到另一个目录的任务。无论是在本地服务器还是远程服务器,这个框架都能轻松实现文件的移动和管理。它提供了一系列的API和配置选项,使开发者能够自定义移动文件的逻辑,并且能够处理各种异常情况。无论是在企业级应用程序中还是在个人项目中,Spring SFTP Integration都是一个非常实用和值得探索的工具。

问题内容

我是 spring intergation 的新手。我需要从 user1/upload 目录处理来自 sftp 服务器的文件,然后将它们移动到 user1/processed 目录。我的代码总体工作正常,但有两个问题:

  1. 当我重新启动应用程序时,目录 user1/processed 以及之前存在的所有文件都会被删除。我只想在那里写入更多文件,而不是每次都清空目录。

  2. 每次我启动应用程序时,我都会收到文件(正如我看到的它们的名称一样,我打印到控制台)我收到旧文件,它们已经被移动到已处理的目录中。这看起来真的很奇怪,因为当我通过 winscp 等其他工具连接到 sftp 时看不到这些文件。旧文件列表是否已在某处兑现?

@Value("${cielo.sftp.host}")
    private String sftpHost;
    @Value("${cielo.sftp.port}")
    private int sftpPort;
    @Value("${cielo.sftp.user}")
    private String sftpUser;
    @Value("${cielo.sftp.pass}")
    private String sftpPasword;
    @Value("${cielo.sftp.remotedir}")
    private String sftpRemoteDirectoryDownload;

    @Value("${cielo.sftp.localdir}")
    private String sftpLocalDirectoryDownload;
    @Value("${cielo.sftp.filter}")
    private String sftpRemoteDirectoryDownloadFilter;

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public SessionFactory<LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost(sftpHost);
        factory.setPort(sftpPort);
        factory.setUser(sftpUser);
        factory.setPassword(sftpPasword);

        factory.setAllowUnknownKeys(true); //Set to true to allow connections to hosts with unknown (or changed) keys. Its default is 'false'. If false, a pre-populated knownHosts file is required.
        return new CachingSessionFactory<>(factory);
    }


    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE - 1)
    public SftpInboundFileSynchronizer sftpInboundFileSynchronizer(final SessionFactory<LsEntry> sftpSessionFactory) {
        SftpInboundFileSynchronizer fileSynchronizer =
                new SftpInboundFileSynchronizer(sftpSessionFactory);
        fileSynchronizer.setDeleteRemoteFiles(false);
        fileSynchronizer.setRemoteDirectory(sftpRemoteDirectoryDownload);
        fileSynchronizer
                .setFilter(new SftpSimplePatternFileListFilter(sftpRemoteDirectoryDownloadFilter)); //todo maybe use RegexPatternFileListFilter?
        return fileSynchronizer;
    }

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE - 2)
    @InboundChannelAdapter(channel = "fromSftpChannel", poller = @Poller(fixedDelay = "1000"))
    //@InboundChannelAdapter(channel = "fromSftpChannel", poller = @Poller(cron = "${cielo.sftp.poller.cron}"))

    public MessageSource<File> sftpMessageSource(final SftpInboundFileSynchronizer sftpInboundFileSynchronizer) {
        SftpInboundFileSynchronizingMessageSource source =
                new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer);
        source.setLocalDirectory(new File("/tmp/local"));
        source.setAutoCreateLocalDirectory(true);
        source.setLocalFilter(new AcceptOnceFileListFilter<File>());
        return source;
    }

    @Bean
    @ServiceActivator(
            inputChannel = "fromSftpChannel")
    public MessageHandler resultFileHandler() {
        return new MessageHandler() {
            @Override
            public void handleMessage(final Message<?> message) throws MessagingException {
                String payload = String.valueOf(message.getPayload());
                System.err.println(payload);
            }
        };
    }

    private static final SpelExpressionParser PARSER = new SpelExpressionParser();

    @Bean(name="fromSftpChannel")
    public MessageChannel fromSftpChannel() {
        return new PublishSubscribeChannel();
    }

    @Bean
    @ServiceActivator(inputChannel = "fromSftpChannel")
    @Order(Ordered.LOWEST_PRECEDENCE)
    public MessageHandler moveFile() {
        SftpOutboundGateway sftpOutboundGateway = new  SftpOutboundGateway(sftpSessionFactory(), AbstractRemoteFileOutboundGateway.Command.MV.getCommand(), "'/user1/upload/'.concat(" + PARSER.parseExpression("payload.getName()").getExpressionString() + ")");
        sftpOutboundGateway.setRenameExpressionString("'/user1/processed/'.concat(" + PARSER.parseExpression("payload.getName()").getExpressionString() + ")");
        sftpOutboundGateway.setRequiresReply(false);
        sftpOutboundGateway.setOutputChannelName("nullChannel");
        sftpOutboundGateway.setOrder(Ordered.LOWEST_PRECEDENCE);
        sftpOutboundGateway.setAsync(true);
        return sftpOutboundGateway;
    }
登录后复制

感谢您的帮助!

我查看了 spring 集成中的示例 - sftp 在复制后重命名或移动远程服务器中的文件,这对我帮助很大

我还检查了官方 spring intergation sftp 文档

解决方法

我不确定启动时如何清除远程目录。需要在您这边调试该行为。但我可以告诉你为什么会看到旧文件。您在处理后进行远程重命名,但这些文件的本地副本仍然存储在 source.setLocalDirectory(new File("/tmp/local")); 上。完成重命名或重新启动后,请考虑清理它。

您还可以查看 SftpStreamingMessageSource 来代替您的逻辑: https ://docs.spring.io/spring-integration/reference/sftp/streaming.html

以上是Spring SFTP Intergation:处理将文件移动到另一个目录的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)