天天看點

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

如果你喜歡這篇文章,請點個贊,加個關注吧!!!