簡體   English   中英

使用 Spring 集成從子目錄中存在的遠程服務器遞歸讀取文件

[英]Recursively read files from remote server present in subdirectories with Spring Integration

我有使用入站采用者從遠程服務器中存在的單個文件夾獲取文件的工作流程,但我想獲取任何遠程服務器父文件夾中存在的所有子文件夾的文件我有這樣的代碼

 @Bean
    public SessionFactory<SftpClient.DirEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost("localhost");
        factory.setPort(port);
        factory.setUser("foo");
        factory.setPassword("foo");
        factory.setAllowUnknownKeys(true);
        factory.setTestSession(true);
        return new CachingSessionFactory<>(factory);
    }
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
    SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setRemoteDirectory("foo");
    fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xml"));
    return fileSynchronizer;
}

@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> sftpMessageSource() {
    SftpInboundFileSynchronizingMessageSource source =
            new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
    source.setLocalDirectory(new File("sftp-inbound"));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    source.setMaxFetchSize(1);
    return source;
}

@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
    return new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            System.out.println(message.getPayload());
        }

    };
}`

但我想獲取 foo 目錄中存在的所有子文件夾的文件,而不是單個文件夾

如果可能,請幫助提供完整代碼

同步器不支持遞歸。 請改用帶有遞歸mget命令的出站網關。

https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-outbound-gateway

使用 mget 命令 mget 根據模式檢索多個遠程文件並支持以下選項:

-P:保留遠程文件的時間戳。

-R:遞歸檢索整個目錄樹。

-x:如果沒有文件與模式匹配則拋出異常(否則返回空列表)。

-D:傳輸成功后刪除每個遠程文件。 如果忽略傳輸,則不會刪除遠程文件,因為 FileExistsMode 為 IGNORE 且本地文件已存在。

mget 操作產生的消息負載是一個List<File> object(即,一個File對象列表,每個對象代表一個檢索到的文件)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM