springboot 集成 spring sftp
公司需求由windows向linux服务器实现定时同步文件,使用sftp传输。之前写过ftp的脚本,查阅资料后卡在登录那段不知如何实现,微软文档openssh有用密匙登录的方法但需要和甲方沟通,遂放弃,考虑 spring sftp 实现,查阅spring文档 1 后,实现也算简单。
spring-boot 2.5.2 引用 jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-sftp</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
配置会话工厂
package com.smile.sftpdemo.config;
import com.jcraft.jsch.ChannelSftp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.annotation.*;
import org.springframework.integration.file.FileNameGenerator;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.sftp.outbound.SftpMessageHandler;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import java.io.File;
@Configuration
public class SftpConfig {
@Autowired
private CustomizeParamConfig config;
@Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(config.host);
factory.setPort(config.port);
factory.setUser(config.username);
factory.setPassword(config.password);
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<ChannelSftp.LsEntry>(factory);
}
@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
public MessageHandler handler() {
SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
handler.setRemoteDirectoryExpression(new LiteralExpression(config.uploadfile));
handler.setFileNameGenerator(new FileNameGenerator() {
@Override
public String generateFileName(Message<?> message) {
return ((File) message.getPayload()).getName();
}
});
return handler;
}
}
消息发送
package com.smile.sftpdemo.service;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
import java.io.File;
@MessagingGateway
public interface SftpService {
@Gateway(requestChannel = "toSftpChannel")
void sendToSftp(File file);
}
注入 扫描
package com.smile.sftpdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableIntegration
@IntegrationComponentScan
@EnableScheduling
public class SftpdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SftpdemoApplication.class, args);
}
}
https://docs.spring.io/spring-integration/docs/5.2.0.M4/reference/html/sftp.html#sftp ↩︎