天天看点

java 链接sftp 下载文件 上传文件

引言

最近用到了sftp,记录一下,安装sftp我就不介绍了,网上有的是

依赖

<dependency>
	<groupId>com.jcraft</groupId>
	<artifactId>jsch</artifactId>
	<version>0.1.54</version>
</dependency>
           

application.properties配置

sftp.server.host=192.168.0.134
sftp.server.port=22
sftp.server.username=admin
sftp.server.password=000000
sftp.server.root-path=D:/data
           

sftp config

import com.jcraft.jsch.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * sftp配置
 *
 * @author zhujx
 * @date 2021/03/02 18:22
 */
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "sftp.server")
public class SftpConfig {

    /**
     * host
     */
    private String host;

    /**
     * 端口
     */
    private Integer port;

    /**
     * 账号
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 根目录
     */
    private String rootPath;

    private ChannelSftp channel;

    private Session session;

    private void connectServer() throws JSchException {
        // 创建JSch对象
        JSch jsch = new JSch();
        // 根据用户名,主机ip,端口获取一个Session对象
        session = jsch.getSession(username, host, port);
        session.setPassword(password);
        Properties configTemp = new Properties();
        configTemp.put("StrictHostKeyChecking", "no");
        // 为Session对象设置properties
        session.setConfig(configTemp);
        // 设置timeout时间
        session.setTimeout(60000);
        session.connect();
        // 通过Session建立链接
        // 打开SFTP通道
        channel = (ChannelSftp) session.openChannel("sftp");
        // 建立SFTP通道的连接
        channel.connect();
    }

    /**
     * 断开SFTP Channel、Session连接
     */
    private void closeChannel() {
        try {
            if (channel != null) {
                channel.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
        } catch (Exception e) {
            log.info("sftp close error", e);
        }
    }

    /**
     * 上传文件
     *
     * @param localPath  本地文件
     * @param remotePath 远程文件
     */
    public void upload(String localPath, String remotePath) {
        log.info("localPath {}", localPath);
        log.info("remotePath {}", remotePath);
        try {
            connectServer();
            channel.put(localPath, remotePath, ChannelSftp.OVERWRITE);
            channel.quit();
        } catch (SftpException e) {
            log.info("sftp upload error", e);
        } catch (JSchException e) {
            log.info("sftp open error", e);
        } finally {
            closeChannel();
        }
    }

    /**
     * 下载文件
     *
     * @param remotePath 远程文件
     * @param localPath  本地文件
     */
    public void download(String remotePath, String localPath) throws Exception {
        log.info("remotePath {}", remotePath);
        log.info("localPath {}", localPath);
        try {
            connectServer();
            channel.get(remotePath, localPath);
            channel.quit();
        } finally {
            closeChannel();
        }
    }
           

如果你喜欢这篇文章,请点个赞,加个关注吧!!!