我需要將圖像上傳到 FTP 服務器。因此,我創建了與 SessionFactory、MessageHandler 和 MessageGateway 的集成配置,用于將文件上傳到 FTP 服務器:@Configuration@IntegrationComponentScanpublic class FtpConfiguration { @Bean public SessionFactory<FTPFile> ftpSessionFactory() { DefaultFtpSessionFactory defaultFtpSessionFactory = new DefaultFtpSessionFactory(); //setup return new CachingSessionFactory<>(defaultFtpSessionFactory); } @Bean @ServiceActivator(inputChannel = "toFtpChannel") public MessageHandler handler() { FtpMessageHandler handler = new FtpMessageHandler(ftpSessionFactory()); handler.setAutoCreateDirectory(true); handler.setRemoteDirectoryExpression(new LiteralExpression("")); handler.setFileNameGenerator(message -> (String) message.getHeaders().get("filename")); return handler; } //to show you that I've tried both /*@Bean public IntegrationFlow ftpOutboundFlow() { return IntegrationFlows.from("toFtpChannel") .handle(Ftp.outboundAdapter(ftpSessionFactory(), FileExistsMode.REPLACE) .useTemporaryFileName(false) .fileNameExpression("headers['" + FileHeaders.FILENAME + "']") .remoteDirectory("") ).get(); }*/ @MessagingGateway public interface UploadGateway { @Gateway(requestChannel = "toFtpChannel") void upload(@Payload byte[] file, @Header("filename") String filename, @Header("path") String path); }}成功構建應用程序。然后我試圖上傳一些文件:@AutowiredUploadGateway uploadGateway;@Overridepublic void uploadImage(byte[] scanBytes, String filename, String path) { try { uploadGateway.upload(scanBytes, filename, path); } catch (Exception e) { log.error("WRONG", e); }}然后它說:“沒有名為'toFtpChannel'的bean可用” 我幾乎嘗試了每個教程,我做錯了什么?
2 回答

繁星coding
TA貢獻1797條經驗 獲得超4個贊
您的應用程序看起來不像真的是 Spring Boot:我們沒有看到@SpringBootApplication
注釋。正是這個將啟動正確的自動配置,包括用于 Spring Integration 的自動配置。
如果您仍然不關心那里的 Spring Boot,那么您將缺少一個標準@EnableIntegration
注釋:https : //docs.spring.io/spring-integration/docs/current/reference/html/overview.html#configuration-enable-一體化

FFIVE
TA貢獻1797條經驗 獲得超6個贊
在您可以在注釋中引用它之前,請求通道應該實際存在。聲明頻道,該問題應該消失
@Bean
public MessageChannel toFtpChannel() {
return new DirectChannel();
}
此示例創建,DirectChannel但您可以根據您的語義選擇任何實現。
希望能幫助到你!
添加回答
舉報
0/150
提交
取消