Spring Integration FTP使用后删除本地文件(Spring Boot)

时间:2018-10-12 13:05:37

标签: java spring spring-boot spring-integration

我正在尝试编写一个程序,该程序可以通过ftp从一台服务器上获取文件,然后通过ftp将其放在另一台服务器上。但是,在写入本地文件后,我遇到了删除本地文件的问题。只要是临时的,就可以在本地保存它不是问题。我已经尝试过将ExpressionEvaluatingRequestHandlerAdvice与OnSuccessExpression结合使用,但无法实际使用该表达式。代码在这里:

@Configuration
@EnableConfigurationProperties(FTPConnectionProperties.class)
public class FTPConfiguration {

    private FTPConnectionProperties ftpConnectionProperties;

    public FTPConfiguration(FTPConnectionProperties ftpConnectionProperties) {
        this.ftpConnectionProperties = ftpConnectionProperties;
    }

    @Bean
    public SessionFactory<FTPFile> ftpInputSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost(ftpConnectionProperties.getInputServer());
        sf.setUsername(ftpConnectionProperties.getInputFtpUser());
        sf.setPassword(ftpConnectionProperties.getInputFtpPassword());
        return new CachingSessionFactory<>(sf);
    }

    @Bean
    public SessionFactory<FTPFile> ftpOutputSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost(ftpConnectionProperties.getOutputServer());
        sf.setUsername(ftpConnectionProperties.getOutputFtpUser());
        sf.setPassword(ftpConnectionProperties.getOutputFtpPassword());
        return new CachingSessionFactory<>(sf);
    }

    @Bean
    public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
        FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpInputSessionFactory());
        fileSynchronizer.setDeleteRemoteFiles(true);
        fileSynchronizer.setRemoteDirectory(ftpConnectionProperties.getInputDirectory());
        fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.TIF"));
        return fileSynchronizer;
    }

    @Bean
    @InboundChannelAdapter(channel = "input", poller = @Poller(fixedDelay = "5000"))
    public MessageSource<File> ftpMessageSource() {
        FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer());
        source.setLocalDirectory(new File("ftp-inbound"));
        source.setAutoCreateLocalDirectory(true);
        source.setLocalFilter(new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), ""));
        return source;
    }

    @Bean
    @ServiceActivator(inputChannel = "input")
    public MessageHandler handler() {
        FtpMessageHandler handler = new FtpMessageHandler(ftpOutputSessionFactory());
        handler.setRemoteDirectoryExpression(new LiteralExpression(ftpConnectionProperties.getOutputDirectory()));
        handler.setFileNameGenerator(message -> {
            if (message.getPayload() instanceof File) {
                return ((File) message.getPayload()).getName();
            } else {
                throw new IllegalArgumentException("File expected as payload.");
            }
        });
        return handler;
    }

}

它正在完全按预期方式处理远程文件,从源中删除远程文件并放入输出中,但使用后不删除本地文件。

2 个答案:

答案 0 :(得分:0)

我建议您将input频道设置为PublishSubscribeChannel,并添加一个简单的订户:

@Bean
public PublishSubscribeChannel input() {
    return new PublishSubscribeChannel();
}


@Bean
@ServiceActivator(inputChannel = "input")
public MessageHandler handler() {
    ...
}


@Bean
@ServiceActivator(inputChannel = "input")
public MessageHandler deleteLocalFileService() {
    return m ->  ((File) message.getPayload()).delete();
}

这样,带有File有效负载的相同消息将首先发送到您的FtpMessageHandler,然后才发送到这个新的deleteLocalFileService,以便基于有效负载删除本地文件

答案 1 :(得分:0)

一种简单的解决方案,可以从SFTP服务器获取文件,然后将该文件移至其他名称不同的文件夹。

   @Bean
        public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
            DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);

            if (sftpServerProperties.getSftpPrivateKey() != null) {

                factory.setPrivateKey(sftpServerProperties.getSftpPrivateKey());
                factory.setPrivateKeyPassphrase(sftpServerProperties.getSftpPrivateKeyPassphrase());
            } else {
                factory.setPassword(sftpServerProperties.getPassword());

            }
            factory.setHost(sftpServerProperties.getSftpHost());
            factory.setPort(sftpServerProperties.getSftpPort());
            factory.setUser(sftpServerProperties.getSftpUser());

            factory.setAllowUnknownKeys(true);

            return new CachingSessionFactory<>(factory);
        }

        @Bean
        public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
            SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
            fileSynchronizer.setDeleteRemoteFiles(false);
            fileSynchronizer.setRemoteDirectory(sftpServerProperties.getSftpRemoteDirectoryDownload());
            fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter(sftpServerProperties.getSftpRemoteDirectoryDownloadFilter()));
            return fileSynchronizer;
        }

        @Bean
        @InboundChannelAdapter(channel = "fromSftpChannel", poller = @Poller(cron = "*/10 * * * * *"))
        public MessageSource<File> sftpMessageSource() {
            SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
                    sftpInboundFileSynchronizer());
            source.setLocalDirectory(util.createDirectory(Constants.FILES_DIRECTORY));
            source.setAutoCreateLocalDirectory(true);
            return source;
        }

        @Bean
        @ServiceActivator(inputChannel = "fromSftpChannel")
        public MessageHandler resultFileHandler() {
            return (Message<?> message) -> {
                String csvFilePath = util.getDirectory(Constants.FILES_DIRECTORY) + Constants.INSIDE + message.getHeaders().get("file_name");
                util.readCSVFile(csvFilePath, String.valueOf(message.getHeaders().get("file_name")));
                File file = (File) message.getPayload();
                File newFile = new File(file.getPath() + System.currentTimeMillis());

                try {
                    FileUtils.copyFile(file, newFile);
                    sftpGateway.sendToSftp(newFile);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (file.exists()) {
                    file.delete();
                }
                if (newFile.exists()) {
                    newFile.delete();
                }

            };
        }

        @Bean
        @ServiceActivator(inputChannel = "toSftpChannelDest")
        public MessageHandler handlerOrderBackUp() {
            SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
            handler.setAutoCreateDirectory(true);
            handler.setRemoteDirectoryExpression(new LiteralExpression(sftpServerProperties.getSftpRemoteBackupDirectory()));
            return handler;
        }


        @MessagingGateway
        public interface SFTPGateway {
            @Gateway(requestChannel = "toSftpChannelDest")
            void sendToSftp(File file);


        }