0

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;
    }

1 Answers1

1

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).

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • thanks a lot! It's actually works, but now it deletes other files in subfolder even though i set in file synchronizer setDeleteRemoteFiles to false, am i doing smth wrong? – Alexei Stirbul May 30 '19 at 14:26
  • Sorry, it's not clear what you mean by "other files". Please add more details. – Gary Russell May 30 '19 at 14:58
  • i'm sorry to bother you, i've fixed this issue - the problem was that setDeleteRemoteFiles line was commented) Thank you for your help!!! It's highly appreciated! – Alexei Stirbul May 30 '19 at 15:14