springboot 集成 spring sftp

本文介绍了如何在Spring Boot 2.5.2中利用spring-integration-sftp组件,配置SFTP会话工厂和消息处理,以实现在Windows迁移到Linux服务器后的定时文件同步,重点在于SFTP连接配置和消息发送流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }

}


  1. https://docs.spring.io/spring-integration/docs/5.2.0.M4/reference/html/sftp.html#sftp ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值