我是 Spring 集成的新手。我有这个要求,首先将文件从 /files 文件夹移动到 SFTP 位置的 /process 文件夹,然后将该文件复制到本地。建议我使用网关,并且配置必须在 java 中使用注释。我曾尝试在 stackoverflow 上寻求答案,但找不到相关内容。但是,我能够使用 @InboundChannelAdapter 并通过配置其他 bean 来复制文件。
下面是我到目前为止写的代码
配置公共类 SftpConfiguration {
@Value("${ftp.file.host}")
private String host;
@Value("${ftp.file.port")
private int port;
@Value("${ftp.file.user")
private String user;
@Value("${ftp.file.password")
private String password;
@Value("${directorry.file.remote")
private String remoteDir;
@Value("${directorry.file.in.pollerDelay")
final private String pollerDelay = "1000";
@Value("${directory.file.remote.move}")
private String toMoveDirectory;
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(host);
factory.setPort(port);
factory.setUser(user);
factory.setPassword(password);
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(false);
fileSynchronizer.setRemoteDirectory(remoteDir);
fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xlsx"));
return fileSynchronizer;
}
@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = pollerDelay))
public MessageSource<File> sftpMessageSource() {
SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
sftpInboundFileSynchronizer());
source.setLocalDirectory(new File("ftp-inbound"));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new AcceptOnceFileListFilter<File>());
return source;
}
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
return new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
try {
new FtpOrderRequestHandler().handle((File) message.getPayload());
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handlerOut() {
return new SftpOutboundGateway(sftpSessionFactory(), "mv", toMoveDirectory);
}
}
我将不胜感激任何提示或建议。谢谢。