java基于Scp实现Linux下远程获取、上传文件

2019-05-29  本文已影响0人  话尔loony
将本机文件上传到远程服务器
#scp /usr/local/news.txt root@192.168.1.1:/etc/opt

** /usr/local/** 本地文件的绝对路径
news.txt 要复制到服务器上的本地文件
root 通过root用户登录到远程服务器(也可以使用其他拥有同等权限的用户)
192.168.6.129 远程服务器的ip地址(也可以使用域名或机器名)
/etc/opt 将本地文件复制到位于远程服务器上的路径

将远程服务器文件下载到本机
#scp root@192.168.1.1:/usr/local/news.txt  /usr/local

root 通过remote用户登录到远程服务器(也可以使用其他拥有同等权限的用户)
192.168.1.1 远程服务器的IP地址(当然也可以使用该服务器域名)
/usr/local/news.txt 欲复制到本机的位于远程服务器上的文件
/usr/local 将远程文件复制到本地的绝对路径

<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
        <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>build210</version>
        </dependency>

工具类:

public class Scpclient {
    private static final FileLog logger = new FileLog(Scpclient.class.getName());
    static private Scpclient instance;

    static synchronized public Scpclient getInstance(String IP, int port,
                                                     String username, String passward) {
        if (instance == null) {
            instance = new Scpclient(IP, port, username, passward);
        }
        return instance;
    }

    public Scpclient(String IP, int port, String username, String passward) {
        this.ip = IP;
        this.port = port;
        this.username = username;
        this.password = passward;
    }

    /**
     * 远程拷贝文件
     * @param remoteFile  远程源文件路径
     * @param localTargetDirectory 本地存放文件路径
     */
    public Map<String,Object> getFile(String remoteFile, String localTargetDirectory) {
        Connection conn = new Connection(ip,port);
        try {
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(username,
                    password);
            if (isAuthenticated == false) {
             logger.log(Level.ERROR_INT,"authentication failed");
             return Common.pageMap(NetMsgUtil.INVOKING_SERVER_EXCEPTION,"authentication failed");
            }
            SCPClient client = new SCPClient(conn);
            client.get(remoteFile, localTargetDirectory);
            conn.close();
        } catch (IOException e) {
            logger.log(Level.ERROR_INT,e.getMessage());
            return Common.pageMap(NetMsgUtil.INVOKING_SERVER_EXCEPTION,e.getMessage());
        }
        return Common.pageMap(NetMsgUtil.NET_NORMAL_MSG,"Document Success");
    }

  

   /**
     * 远程上传文件
     * @param localFile 本地文件路径
     * @param remoteTargetDirectory  远程存放文件路径
     */
    public void putFile(String localFile, String remoteTargetDirectory) {
        Connection conn = new Connection(ip,port);
        try {
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(username,
                    password);
            if (isAuthenticated == false) {
                logger.log(Level.ERROR_INT,"authentication failed");
            }
            SCPClient client = new SCPClient(conn);
            client.put(localFile, remoteTargetDirectory);
            conn.close();
        } catch (IOException e) {
            logger.log(Level.ERROR_INT,e.getMessage());
        }
    }

  /**
     * 远程上传文件并对上传文件重命名
     * @param localFile 本地文件路径
      *@param remoteFileName远程文件名
     * @param remoteTargetDirectory  远程存放文件路径
     *@param mode 默认"0600",length=4
     */
    public void putFile(String localFile, String remoteFileName,String remoteTargetDirectory,String mode) {
        Connection conn = new Connection(ip,port);
        try {
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(username,
                    password);
            if (isAuthenticated == false) {
                logger.log(Level.ERROR_INT,"authentication failed");
            }
            SCPClient client = new SCPClient(conn);
            if((mode == null) || (mode.length() == 0)){
                mode = "0600";
            }
            client.put(localFile, remoteFileName, remoteTargetDirectory, mode);

            //重命名
            Session sess = conn.openSession();
            String tmpPathName = remoteTargetDirectory + File.separator+ remoteFileName;
            String newPathName = tmpPathName.substring(0, tmpPathName.lastIndexOf("."));
            sess.execCommand("mv " + remoteFileName + " " + newPathName);

            conn.close();
        } catch (IOException e) {
            logger.log(Level.ERROR_INT,e.getMessage());
        }
    }

    private String ip;
    private int port;
    private String username;
    private String password;

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

}

调用姿势:

Scpclient scpclient = Scpclient.getInstance(String IP, int port,String username, String passward);
// 拷贝
scpclient .getFile(...);
// 上传
scpclient .putFile(...);
上一篇 下一篇

猜你喜欢

热点阅读