如何使用SFTP Outbound Gateway'mget'命令下载文件?

时间:2017-10-12 10:40:14

标签: spring-integration

我想使用'mget'命令从sftp服务器下载文件。这是我的java配置:

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



@Bean(name = "lsGateway")
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handlerLs() {
    // call 'mget' command to download all the files in server folder
    SftpOutboundGateway sftpOutboundGateway = new  SftpOutboundGateway(sftpSessionFactory(), "mget", "payload");
    sftpOutboundGateway.setLocalDirectory(new File("/local/path"));
    return sftpOutboundGateway;

}

网关接口:

@MessagingGateway
public interface OutboundGatewayOption {
@Gateway(requestChannel = "sftpChannel")
 List<File> mget(String dir);

}

执行下载:

@Component
public class Step1Tasklet implements Tasklet {

@Autowired
private OutboundGatewayOption gatewayOption;

@Override
public RepeatStatus execute(StepContribution stepContribution,
        ChunkContext chunkContext) throws Exception {

    // download  files in server folder
    List<File> files = gatewayOption.mget("/ftp/server/path/");


    return RepeatStatus.FINISHED;
}

}

我遇到了这个例外:

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sftpChannel' available

我已经google了但是找不到问题,请有人帮忙!

1 个答案:

答案 0 :(得分:0)

  

没有名为'sftpChannel'的bean

表示您还没有sftpChannel MessageChannel bean。

我认为问题来自@MessagingGateway bean定义。

您需要做的就是声明sftpChannel bean:

@Bean
public MessageChannel sftpChannel() {
    return new DirectChannel();
}

是的,在最新的Spring Integration中,此问题已修复,MessageChannel最近根据需要从其名称中解析。