2015-06-04 56 views
0

使用Spring集成我想在遠程SFTP服務器上一次移動或刪除多個文件或非空文件夾。但我似乎無法在官方Spring docs中找到對此的支持,因爲它似乎不受支持。儘管反正文件並不總是正確的。Spring集成SFTP:刪除或刪除多個文件

我想通過使用int-sftp:outbound-gatewayrm命令與有效載荷的目錄名稱。但它似乎並不奏效。我還沒有嘗試過mv,但我想知道是否有人在Spring Integration中有這種行爲的經驗。

回答

0

從您的問題來看,這不是很清楚:您希望在本地應用程序或遠程應用程序中刪除的文件在SFTP服務器上嗎?

下面是什麼,我有一個我的應用程序的例子,也許可以幫助:傳入消息(與有效載荷文件名)將首先發送到遠程SFTP服務器,然後在本地刪除

<integration:publish-subscribe-channel 
    id="sftpChannel" /> 

<!-- The processed file will be sftped to the server --> 
<sftp:outbound-channel-adapter id="sftpOutboundAdapter" 
    session-factory="sftpSessionFactory" channel="sftpChannel" order="1" 
    charset="UTF-8" remote-file-separator="/" remote-directory="${sftp.remote.directory}" 
    remote-filename-generator-expression="payload.getName()" mode="REPLACE" /> 

<!-- sftped file will be removed from the staging folder --> 
<integration:service-activator 
    input-channel="sftpChannel" output-channel="nullChannel" ref="sftpFileDeleter" 
    method="deleteAfterSftpingFile" order="2" /> 

與SftpFileDeleter是

公共類SftpFileDeleter {

private static final Logger LOGGER = Logger 
     .getLogger(SftpFileDeleter.class); 

@ServiceActivator 
public void deleteAfterSftpingFile(Message<File> fileMessage) throws IOException{ 
    Path fileToDeletePath = Paths.get(fileMessage.getPayload().getAbsolutePath()); 
    Files.delete(fileToDeletePath); 

    LOGGER.info("[SENT]File Sent to Sftp Server and deleted:"+fileToDeletePath.getFileName()); 


} 

}

+0

感謝您的答案。我想要在SFTP服務器上遠程刪除或移動多個文件。你的答案給了我一個想法,即使用Java而不是使用XML來刪除。感謝那! –

0

你看過這個例子嗎?

https://github.com/spring-projects/spring-integration-samples/blob/master/basic/sftp/src/test/resources/META-INF/spring/integration/SftpOutboundGatewaySample-context.xml

看起來這是你想要做什麼。根據文檔(http://docs.spring.io/spring-integration/reference/html/sftp.html,第26.7節),您可能會做錯的一件事是,傳入消息的有效負載不一定包含文件名:您需要注意標題儘管,並且設置正確屬性(在你的情況下file_remoteDirectory/file_remoteFile)與適當的值。

我不知道您當前的配置,但您可能需要在SFTP出站網關之前的消息轉換器,將信息從有效負載移動到消息頭。

+0

這就是我現在這樣做的方式。但是對於10k-25k文件來說太慢了。僅僅因爲它會爲每個要刪除的文件創建一個到SFTP服務器的新連接。感謝您的反饋。 –

+0

假設大部分文件都在類似的目錄中,並且您想要清除所有目錄(而不是逐個文件),您可以使用內部遍歷25k郵件的分離器/聚合器,並將所有目錄保存在List中。聚合器在處理所有消息後觸發後,聚合器可以爲每個目錄釋放一條消息以進行清理,其中file_remoteFile爲「*」。 –

0

我在Spring的頂部寫了自己的代碼解決了這個問題,就像@Vincent_F建議的那樣。首先你必須autowire的SFTP會話工廠,像這樣:

@Autowired 
private DefaultSftpSessionFactory sftpSessionFactory; 

後該會話可以用來在我的情況下,重命名的目錄。這應該可能也適用於默認的Spring DSL或XML,但我無法得到它的工作...這是我自己的代碼與此用例有關:

SftpSession session = sftpSessionFactory.getSession(); 

try { 
    if (!session.exists(sftpConfiguration.getOtherRemoteDirectory())) { 
     throw new FileNotFoundException("Remote directory does not exists... Continuing"); 
    } 

    for (ChannelSftp.LsEntry entry : session.list(sftpConfiguration.getRemoteDirectory())) { 
     if (entry.getFilename().equalsIgnoreCase(sftpConfiguration.getOtherDirectory())) { 
      session.rename(sftpConfiguration.getOtherRemoteDirectory(), String.format("%s%s-%s", 
        sftpConfiguration.getRemoteDirectory(), sftpConfiguration.getReportDirectory(), 
        this.createDate(new Integer(entry.getAttrs().getMTime()).longValue() * 1000))); 
     } 
    } 
} catch (FileNotFoundException e) { 
    logger.error(e.getMessage()); 
} catch (IOException e) { 
    logger.error("Could not rename remote directory.", e); 
}