I am stuck with the problem. I have a folder om my sftp server with a file in it: folder/file.txt . What i'm trying to do is simply to move this file to another directory : folder/subfolder/file.txt. In documentation it was written that you simply need to use OutboundGateway with the MV command. That's ok but the main problem is that i don`t know exactly what would be the name of the file, so i need to provide this name dynamically. How can i do that?
@Bean
@InboundChannelAdapter(value = "toSftpChannel",
poller = @Poller(fixedDelay = "60000", maxMessagesPerPoll = "-1"))
public MessageSource<File> sftpMessageSource() {
SftpInboundFileSynchronizingMessageSource source =
new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
source.setLocalDirectory(new File(localDirectory));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new AcceptOnceFileListFilter<File>());
return source;
}
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(getSftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(false);
fileSynchronizer.setRemoteDirectory("/folder");
fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.txt"));
return fileSynchronizer;
}
@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
public SftpOutboundGateway moveFileHandler() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(getSftpSessionFactory(), "mv", "'folder/" + "file.txt" + "'");
sftpOutboundGateway.setRenameExpression(new LiteralExpression("/folder/subfolder/" + "file.txt"));
return sftpOutboundGateway;
}
Instead of using literal expressions, use a dynamic expression.
e.g. instead of
"'folder/" + "file.txt" + "'"
use
"'folder/' + headers['file_relativePath']"
and
sftpOutboundGateway.setRenameExpression(parser.parseExpression("'/folder/subfolder/' + headers['file_relativePath']";
(The relative path header is set up by the inbound adapter).