我需要将图像上传到 FTP 服务器。因此,我创建了与 SessionFactory、MessageHandler 和 MessageGateway 的集成配置,用于将文件上传到 FTP 服务器:
@Configuration
@IntegrationComponentScan
public 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);
}
}
成功构建应用程序。然后我试图上传一些文件:
@Autowired
UploadGateway uploadGateway;
@Override
public void uploadImage(byte[] scanBytes, String filename, String path) {
try {
uploadGateway.upload(scanBytes, filename, path);
} catch (Exception e) {
log.error("WRONG", e);
}
}
然后它说:“没有名为'toFtpChannel'的bean可用” 我几乎尝试了每个教程,我做错了什么?
繁星coding
FFIVE
相关分类