Spring 集成 - SFTP 在复制后重命名或移动远程服务器中的文件

Spring integration - SFTP rename or move file in remote server after copying

我正在尝试移动或重命名远程文件,而不是在下载后删除远程文件,我发现可以通过出站网关移动命令来完成,但找不到正确的方法。

下载后请帮忙重命名文件

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SessionFactory<LsEntry> sftpSessionFactory(
        final DataloadServiceProperties DataloadServiceProperties) {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(DataloadServiceProperties.getSftpHost());
    factory.setPort(DataloadServiceProperties.getSftpPort());
    factory.setUser(DataloadServiceProperties.getSftpUser());
    if (DataloadServiceProperties.getSftpPrivateKey() != null) {
        factory.setPrivateKey(DataloadServiceProperties.getSftpPrivateKey());
        factory.setPrivateKeyPassphrase(
                DataloadServiceProperties.getSftpPrivateKeyPassphrase());
    }
    else {
        factory.setPassword(DataloadServiceProperties.getSftpPasword());
    }
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(factory);
}

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE - 1)
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer(
        final SessionFactory<LsEntry> sftpSessionFactory,
        final DataloadServiceProperties DataloadServiceProperties) {
    SftpInboundFileSynchronizer fileSynchronizer =
            new SftpInboundFileSynchronizer(sftpSessionFactory);
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setRemoteDirectory(
            DataloadServiceProperties.getSftpRemoteDirectoryDownload());
    fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter(
            DataloadServiceProperties.getSftpRemoteDirectoryDownloadFilter()));
    return fileSynchronizer;
}

在 SFTP 服务器中查找文件的入站通道

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE - 2)
@InboundChannelAdapter(
        channel = "fromSftpChannel",
        poller = @Poller(
                cron = "${sftp.poller.cron}"))
public MessageSource<File> sftpMessageSource(
        final SftpInboundFileSynchronizer sftpInboundFileSynchronizer,
        final DataloadServiceProperties DataloadServiceProperties) {
    SftpInboundFileSynchronizingMessageSource source =
            new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer);
    source.setLocalDirectory(
            new File(DataloadServiceProperties.getSftpLocalDirectoryDownload()));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    return source;
}

下载到本地文件夹后正在处理文件

@Bean
@Inject
@ServiceActivator(
        inputChannel = "fromSftpChannel")
public MessageHandler resultFileHandler() {
    return new MessageHandler() {
        @Override
        public void handleMessage(final Message<?> message) throws MessagingException {
            String payload = String.valueOf(message.getPayload());
            if (!StringUtils.isEmpty(payload) && payload.endsWith("gz")) {
                LOGGER.info("toRequest : {}", message.getPayload());
            }
        }
    };
}

谢谢 Artem Bilan,我添加了下面的代码,用于在下载后将文件移动到 uat 文件夹。它现在按预期工作。

private static final SpelExpressionParser PARSER = new SpelExpressionParser();

@Bean(name="fromSftpChannel")
 public MessageChannel fromSftpChannel() {
     return new PublishSubscribeChannel();
 }

 @Bean
@Inject
@ServiceActivator(inputChannel = "fromSftpChannel")
@Order(Ordered.LOWEST_PRECEDENCE)
public MessageHandler moveFile() {
    SftpOutboundGateway sftpOutboundGateway = new  SftpOutboundGateway(sftpSessionFactory(), Command.MV.getCommand(), "'/test/'.concat(" + PARSER.parseExpression("payload.getName()").getExpressionString() + ")");
    sftpOutboundGateway.setRenameExpressionString("'/test/uat/'.concat(" + PARSER.parseExpression("payload.getName()").getExpressionString() + ")");
    sftpOutboundGateway.setRequiresReply(false);
    sftpOutboundGateway.setOutputChannelName("nullChannel");
    sftpOutboundGateway.setOrder(Ordered.LOWEST_PRECEDENCE);
    sftpOutboundGateway.setAsync(true);
    return sftpOutboundGateway;
}    

您需要将 fromSftpChannel 设置为 PublishSubscribeChannel,并让第二个订阅者成为 SftpOutboundGateway。您真正为 Command.MV 配置的那个,仅此而已!不要忘记配置 setRenameExpression() 以指定移动的远程路径!