如何使用Spring Integration从sftp移到本地目录时如何将不同的文件保存到不同的位置

问题描述 投票:0回答:1
@SpringBootApplication
@IntegrationComponentScan
public class SftpJavaApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context =
                    new SpringApplicationBuilder(SftpJavaApplication.class)
                        .web(false)
                        .run(args);
        MyGateway gateway = context.getBean(MyGateway.class);
        gateway.sendToSftp(new File("/foo/bar.txt"));
    }

    @Bean
    public SessionFactory<LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost("localhost");
        factory.setPort(port);
        factory.setUser("foo");
        factory.setPassword("foo");
        factory.setAllowUnknownKeys(true);
        factory.setTestSession(true);
        return new CachingSessionFactory<LsEntry>(factory);
    }

    @Bean
    @ServiceActivator(inputChannel = "toSftpChannel")
    public MessageHandler handler() {
        SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
        handler.setRemoteDirectoryExpressionString("headers['remote-target-dir']");
        handler.setFileNameGenerator(new FileNameGenerator() {

            @Override
            public String generateFileName(Message<?> message) {
                 return "handlerContent.test";
            }

        });
        return handler;
    }

    @MessagingGateway
    public interface MyGateway {

         @Gateway(requestChannel = "toSftpChannel")
         void sendToSftp(File file);

    }
}

使用此(Spring集成)代码,我能够将文件从SFTP移到本地目录。但是我想将所有相似的文件移到一个文件夹,即将所有pdf移到一个文件夹,将所有xml文件移到另一个文件夹。我怎样才能做到这一点 ?

java spring-boot spring-integration
1个回答
0
投票

该代码执行相反操作(发送到SFTP)。

使用出站网关列出(LS)并获取每个文件并将其放在特定目录中。

https://docs.spring.io/spring-integration/docs/5.3.0.RELEASE/reference/html/sftp.html#sftp-outbound-gateway

© www.soinside.com 2019 - 2024. All rights reserved.