一、导入依赖
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-sftp</artifactId>
</dependency>
二、Sftp配置类
@Configuration
@EnableIntegration
public class SpringSftpConfig {
@Autowired
private SftpProperties sftpProperties;
@Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(sftpProperties.getHost());
factory.setPort(sftpProperties.getPort());
factory.setUser(sftpProperties.getUsername());
if (StringUtils.isNotEmpty(sftpProperties.getPrivateKey())) {
factory.setPrivateKey(new ClassPathResource(sftpProperties.getPrivateKey()));
factory.setPrivateKeyPassphrase(sftpProperties.getPassphrase());
} else {
factory.setPassword(sftpProperties.getPassword());
}
factory.setAllowUnknownKeys(true);
CachingSessionFactory<ChannelSftp.LsEntry> cachingSessionFactory = new CachingSessionFactory<>(factory);
cachingSessionFactory.setPoolSize(10);
cachingSessionFactory.setSessionWaitTimeout(10000);
return cachingSessionFactory;
}
@Bean
public SftpRemoteFileTemplate sftpRemoteFileTemplate() {
return new SftpRemoteFileTemplate(sftpSessionFactory());
}
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(true);
fileSynchronizer.setRemoteDirectory(sftpProperties.getRoot());
fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.txt"));
return fileSynchronizer;
}
@Bean
@ServiceActivator(inputChannel = "lsChannel")
public MessageHandler lsHandler() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "ls", "payload");
sftpOutboundGateway.setOptions("-dirs"); //配置项
return sftpOutboundGateway;
}
@Bean
@ServiceActivator(inputChannel = "downloadChannel")
public MessageHandler downloadHandler() {
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "mget", "payload");
sftpOutboundGateway.setOptions("-R");
sftpOutboundGateway.setFileExistsMode(FileExistsMode.REPLACE_IF_MODIFIED);
sftpOutboundGateway.setLocalDirectory(new File(sftpProperties.getLocalPath()));
sftpOutboundGateway.setAutoCreateLocalDirectory(true);
return sftpOutboundGateway;
}
@Bean
@ServiceActivator(inputChannel = "uploadChannel")
public MessageHandler uploadHandler() {
SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
handler.setRemoteDirectoryExpression(new LiteralExpression(sftpProperties.getRoot()));
handler.setFileNameGenerator(message -> {
if (message.getPayload() instanceof File) {
return ((File) message.getPayload()).getName();
} else {
throw new IllegalArgumentException("File expected as payload.");
}
});
return handler;
}
@MessagingGateway
public interface SftpGateway {
@Gateway(requestChannel = "lsChannel")
List<FileInfo> listFile(String dir);
@Gateway(requestChannel = "downloadChannel")
List<File> downloadFiles(String dir);
@Gateway(requestChannel = "uploadChannel")
void uploadFile(File file);
}
}
其中,SftpProperties是第二篇中的配置类。SessionFactory来创建连接工厂,进行连接操作。SftpRemoteFileTemplate是Spring-integration-sftp进行sftp操作的模板类,相当于jsch的ChannelSftp。但是SftpRemoteFileTemplate的上传下载方法不大好用,是通过InputStream和OutputStream进行操作的。因此这里通过Spring integration的MessageHandler来进行上传下载操作。
@Autowired
private SpringSftpConfig.SftpGateway sftpGateway;
sftpGateway.listFile("/Users/kungfupanda").stream().forEach(System.out::println);
sftpGateway.downloadFiles("/Users/kungfupanda/sftpserver/RELREVMOD_ddmmyyyy.txt").stream().forEach(System.out::println);
sftpGateway.uploadFile(new File("RELREVMOD_ddmmyyyy.txt"));
三、启动集成扫描
在启动类上加上@IntegrationComponentScan注解
@IntegrationComponentScan
@SpringBootApplication
@EnableScheduling
public class CDDAlertApplication{
public static void main(String... args){
SpringApplication.run(CDDAlertApplication.class, args);
}
}
结语
有关Spring Integration SFTP测试,可以看第一篇中嵌入式SFTP代码。
参考文献:
https://docs.spring.io/spring-integration/docs/5.2.0.M4/reference/html/sftp.html