我是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);
}
}
我會感謝任何提示或建議的代碼。 謝謝。
阿爾喬姆比蘭我在這裏看到你的觀點。所以你建議使用SftpOutboundGateway將我的文件從/ files文件夾移動到/處理文件夾。我的查詢首先是使用@InboundChannelAdapter 從/文件中選擇文件,然後SftpOutboundGateway將它移動到所需的位置說/進程。 現在我想將該文件複製到/進程的本地。 這將如何發生。 我將我寫的代碼添加到我的問題中。請看一看,並建議我應該做些什麼改變,將移動後的文件複製到本地/進程。 –
到目前爲止,我在代碼中看不到問題。但是,如果你談論本地'/ files'目錄,那麼你必須使用'FileReadingMessageSource'並通過'SftpMessageHandler'將這些本地文件發送到遠程'/ process'。另一方面,您肯定可以繼續使用'SftpInboundFileSynchronizingMessageSource'將遠程文件從'/ process'輪詢到本地目錄。否則,請更改您的要求並考慮我的評論,因爲我只是不理解任務 –
您好artem讓我試着再次解釋。考慮你有兩個文件夾在本地調用者/本地的名爲/ initial和/ final的sftp和一個文件夾。現在需求是首先將文件從/ initial移動到/ final。然後將該文件從/ final複製到/ local。我希望這可以幫助你更好地理解問題。現在需求是否認爲上述代碼是正確的。 –