2017-10-18 187 views
0

我是Spring集成的新手。我有這樣的要求,首先將文件從/ files文件夾移動到SFTP位置中的/ process文件夾,然後將該文件複製到本地。 我建議使用網關和配置必須在Java中使用註釋。 我已經嘗試尋找這裏的答案在stackoverflow但找不到相關的東西。 但是我能夠使用@InboundChannelAdapter和配置其他bean來複制文件。在Spring集成中使用Java配置文件從SFTP複製和移動文件

下面是我寫到目前爲止

配置 公共類SftpConfiguration {

@Value("${ftp.file.host}") 
private String host; 

@Value("${ftp.file.port") 
private int port; 

@Value("${ftp.file.user") 
private String user; 

@Value("${ftp.file.password") 
private String password; 

@Value("${directorry.file.remote") 
private String remoteDir; 

@Value("${directorry.file.in.pollerDelay") 
final private String pollerDelay = "1000"; 

@Value("${directory.file.remote.move}") 
private String toMoveDirectory; 

@Bean 
public SessionFactory<LsEntry> sftpSessionFactory() { 
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); 
    factory.setHost(host); 
    factory.setPort(port); 
    factory.setUser(user); 
    factory.setPassword(password); 
    factory.setAllowUnknownKeys(true); 
    return new CachingSessionFactory<LsEntry>(factory); 
} 

@Bean 
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() { 
    SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory()); 
    fileSynchronizer.setDeleteRemoteFiles(false); 
    fileSynchronizer.setRemoteDirectory(remoteDir); 
    fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xlsx")); 
    return fileSynchronizer; 
} 

@Bean 
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = pollerDelay)) 
public MessageSource<File> sftpMessageSource() { 
    SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
      sftpInboundFileSynchronizer()); 
    source.setLocalDirectory(new File("ftp-inbound")); 
    source.setAutoCreateLocalDirectory(true); 
    source.setLocalFilter(new AcceptOnceFileListFilter<File>()); 

    return source; 
} 

@Bean 
@ServiceActivator(inputChannel = "sftpChannel") 
public MessageHandler handler() { 
    return new MessageHandler() { 

     @Override 
     public void handleMessage(Message<?> message) throws MessagingException { 
      try { 
       new FtpOrderRequestHandler().handle((File) message.getPayload()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

    }; 
} 

@Bean 
@ServiceActivator(inputChannel = "sftpChannel") 
public MessageHandler handlerOut() { 
    return new SftpOutboundGateway(sftpSessionFactory(), "mv", toMoveDirectory); 
} 

}

我會感謝任何提示或建議的代碼。 謝謝。

回答

0

對,您需要使用@Bean@InboundChannelAdapterSftpInboundFileSynchronizingMessageSource將文件從遠程/process文件夾下載到本地目錄。這是在poller基礎上完成的,並且是一個單獨的過程,完全與移動操作無關。此舉的邏輯,你可以通過FtpOutboundGatewayMV命令執行:

MV命令沒有選項。

表達屬性定義了「從」路徑和重命名表達屬性定義「到」路徑。 默認情況下,重命名錶達式headers['file_renameTo']。 此表達式不能計算爲空或者爲空String。 如有必要,將創建所需的任何遠程目錄。 結果消息的有效載荷是Boolean.TRUE。 在file_remoteDirectory標題中提供了原始遠程目錄,並且在file_remoteFile標題中提供了文件名。 新路徑位於file_renameTo標題中。

這一個,你必須通過使用方法:

@Bean 
@ServiceActivator(inputChannel = "sftpChannel") 
public MessageHandler handler() { 
    return new SftpOutboundGateway(ftpSessionFactory(), "mv", "'my_remote_dir/my_file'"); 
} 
+0

阿爾喬姆比蘭我在這裏看到你的觀點。所以你建議使用SftpOutboundGateway將我的文件從/ files文件夾移動到/處理文件夾。我的查詢首先是使用@InboundChannelAdapter 從/文件中選擇文件,然後SftpOutboundGateway將它移動到所需的位置說/進程。 現在我想將該文件複製到/進程的本地。 這將如何發生。 我將我寫的代碼添加到我的問題中。請看一看,並建議我應該做些什麼改變,將移動後的文件複製到本地/進程。 –

+0

到目前爲止,我在代碼中看不到問題。但是,如果你談論本地'/ files'目錄,那麼你必須使用'FileReadingMessageSource'並通過'SftpMessageHandler'將這些本地文件發送到遠程'/ process'。另一方面,您肯定可以繼續使用'SftpInboundFileSynchronizingMessageSource'將遠程文件從'/ process'輪詢到本地目錄。否則,請更改您的要求並考慮我的評論,因爲我只是不理解任務 –

+0

您好artem讓我試着再次解釋。考慮你有兩個文件夾在本地調用者/本地的名爲/ initial和/ final的sftp和一個文件夾。現在需求是首先將文件從/ initial移動到/ final。然後將該文件從/ final複製到/ local。我希望這可以幫助你更好地理解問題。現在需求是否認爲上述代碼是正確的。 –

相關問題