Spring boot 如何使用sftp和spring boot将文件从ftp服务器复制到本地目录,Java
我有通过SFTP的入站和出站通道适配器的代码。我想通过spring引导调度程序调用这些方法,而不使用轮询。查看如何调用resultFileHandler()方法的示例Spring boot 如何使用sftp和spring boot将文件从ftp服务器复制到本地目录,Java,spring-boot,sftp,Spring Boot,Sftp,我有通过SFTP的入站和出站通道适配器的代码。我想通过spring引导调度程序调用这些方法,而不使用轮询。查看如何调用resultFileHandler()方法的示例 公共类SftpConfig{ @值(${nodephone.directory.sftp.host}”) 私有字符串sftpHost; @值(${nodephone.directory.sftp.port}”) 私人港口; @值(${nodephone.directory.sftp.user}”) 私有字符串; @值(${node
公共类SftpConfig{
@值(${nodephone.directory.sftp.host}”)
私有字符串sftpHost;
@值(${nodephone.directory.sftp.port}”)
私人港口;
@值(${nodephone.directory.sftp.user}”)
私有字符串;
@值(${nodephone.directory.sftp.password}”)
私人弦剑;
@值(${nodephone.directory.sftp.remote.directory.download})
私有字符串sftpRemoteDirectoryDownload;
@值(${nodephone.directory.sftp.remote.directory.upload}”)
私有字符串sftpRemoteDirectoryUpload;
@值(${nodephone.directory.sftp.remote.directory.filter}”)
私有字符串sftpRemoteDirectoryFilter;
@值(${nodephone.directory.sftp.remote.directory.localDirectory}”)
私有字符串目录;
//私有FtpOrderRequestHandler;
@豆子
公共会话工厂sftpSessionFactory(){
DefaultSftpSessionFactory=新的DefaultSftpSessionFactory(true);
工厂设置主机(sftpHost);
工厂设置端口(sftpPort);
工厂设置用户(sftpUser);
出厂设置密码(SFTPA);
工厂。setAllowUnknownKeys(真);
返回新的CachingSessionFactory(工厂);
}
@豆子
公共SftpInboundFileSynchronizer SftpInboundFileSynchronizer(){
SftpInboundFileSynchronizer fileSynchronizer=新的SftpInboundFileSynchronizer(sftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(true);
setRemoteDirectory(sftpRemoteDirectoryDownload);
setFilter(新的SftpSimplePatternFileListFilter(sftpRemoteDirectoryFilter));
返回文件同步器;
}
@豆子
@InboundChannelAdapter(channel=“fromstpchannel”,poller=@poller(cron=“0/5*****”)
public MessageSource sftpMessageSource(){
SftpInboundFileSynchronizingMessageSource=新的SftpInboundFileSynchronizingMessageSource(
sftpInboundFileSynchronizer());
source.setAutoCreateLocalDirectory(true);
setLocalFilter(新的AcceptOnceFileListFilter());
setLocalDirectory(新文件(“/local”);
返回源;
}
@豆子
@ServiceActivator(inputChannel=“fromSftpChannel”)
public MessageHandler resultFileHandler(){
返回新的MessageHandler(){
@凌驾
public void handleMessage(消息消息消息)引发MessaginException{
System.out.println(“*******************”+message.getPayload());
}
};
}
我已经用Configuration annotation进行了测试,它从服务器读取文件,但是我想从Cron运行它,而不是轮询,我如何调用resultFileHandler()方法我从未在任何生产代码中使用Spring集成来实现这一点,尽管我做了如下操作,使用sftp/ftp从远程服务器下载文件 我只使用SftpOutboundGateway(可能有更好的方法)调用“mget”方法并获取有效负载(文件)
您已经指定了cron,对吗?此代码每5分钟轮询一次文件,然后关闭会话。(如果没有,您可以搜索以确保连接已关闭)。如果您不想使用轮询器,您可能必须在一个tasklet中编写此逻辑,并在cron上执行该tasklet。由于在spring boot中非常新,我不知道如何手动调用函数。例如,调用哪个方法来下载文件,而不是将所有内容都放在@congifuration下。此代码在本身,但我只是想从我的classI中控制下载机制。我已经在@Configuration下看到了您的remofileconfiguration类,它不是在您调用该方法之前下载了所有内容吗。还有,谁实现了SFTPGateway?很抱歉,我在这方面非常新手不,它不会下载任何内容,只是确保在f中建立连接actory.当您调用
sftpGateway.get
时,SftpOutboundGateway的mget
方法会在通道sftpChannel
上触发。我不知道我做错了什么,它会在应用程序启动期间下载所有文件
public class SftpConfig {
@Value("${nodephone.directory.sftp.host}")
private String sftpHost;
@Value("${nodephone.directory.sftp.port}")
private int sftpPort;
@Value("${nodephone.directory.sftp.user}")
private String sftpUser;
@Value("${nodephone.directory.sftp.password}")
private String sftpPasword;
@Value("${nodephone.directory.sftp.remote.directory.download}")
private String sftpRemoteDirectoryDownload;
@Value("${nodephone.directory.sftp.remote.directory.upload}")
private String sftpRemoteDirectoryUpload;
@Value("${nodephone.directory.sftp.remote.directory.filter}")
private String sftpRemoteDirectoryFilter;
@Value("${nodephone.directory.sftp.remote.directory.localDirectory}")
private String sftpLocalDirectory;
// private FtpOrderRequestHandler handler;
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(sftpHost);
factory.setPort(sftpPort);
factory.setUser(sftpUser);
factory.setPassword(sftpPasword);
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(true);
fileSynchronizer.setRemoteDirectory(sftpRemoteDirectoryDownload);
fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter(sftpRemoteDirectoryFilter));
return fileSynchronizer;
}
@Bean
@InboundChannelAdapter(channel = "fromSftpChannel", poller = @Poller(cron = "0/5 * * * * *"))
public MessageSource<File> sftpMessageSource() {
SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
sftpInboundFileSynchronizer());
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new AcceptOnceFileListFilter<File>());
source.setLocalDirectory(new File("/local"));
return source;
}
@Bean
@ServiceActivator(inputChannel = "fromSftpChannel")
public MessageHandler resultFileHandler() {
return new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
System.out.println("********************** " + message.getPayload());
}
};
}
@Configuration
@ConfigurationProperties(prefix = "sftp")
@Setter
@Getter
@EnableIntegration
public class RemoteFileConfiguration {
private String clients;
private String hosts;
private int ports;
private String users;
private String passwords;
@Bean(name = "clientSessionFactory")
public SessionFactory<LsEntry> clientSessionFactory() {
DefaultSftpSessionFactory sf = new DefaultSftpSessionFactory();
sf.setHost(hosts);
sf.setPort(ports);
sf.setUser(users);
sf.setPassword(passwords);
sf.setAllowUnknownKeys(true);
return new CachingSessionFactory<>(sf);
}
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler clientMessageHandler() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(
clientSessionFactory(), "mget", "payload");
sftpOutboundGateway.setAutoCreateLocalDirectory(true);
sftpOutboundGateway.setLocalDirectory(new File("/users/localPath/client/INPUT/"));
sftpOutboundGateway.setFileExistsMode(FileExistsMode.REPLACE_IF_MODIFIED);
sftpOutboundGateway.setFilter(new AcceptOnceFileListFilter<>());
return sftpOutboundGateway;
}
}
@MessagingGateway
public interface SFTPGateway {
@Gateway(requestChannel = "sftpChannel")
List<File> get(String dir);
}
@Slf4j
@Getter
@Setter
public class RemoteFileInboundTasklet implements Tasklet {
private RemoteFileTemplate remoteFileTemplate;
private String remoteClientDir;
private String clientName;
private SFTPGateway sftpGateway;
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext)
throws Exception {
List<File> files = sftpGateway.get(remoteClientDir);
if (CollectionUtils.isEmpty(files)) {
log.warn("No file was downloaded for client {}.", clientName);
return RepeatStatus.FINISHED;
}
log.info("Total file: {}", files.size());
return RepeatStatus.FINISHED;
}
}
@Autowired
private SFTPGateway sftpGateway;