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

其他目录文件操作请看原文

你可能感兴趣的:(Java)