Himangshu Chowdhary
Himangshu Chowdhary

Reputation: 71

Annotation Configuration for uploading files to server using Spring Integration FTP adapter

I'm unable to upload files to a server using annotation based configuration for Spring Integration FTP Adapter. The code that I have used is:

    @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;

}

I'm trying to read the files from a local folder and push it into a channel but the IntegrationFlow doesn't allow me to attach a channel to it.Please advise how to achieve the same as this snippet is not helping.

Upvotes: 2

Views: 2917

Answers (2)

Himangshu Chowdhary
Himangshu Chowdhary

Reputation: 71

A working example for people's reference to the above mentioned question is:

@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);
    }
}

Upvotes: 0

Gary Russell
Gary Russell

Reputation: 174729

You seem to have completely misunderstood Spring Java configuration. @Bean is for defining beans - you should not be sending messages like you are doing in the for loop - the application context is not ready to accept messages yet, it is only defining beans at this point.

You should also configure the session factory as a @Bean - not declaring it within the integration flow @Bean.

Finally, starting a flow with an outbound adapter makes no sense; you need...

@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();
}

Then, after you create the context, send messages to the output channel.

Upvotes: 1

Related Questions