【问题标题】:spring integration - read a remote file line by linespring integration - 逐行读取远程文件
【发布时间】:2014-11-14 01:40:10
【问题描述】:

我正在尝试使用 spring 集成逐行读取远程文件。使用找到here 的spring 文档,我已经将我的项目设置为轮询文件并在找到时通过sftp 传输它。我被困在如何一次一行地读取文件内容。

这是我目前用于拉入文件的入站通道适配器设置。

<int-sftp:inbound-channel-adapter id="sftpAdapterAutoCreate"
        session-factory="sftpSessionFactory"
        channel="receiveChannel"
        filename-pattern="*.txt"
        remote-directory="/home/springftp"
        preserve-timestamp="true"
        local-directory="file:C:\sprintftp"
        auto-create-local-directory="true"
        temporary-file-suffix=".writing"
        delete-remote-files="false">
    <int:poller fixed-rate="1000" max-messages-per-poll="1"/>
</int-sftp:inbound-channel-adapter>

<int:channel id="receiveChannel"> 
    <int:queue/> 
</int:channel> 

编辑:澄清一下,我想一次从远程文件中检索一行,然后处理该行的内容,然后检索下一行。类似于为本地文件创建 java.io.inputstream 并逐行读取。

非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: java spring spring-integration


    【解决方案1】:

    您可以在接收文件后使用&lt;file-to-string-transformer&gt; 和&lt;splitter&gt; 将payload 的内容分隔到行列表中。

    更新

    我想从远程文件中一次检索一行,然后处理该行的内容,然后检索下一行。类似于为本地文件创建 java.io.inputstream 并逐行读取。

    好吧,很遗憾,我们没有为此提供高级组件,但您可以尝试使用 RemoteFileTemplate 中的功能:

    RemoteFileTemplate<FTPFile> template = new RemoteFileTemplate<FTPFile>(this.ftpSessionFactory);
    template.setFileNameExpression(new SpelExpressionParser().parseExpression("payload"));
    template.setBeanFactory(mock(BeanFactory.class));
    template.afterPropertiesSet();
    final ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
    template.get(new GenericMessage<String>("ftpSource/ftpSource1.txt"), new InputStreamCallback() {
    
        @Override
        public void doWithInputStream(InputStream stream) throws IOException {
            FileCopyUtils.copy(stream, baos1);
        }
    });
    

    您可以将此代码连接到您的一些 POJO 服务,并将最后一个与 &lt;service-activator&gt; 连接。

    【讨论】:

    • 对不起,我应该澄清一下我的意图。我想一次从远程文件中检索一行,然后处理该行的内容,然后检索下一行。类似于为本地文件创建 java.io.inputstream 并逐行读取。
    • @ArtemBilan 我们不能使用带有-stream 选项的SFTP 出站网关get 命令将文件作为流读取吗? docs.spring.io/spring-integration/reference/html/…
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多