0

我正在尝试使用 spring 集成的 FTP 文件上传和下载示例。我想手动将文件上传到 outputChannel。当 inputChannel 发生变化时,我不想调用它。因此,FTPApplication 应该只有一个 outputChannel bean 的声明。为此,我参考了这篇文章使用spring spring将文件直接上传到ftp服务器中的特定文件夹(仅用于手动上传,这种方式在大多数页面中都很常见)。以下是 Spring Boot 应用程序代码:

@SpringBootApplication
public class FtpApplication {

    public static void main(String[] args) {
        SpringApplication.run(FtpApplication.class, args);
    }

    @Bean
    public SessionFactory<FTPFile> ftpSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost("localhost");
        sf.setPort(21);
        sf.setUsername("root");
        sf.setPassword("root");
        return new CachingSessionFactory<FTPFile>(sf);
    }

    @Bean
    public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
        FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory());
        fileSynchronizer.setDeleteRemoteFiles(false);
        fileSynchronizer.setRemoteDirectory("/");
        fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.xml"));
        return fileSynchronizer;
    }

    @Bean
    @InboundChannelAdapter(value="inputChannel", channel = "inputChannel")
    public MessageSource<File> ftpMessageSource() {
        FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(
                ftpInboundFileSynchronizer());
        source.setLocalDirectory(new File("ftp-inbound"));
        source.setAutoCreateLocalDirectory(true);
        source.setLocalFilter(new AcceptOnceFileListFilter<File>());
        return source;
    }

    @Bean
    @ServiceActivator(inputChannel = "inputChannel")
    public MessageHandler handler() {
        return new MessageHandler() {
            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
                System.out.println(message.getPayload());
            }
        };
    }

    @Bean(name = PollerMetadata.DEFAULT_POLLER)
    public PollerMetadata defaultPoller() {
        PollerMetadata pollerMetadata = new PollerMetadata();
        pollerMetadata.setTrigger(new PeriodicTrigger(10));
        return pollerMetadata;
    }

    @Bean
    @InboundChannelAdapter(value="outputChannel", channel = "outputChannel")
    public MessageSource<File> ftpMessageSource1() {
        FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(
                ftpInboundFileSynchronizer());
        source.setLocalDirectory(new File("ftp-outbound"));
        source.setAutoCreateLocalDirectory(true);
        source.setLocalFilter(new AcceptOnceFileListFilter<File>());
        return source;
    }

}

控制器代码如下图:

@RestController
public class FTPController {

    @Autowired
    MessageChannel outputChannel;

    @RequestMapping(value = "/ftpuploadtest", method = RequestMethod.GET)
    public void getM36Messages() {
        File file = new File("ftp.txt");
        Message<File> fileMessage = MessageBuilder.withPayload(file).build();
        outputChannel.send(fileMessage);
    }
}

当我运行应用程序时,我收到以下错误:

Description:
Field outputChannel in com.ftp.FTPController required a single bean, but 3 were found:
    - nullChannel: defined in null
    - errorChannel: defined in null
    - inputChannel: a programmatically registered singleton

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

请帮助解决这个问题。

4

2 回答 2

0

首先,我认为您在outputChannel组件方面犯了一些错误。

如果那是关于上传,那么你必须使用@ServiceActivatorand FtpMessageHandler

另一方面,对于这样的问题,您必须@BeanoutputChannel和 一起声明@Autowired,在 中FTPController,您应该添加@Qualifier("outputChannel").

于 2016-09-27T21:40:32.313 回答
0

使用 @Qualifier 和 @Autowired 。并保持变量名称与 bean 名称相同。

于 2016-10-05T11:46:16.667 回答