我无法使用基于注释的Spring Integration FTP Adapter配置将文件上传到服务器。我使用的代码是:
@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不允许我将频道附加到它。请告知如何实现相同,因为此片段没有帮助。
答案 0 :(得分:1)
您似乎完全误解了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();
}
然后,在创建上下文后,将消息发送到输出通道。
答案 1 :(得分: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);
}
}