使用Spring Integration FTP适配器将文件上载到服务器的注释配置

使用Spring Integration FTP适配器将文件上载到服务器的注释配置,ftp,spring-integration,Ftp,Spring Integration,我无法使用Spring Integration FTP适配器基于注释的配置将文件上载到服务器。我使用的代码是: @SuppressWarnings({ "unchecked", "rawtypes" }) @Bean public IntegrationFlow ftpOut() { DefaultFtpSessionFactory defSession=new DefaultFtpSessionFactory(); defSession.setUsername("c

我无法使用Spring Integration FTP适配器基于注释的配置将文件上载到服务器。我使用的代码是:

    @SuppressWarnings({ "unchecked", "rawtypes" })
@Bean
public IntegrationFlow ftpOut()
{


    DefaultFtpSessionFactory defSession=new DefaultFtpSessionFactory();
    defSession.setUsername("chh7kor");
    defSession.setPassword("Geetansh71!!");
    defSession.setPort(21);
    defSession.setHost("10.47.116.158");
    String remoteDirectory=DefaultFtpSessionFactory.DEFAULT_REMOTE_WORKING_DIRECTORY;


    File localDirectory=new File("C:\\FTP_Default");

    return IntegrationFlows.from(Ftp.outboundAdapter(defSession, FileExistsMode.REPLACE).remoteDirectory(remoteDirectory)).get();



}

 @Bean
 public MessageChannel outputChannel() 
 {
     File f=new File(PATH_FOR_FILES_FROM_SERVER);
        File[] allSubFiles=f.listFiles();

        DirectChannel dC=new DirectChannel(); 

     for(File iterateFiles:allSubFiles)                    
        {
        final Message<File> messageFile = MessageBuilder.withPayload(iterateFiles).build();
          dC.send(messageFile);



        }
     return dC;

}
@SuppressWarnings({“unchecked”,“rawtypes”})
@豆子
公共集成流ftpOut()
{
DefaultFtpSessionFactory defSession=新的DefaultFtpSessionFactory();
defSession.setUsername(“chh7kor”);
setPassword(“Geetansh71!!”;
解除设定端口(21);
defSession.setHost(“10.47.116.158”);
String remoteDirectory=DefaultFtpSessionFactory.DEFAULT\u REMOTE\u WORKING\u目录;
File localDirectory=新文件(“C:\\FTP\U默认值”);
返回IntegrationFlows.from(Ftp.outboundAdapter(defSession,FileExistsMode.REPLACE).remoteDirectory(remoteDirectory)).get();
}
@豆子
公共消息通道输出通道()
{
文件f=新文件(来自\u服务器的\u文件的路径\u);
File[]allSubFiles=f.listFiles();
DirectChannel dC=新的DirectChannel();
对于(文件迭代文件:所有子文件)
{
最终消息messageFile=MessageBuilder.withPayload(iterateFiles.build();
dC.send(messageFile);
}
返回dC;
}

我正试图从本地文件夹中读取文件并将其推送到通道中,但IntegrationFlow不允许我将通道附加到其中。请建议如何实现此目的,因为此代码段没有帮助。

您似乎完全误解了Spring Java配置
@Bean
用于定义Bean-您不应该像在for循环中那样发送消息-应用程序上下文还没有准备好接受消息,此时它只定义Bean

您还应该将会话工厂配置为
@Bean
——而不是在集成流
@Bean
中声明它

最后,使用出站适配器启动流毫无意义;你需要

@Bean
public IntegrationFlow ftpOut() {
    String remoteDirectory=DefaultFtpSessionFactory.DEFAULT_REMOTE_WORKING_DIRECTORY;
    File localDirectory=new File("C:\\FTP_Default");

    return IntegrationFlows.from(outputChannel())
         .handle(Ftp.outboundAdapter(defSession, FileExistsMode.REPLACE).remoteDirectory(remoteDirectory)))
         .get();
}

然后,在创建上下文后,将消息发送到输出通道。

人们参考上述问题的一个工作示例是:

@Bean
public DefaultFtpSessionFactory sessionFactory()
{
    DefaultFtpSessionFactory defSession=new DefaultFtpSessionFactory();
    defSession.setUsername("chh7kor");
    defSession.setPassword("Geetansh71!!");
    defSession.setPort(21);
    defSession.setHost("10.47.116.158");

    return defSession;
}


@Bean
public IntegrationFlow ftpOut()
{


    String remoteDirectory=sessionFactory().DEFAULT_REMOTE_WORKING_DIRECTORY;


        return IntegrationFlows.from(messageChannel())
             .handle(Ftp.outboundAdapter(sessionFactory(), FileExistsMode.REPLACE).remoteDirectory(remoteDirectory+"/F").autoCreateDirectory(true))
             .get();


}

public static void main(String args[])
{
File f=new File(PATH_FOR_FILES_FROM_SERVER);
    File[] allSubFiles=f.listFiles();
    for (File file : allSubFiles) {
        if(file.isDirectory())
        {
            System.out.println(file.getAbsolutePath()+" is directory");
            //Steps for directory
        }
        else
        {
        System.out.println(file.getAbsolutePath()+" is file");
            //steps for files
        }
    }

    PollableChannel pC=ctx.getBean("pollableChannel", PollableChannel.class);

    for(File iterateFiles:allSubFiles)                    
    {
    final Message<File> messageFile = MessageBuilder.withPayload(iterateFiles).build();
        pC.send(messageFile);

        Thread.sleep(2000);
    }
}
@Bean
公共默认ftpSessionFactory会话工厂()
{
DefaultFtpSessionFactory defSession=新的DefaultFtpSessionFactory();
defSession.setUsername(“chh7kor”);
setPassword(“Geetansh71!!”;
解除设定端口(21);
defSession.setHost(“10.47.116.158”);
退场;
}
@豆子
公共集成流ftpOut()
{
字符串remoteDirectory=sessionFactory()。默认的远程工作目录;
返回IntegrationFlows.from(messageChannel())
.handle(Ftp.outboundAdapter(sessionFactory(),FileExistsMode.REPLACE).remoteDirectory(remoteDirectory+“/F”).autoCreateDirectory(true))
.get();
}
公共静态void main(字符串参数[])
{
文件f=新文件(来自\u服务器的\u文件的路径\u);
File[]allSubFiles=f.listFiles();
用于(文件:所有子文件){
if(file.isDirectory())
{
System.out.println(file.getAbsolutePath()+“是目录”);
//创建目录的步骤
}
其他的
{
System.out.println(file.getAbsolutePath()+“is file”);
//文件的步骤
}
}
PollableChannel pC=ctx.getBean(“PollableChannel”,PollableChannel.class);
对于(文件迭代文件:所有子文件)
{
最终消息messageFile=MessageBuilder.withPayload(iterateFiles.build();
pC.send(messageFile);
《睡眠》(2000年);
}
}

感谢您纠正我的错误,因为我确实对Spring Java配置感到困惑。你的建议非常有效。你介意看看这个问题吗