2014-09-19 29 views
0

我正在嘗試使用彈簧集成逐行讀取遠程文件。使用找到的spring文檔here我已經建立了我的項目來輪詢文件,並在找到時通過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並逐行讀取它。

任何幫助,非常感謝。謝謝!

回答

2

您可以在接收文件後使用<file-to-string-transformer>,並使用<splitter>將​​的內容分隔到行列表中。

UPDATE

我想在從遠程文件的時間以檢索一個線,然後處理該行的內容,然後檢索下一行。類似於爲本地文件創建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>連接最後一個。

+0

對不起,我應該澄清我的意圖。我想從遠程文件中一次檢索一行,然後處理該行的內容,然後檢索下一行。類似於爲本地文件創建java.io.inputstream並逐行讀取它。 – Chris 2014-09-19 13:01:33

+0

在此事上增加了示例代碼 – 2014-09-19 13:14:59

+0

@ArtemBilan我們不能使用SFTP出站網關'get'命令和'-stream'選項來將文件讀取爲流? http://docs.spring.io/spring-integration/reference/html/sftp.html#sftp-outbound-gateway – 2016-01-06 06:19:56