首先需要有一个Springboot的项目
在pom.xml中添加以下依赖,使用spring-integration-sftp
核心组件org.springframework.integration spring-integration-sftp 5.5.11
spring integration sftp 是 spring integration 框架中的 sftp 的模块。基于com.jcraft.jsch执行sftp命令
-
SftpSessionFactory:
Sftp客户端与服务器连接的工厂,每次与Sftp服务器端建立连接均会执行工厂建立新的session连接,这些连接可以被缓存下来,以便下次快速连接。同时支持session共享可以在一个连接上执行多种操作
-
SftpSessionCaching:
sftp 会话缓存工厂。通过 poolSize 和 sessionWaiteTimeout 来设置缓存池大小和会话等待超时时间。缓存池默认是无限大,超时时间默认是 Integer.MAX_VALUE。
-
SftpRemoteFileTemplate:
Stp 远程文件模板,将sftp的下载上传删除重命名等进行的封装。本质是RemoteFileTemplete的继承,基于流传输实现
-
SftpInboundChannelAdapter:
sftp 入站通道适配器。可同步远程目录到本地,且可监听远程文件的操作,可实现下载。 -
SftpOutboundChannelAdapter:
sftp 出站通道适配器。实际是一个 sftp 消息处理器,将在服务器与客户端之间创建一个消息传输通道。此处的消息指的是 Message 的 payload,其支持 File、byte[]、String。其支持 ls、nlst、get、rm、mget、mv、put、mput 操作。 -
Channel Adapter:
通道适配器,实际上是适配消息在客户端和服务器之间的传输。inbound adapter 是接收其它系统的消息,outbound adapter 是发送消息到其它系统。 -
@ServiceActivator:
将注解作用的方法注册为处理消息的站点,inputChannel 表示接收消息的通道。
在application.yaml(或者application.propertites)中导入以下数据(或等价形式)
sftp: host: 'localhost' port: 22 user: 'username' password: 'password' remote-root-dir: '/rootdir' local-cache-dir: '~/.cacheDir'
新建SFTPConfig.java配置SFTP的配置
@Configuration
@EnableIntegration
public class SftpConfig {
@Value("${sftp.host}")
private String host;
@Value("${sftp.port}")
private int port;
@Value("${sftp.user}")
private String user;
@Value("${sftp.password}")
private String password;
@Value("${sftp.local-cache-dir}")
private String localCacheDir;
@Value("${sftp.remote-root-dir}")
private String remoteRootDir;
@Bean
public SessionFactory sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(host);
factory.setPort(port);
factory.setUser(user);
factory.setPassword(password);
factory.setAllowUnknownKeys(true);
CachingSessionFactory cachingSessionFactory = new CachingSessionFactory<>(factory);
cachingSessionFactory.setPoolSize(10);
return cachingSessionFactory;
}
@Bean
public SftpRemoteFileTemplate sftpRemoteFileTemplate() {
return new SftpRemoteFileTemplate(sftpSessionFactory());
}
@Bean
@ServiceActivator(inputChannel = "lsChannel")
public MessageHandler lsHandler() {
SftpOutboundGateway gateway = new SftpOutboundGateway(sftpSessionFactory(), "ls", "payload");
gateway.setOptions("-dirs"); // 显示目录记录
return gateway;
}
@Bean
@ServiceActivator(inputChannel = "getChannel")
public MessageHandler getHandler() {
SftpOutboundGateway gateway = new SftpOutboundGateway(sftpSessionFactory(), "get", "payload");
gateway.setOptions("-P");
gateway.setAutoCreateDirectory(true);
gateway.setLocalDirectory(new File(localCacheDir));
return gateway;
}
@Bean
public DirectChannel getChannel() {
return new DirectChannel();
}
@Bean
public DirectChannel lsChannel() {
return new DirectChannel();
}
}
配置Gateway网关接口
@MessagingGateway
public interface SftpGateway {
@Gateway(requestChannel = "lsChannel")
List listFile(String directory);
@Gateway(requestChannel = "getChannel")
File getFile(String directory);
}
测试使用
@SpringBootTest
class JudgeApplicationTests {
@Resource
SftpGateway sftpGateway;
@Test
void contextLoads() {
List list = sftpGateway.listFile("~/admin");
System.out.println(list);
}
}



