Java遠程連接服務器實現文件上傳下載及目錄操作


詳情請閱讀原文
在其基礎之上做了進一步的封裝

 <!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>

import com.jcraft.jsch.*;

import java.util.Properties;

public class SFTPChannel {
	//ip地址
    private String host;
    //登錄賬號
    private String username;
    //登錄密碼
    private String password;
    //默認端口號
    private int port = 22;
    //默認過期時間
    private int timeout = 60000;
    private Session session = null;
    private Channel channel = null;


    public SFTPChannel(String host, String username, String password) {
        this.host = host;
        this.username = username;
        this.password = password;
    }

    public SFTPChannel(String host, String username, String password, int port) {
        this.host = host;
        this.username = username;
        this.password = password;
        this.port = port;
    }

    public SFTPChannel(String host, String username, String password, int port, int timeout) {
        this.host = host;
        this.username = username;
        this.password = password;
        this.port = port;
        this.timeout = timeout;
    }

    //創建連接
    public ChannelSftp getChannel(SFTPChannel sftpChannel) throws JSchException {
        // 創建JSch對象
        JSch jSch = new JSch();
        // 根據用戶名,主機ip,端口獲取一個Session對象
        session = jSch.getSession(sftpChannel.getUsername(), sftpChannel.getHost(), sftpChannel.getPort());
        // 設置密碼
        session.setPassword(sftpChannel.getPassword());
        Properties properties = new Properties();
        //主機公鑰確認 無口令 SSH 登錄(即通過客戶端公鑰認證),就可以直接連接到遠程主機。
        //這是基於 SSH 協議的自動化任務常用的手段
        properties.put("StrictHostKeyChecking", "no");
        // 為Session對象設置properties
        session.setConfig(properties);
        // 設置timeout時間
        session.setTimeout(sftpChannel.getTimeout());
        // 通過Session建立鏈接
        session.connect();
        // 打開SFTP通道
        channel = session.openChannel("sftp");
        // 建立SFTP通道的連接
        channel.connect();
        return (ChannelSftp) channel;
    }

    //關閉連接
    public void closeChannel() {
        if (null != channel) {
            channel.disconnect();
        }
        if (null != session) {
            session.disconnect();
        }
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}

使用示例

public static void main(String[] args) throws Exception {
        SFTPChannel sftpChannel = new SFTPChannel("服務器ip", "root", "root");
        //直接將本地文件名為src的文件上傳到目標服務器,目標文件名為dst。
        //(注:使用這個方法時,dst可以是目錄,當dst是目錄時,上傳后的目標文件名將與src文件名相同)
        String src = "D:\\project\\Rar.txt";
        String dst = "/opt/csv/";
        ChannelSftp channelSftp = sftpChannel.getChannel(sftpChannel);
        System.out.println("創建鏈接");
        channelSftp.put(src, dst, ChannelSftp.OVERWRITE);
        System.out.println("上傳文件成功");
        //展示上傳文件目錄下的所有文件
        Vector vector = channelSftp.ls(dst);
        System.out.println(vector.toString());
        //關閉連接
        channelSftp.quit();
        sftpChannel.closeChannel();
    }

其他目錄文件操作請看原文


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM