首頁  >  文章  >  Spring SFTP Intergation:處理將檔案移到另一個目錄

Spring SFTP Intergation:處理將檔案移到另一個目錄

WBOY
WBOY轉載
2024-02-09 14:18:09514瀏覽

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中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除