簡體   English   中英

Spring集成-逐行讀取遠程文件

[英]spring integration - read a remote file line by line

我正在嘗試使用Spring集成逐行讀取遠程文件。 使用此處找到的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並逐行讀取它。

任何幫助深表感謝。 謝謝!

您可以在接收File和<splitter>之后使用<file-to-string-transformer>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服務中,並使用<service-activator>最后一個。

暫無
暫無

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

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