SftpChannel远程网络文件传输使用

前言

众所周知NIO的管道技术可用于我们本地IO流作为缓冲区传输byteBuffer的,但是如果我们远程网络传输则可以使用SftpChannel来实现上传下载等操作

maven
版本可选 0.1.55
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <!-- 不能设置optinoal=true-->
        </dependency>
源码示例

配合Springboot配置参数

package cn.pinming.pmsuite.file.archive.bean;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.Serializable;

/**
 * @PROJECT_NAME: 杭州品茗信息技术有限公司
 * @DESCRIPTION:
 * @author: 徐子木
 * @DATE: 2021/2/21 2:51 下午
 */
@Data
@ConfigurationProperties(prefix = "suite.sftp")
public class SftpConfigProperties implements Serializable {


    private String ip = "192.168.10.37";

    private int port =22;

    private String userName = "root";

    private String password = "Pinming9158";

    private int timeOut = 18000000;
}

管理管道流

package cn.pinming.pmsuite.file.archive.sftp;

import cn.hutool.core.util.StrUtil;
import cn.pinming.pmsuite.file.archive.bean.SftpConfigProperties;
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Properties;

/**
 * @PROJECT_NAME: 杭州品茗信息技术有限公司
 * @DESCRIPTION:
 * @author: 徐子木
 * @DATE: 2021/2/21 3:00 下午
 */
@Slf4j
@Service
public class SftpArchiveService {

    private final SftpConfigProperties sftpConfigProperties;

    public SftpArchiveService(SftpConfigProperties sftpConfigProperties) {
        this.sftpConfigProperties = sftpConfigProperties;
    }
    
    /**
     * @param : 
     * @desc : 获取channel
     * @dete : 2021/2/22 3:33 下午
     * @Return: 
     * @author: 徐子木
     */
    public ChannelSftp getChannel() throws JSchException {
        String ip = sftpConfigProperties.getIp();
        int port = sftpConfigProperties.getPort();
        String userName = sftpConfigProperties.getUserName();
        String password = sftpConfigProperties.getPassword();

        JSch jSch = new JSch();

        Session session = jSch.getSession(userName, ip, port);
        if (StrUtil.isNotBlank(password)) {
            session.setPassword(password);
        }

        Properties properties = new Properties();
        properties.put("StrictHostKeyChecking", "no");
        session.setConfig(properties);

        session.setTimeout(sftpConfigProperties.getTimeOut());
        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();

        log.debug("sftp连接成功 ...ip:{},username:{}", ip, userName);

        return (ChannelSftp) channel;
    }
    
    /**
     * @param : 
     * @desc : 关闭流操作
     * @dete : 2021/2/22 3:33 下午
     * @Return: 
     * @author: 徐子木
     */
    public void closeChannel(ChannelSftp channel) {
        try {
            if (null != channel) {
                channel.disconnect();
            }
            // session其实也可以手动关闭,但是理论上他已经有超时连接了,会自动断开
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.debug("sftp 断开成功");
    }

}

操作上传备份

    @Override
    public void sftpBackUpFile(String srcFile, String destFile) {
        ChannelSftp channel = null;
        try {
            channel = sftpArchiveService.getChannel();
            // TODO 其实可以把公共父目录封装起来会方便许多 例:config.getSftpLocalPath + srcFile

            channel.put(srcFile, destFile, ChannelSftp.OVERWRITE);
            channel.quit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sftpArchiveService.closeChannel(channel);
        }


    }

其他操作,包括下载什么的参考具体api,就不全贴出来了

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容