在Spring Boot中从FTP发送和接收文件

在Spring Boot中从FTP发送和接收文件,spring,spring-boot,spring-integration,ftps,Spring,Spring Boot,Spring Integration,Ftps,我是Spring框架的新手,事实上,我正在学习和使用Spring Boot。最近,在我正在开发的应用程序中,我让Quartz调度器工作,现在我想让Spring集成在那里工作:FTP连接到服务器,从中写入和读取文件 我想要的其实很简单(正如我在以前的java应用程序中所做的那样)。我有两个Quartz作业计划在每天不同的时间启动:一个从FTP服务器读取文件,另一个将文件写入FTP服务器 我会详细说明我到目前为止所做的工作 @SpringBootApplication @ImportResource

我是Spring框架的新手,事实上,我正在学习和使用Spring Boot。最近,在我正在开发的应用程序中,我让Quartz调度器工作,现在我想让Spring集成在那里工作:FTP连接到服务器,从中写入和读取文件

我想要的其实很简单(正如我在以前的java应用程序中所做的那样)。我有两个Quartz作业计划在每天不同的时间启动:一个从FTP服务器读取文件,另一个将文件写入FTP服务器

我会详细说明我到目前为止所做的工作

@SpringBootApplication
@ImportResource("classpath:ws-config.xml")
@EnableIntegration
@EnableScheduling
public class MyApp extends SpringBootServletInitializer {

    @Autowired
    private Configuration configuration;

    //...

    @Bean
    public DefaultFtpsSessionFactory  myFtpsSessionFactory(){
        DefaultFtpsSessionFactory sess = new DefaultFtpsSessionFactory();
        Ftp ftp = configuration.getFtp();
        sess.setHost(ftp.getServer());
        sess.setPort(ftp.getPort());
        sess.setUsername(ftp.getUsername());
        sess.setPassword(ftp.getPassword());
        return sess;
    }

}
我将以下类命名为FtpGateway,如下所示:

@Component
public class FtpGateway {

    @Autowired
    private DefaultFtpsSessionFactory sess;

    public void sendFile(){
        // todo
    }

    public void readFile(){
        // todo
    }

}
我正在阅读文档来学习如何这样做。Spring Integration的FTP似乎是事件驱动的,所以我不知道当触发器在准确的时间触发时,如何从作业执行sendFile()和readFile()

该文档告诉我如何使用入站通道适配器(从FTP读取文件?)、出站通道适配器(将文件写入FTP?)和出站网关(做什么?):

SpringIntegration通过提供三个客户端端点支持通过FTP/FTPS发送和接收文件:入站通道适配器、出站通道适配器和出站网关。它还为定义这些客户机组件提供了方便的基于命名空间的配置选项

所以,我还不清楚该怎么做

请问,有人能给我一个提示吗

谢谢大家!

编辑

谢谢你,迪纳姆先生。首先,我将尝试一个简单的任务:从FTP读取一个文件,轮询器将每5秒运行一次。这是我补充的:

@Bean
public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
    FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(myFtpsSessionFactory());
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setPreserveTimestamp(true);
    fileSynchronizer.setRemoteDirectory("/Entrada");
    fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.csv"));
    return fileSynchronizer;
}


@Bean
@InboundChannelAdapter(channel = "ftpChannel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> ftpMessageSource() {
    FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(inbound);
    source.setLocalDirectory(new File(configuracion.getDirFicherosDescargados()));
    source.setAutoCreateLocalDirectory(true);
    source.setLocalFilter(new AcceptOnceFileListFilter<File>());
    return source;
}

@Bean
@ServiceActivator(inputChannel = "ftpChannel")
public MessageHandler handler() {
    return new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            Object payload = message.getPayload();
            if(payload instanceof File){
                File f = (File) payload;
                System.out.println(f.getName());
            }else{
                System.out.println(message.getPayload());
            }
        }

    };
}
@Bean
公共FtpInboundFileSynchronizer FtpInboundFileSynchronizer(){
FtpInboundFileSynchronizer fileSynchronizer=新的FtpInboundFileSynchronizer(myFtpsSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(false);
setPreserveTimestamp(true);
fileSynchronizer.setRemoteDirectory(“/Entrada”);
setFilter(新的FtpSimplePatternFileListFilter(“*.csv”);
返回文件同步器;
}
@豆子
@InboundChannelAdapter(channel=“ftpcchannel”,poller=@poller(fixedDelay=“5000”))
public MessageSource ftpMessageSource(){
FtpInboundFileSynchronizingMessageSource=新的FtpInboundFileSynchronizingMessageSource(入站);
source.setLocalDirectory(新文件(configuracion.getDirFicherosDescargados());
source.setAutoCreateLocalDirectory(true);
setLocalFilter(新的AcceptOnceFileListFilter());
返回源;
}
@豆子
@ServiceActivator(inputChannel=“ftpChannel”)
public MessageHandler(){
返回新的MessageHandler(){
@凌驾
public void handleMessage(消息消息消息)引发MessaginException{
对象负载=message.getPayload();
if(文件的有效负载实例){
文件f=(文件)有效载荷;
System.out.println(f.getName());
}否则{
System.out.println(message.getPayload());
}
}
};
}

然后,当应用程序运行时,我在远程文件夹中放置了一个新的csv文件intro“Entrada”,但handler()方法在5秒后不会运行。。。我做错了什么?

请在轮询器方法上添加@Scheduled(fixedDelay=5000)

您应该在tasklet中使用SPRING BATCH。使用Spring提供的现有接口配置bean、crone time和输入源要容易得多

上面的示例是基于注释和xml的,您可以使用其中任何一种


其他好处利用听众和并行步骤。这个框架也可以以读-处理-写的方式使用。

配置和进出通道、连接轮询器和spring集成应该可以完成您现在手动完成的所有操作。我无法让它工作。请提供任何解释如何操作的站点@M.Deinum?打开调试日志以查看轮询事件。我看不到您在
ftpMessageSource()
中使用
ftpInboundFileSynchronizer()
。有一些可疑的
入站
,但我们不知道这是怎么回事…@russellhoff我也被困在同一点上。。对你有用吗?