【问题标题】:How to get files recursively from subdirectories spring-integration如何从子目录中递归获取文件spring-integration
【发布时间】:2021-03-17 22:16:47
【问题描述】:

请告诉我如何从子目录中递归获取文件?

我添加一个表达式来选择一个远程目录到我的 IntegrationFlow - String fileNameRegex = "(\d {4} | 成功 | 错误 |.*.xml)"; ftp.inboundStreamingAdapter(模板())。 RemoteDirectory(新的ValueExpression (fileNameRegex))

但这不起作用,文件只取自根目录。

目录结构如下:

/
1111/
    In/
    Out/
    Error/
    Success/

2222/
    In/
    Out/
    Error/
    Success/

我只需要从 Error and Success *.xml 中获取文件

@Bean
public DelegatingSessionFactory<FTPFile> delegatingSessionFactory() {
    Map<Object, SessionFactory<FTPFile>> factories = new LinkedHashMap<>();
    for (int i = 0; i < this.names.length; i++) {
        DefaultFtpSessionFactory factory = new DefaultFtpSessionFactory();
        factory.setHost(this.hosts[i]);
        factory.setUsername(this.users[i]);
        factory.setPassword(this.pwds[i]);
        factories.put(this.names[i], factory);
    }
    return new DelegatingSessionFactory<>(factories, factories.values().iterator().next());
}

@Bean
public RotatingServerAdvice advice() {
    List<RotationPolicy.KeyDirectory> keyDirectories = new ArrayList<>();
    keyDirectories.add(new RotationPolicy.KeyDirectory(this.names[0], ""));
    keyDirectories.add(new RotationPolicy.KeyDirectory(this.names[1], ""));
    return new RotatingServerAdvice(delegatingSessionFactory(), keyDirectories);
}

@Bean
public FtpRemoteFileTemplate template() {
    return new FtpRemoteFileTemplate(delegatingSessionFactory());
}

@Bean
public IntegrationFlow ftpIntegrationFlow() {
    String fileNameRegex = "(\\d{4}|Success|Error|.*.xml)";
    return IntegrationFlows.from(
            Ftp.inboundStreamingAdapter(template()).remoteDirectory(new ValueExpression<>(fileNameRegex)),
            e -> e.poller(Pollers.fixedDelay(500).advice(advice())))
            .transform(new StreamTransformer("UTF-8"))
            .handle(message -> {
                log.info("Read file: {}", message.getHeaders()
                        .get("file_remoteDirectory" + "/" + message.getHeaders()
                                .get("file_remoteFile")));
                messageService.unmarshall(message);
            })
            .get();
}

【问题讨论】:

    标签: java spring spring-integration spring-integration-ftp


    【解决方案1】:

    尚不支持:https://github.com/spring-projects/spring-integration/issues/3407

    那些深层文件在根目录中是不可见的。您必须将特定子目录的通道适配器配置为工作根。

    【讨论】:

    猜你喜欢
    • 2023-01-31
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 2020-03-13
    相关资源
    最近更新 更多