2016-01-13 46 views
2

文件服務器註解的配置我無法上傳使用基於註解配置Spring集成FTP適配器的文件到服務器。我所使用的代碼是:上傳使用Spring集成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; 

} 

我試圖從本地文件夾下載文件,並將其推入通道,但IntegrationFlow不允許我向it.Please建議附加一個通道如何實現這個片段一樣沒有幫助。

回答

0

人們的參照上面提到的問題的工作的例子是:

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

你似乎有完全誤解的Spring Java配置。 @Bean是定義豆 - 你不應該送你一樣的消息正在做的for循環 - 應用程序上下文不準備接受的消息是,它僅在這一點上定義豆類。

您還應該配置會話工廠爲@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(); 
} 

然後,創建上下文後,將消息發送到輸出通道。

+0

感謝糾正我,因爲我實在是與Spring Java配置混淆。你的建議完美運作。此外,你會介意看看這個問題http://stackoverflow.com/questions/34611165/beans-not-recognized-from-spring-context-xml-while-loading-the-xml-file-as-a-平價 –

相關問題