• linux scp 远程获取文件


    需要的jar包:ganymed-ssh2-build210.jar


    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    import ch.ethz.ssh2.Connection;
    import ch.ethz.ssh2.SCPClient;

    public class Scpclient {

        // public Scpclient(){}
        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;
        }

       
        public void getFile(String remoteFile, String localTargetDirectory) {
            Connection conn = new Connection(ip,port);
            try {
                conn.connect();
                boolean isAuthenticated = conn.authenticateWithPassword(username,
                        password);
                if (isAuthenticated == false) {
                    System.err.println("authentication failed");
                }
                SCPClient client = new SCPClient(conn);
                client.get(remoteFile, localTargetDirectory);
                conn.close();
            } catch (IOException ex) {
                Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
            }
        }

       
        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) {
                    System.err.println("authentication failed");
                }
                SCPClient client = new SCPClient(conn);
                client.put(localFile, remoteTargetDirectory);
                conn.close();
            } catch (IOException ex) {
                Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
            }
        }
       
       
        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) {
                    System.err.println("authentication failed");
                }
                SCPClient client = new SCPClient(conn);
                if((mode == null) || (mode.length() == 0)){
                    mode = "0600";
                }
                client.put(localFile, remoteFileName, remoteTargetDirectory, mode);
               
                //重命名
                ch.ethz.ssh2.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 ex) {
                Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
            }
        }
       
    //    public void putFile(String localFile, String remoteFileName,String remoteTargetDirectory) {
    //        Connection conn = new Connection(ip,port);
    //        try {
    //            conn.connect();
    //            boolean isAuthenticated = conn.authenticateWithPassword(username,
    //                    password);
    //            if (isAuthenticated == false) {
    //                System.err.println("authentication failed");
    //            }
    //            SCPClient client = new SCPClient(conn);
    //            client.put(getBytes(localFile), remoteFileName, remoteTargetDirectory);
    //            conn.close();
    //        } catch (IOException ex) {
    //            Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null,ex);
    //        }
    //    }
       
        public static byte[] getBytes(String filePath) {
            byte[] buffer = null;
            try {
                File file = new File(filePath);
                FileInputStream fis = new FileInputStream(file);
                ByteArrayOutputStream byteArray = new ByteArrayOutputStream(1024*1024);
                byte[] b = new byte[1024*1024];
                int i;
                while ((i = fis.read(b)) != -1) {
                    byteArray.write(b, 0, i);
                }
                fis.close();
                byteArray.close();
                buffer = byteArray.toByteArray();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return buffer;
        }

        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 scp = Scpclient.getInstance(ip, port,uname,pwd);
    scp.putFile("本地文件路径", fileFeed.getName()+".tmp", "推送文件,到服务器的目录路径", null);
    scp.getFile(remoteFile, localTargetDirectory);

  • 相关阅读:
    最小割树
    POJ2774 很长的信息
    决战 状压dp
    confd + Nacos | 无代码侵入的配置变更管理
    阿里云应用高可用服务 AHAS 流控降级实现 SQL 自动防护功能
    Web应用托管服务(Web+)隐藏的十个上云最佳姿势
    Java 函数优雅之道
    探索云网络技术前沿,Sigcomm 2019 阿里云参会分享
    MaxCompute 最新特性介绍 | 2019大数据技术公开课第三季
    阿里巴巴大数据产品最新特性介绍 | 2019大数据技术公开课第四季
  • 原文地址:https://www.cnblogs.com/superjt/p/2661025.html
Copyright © 2020-2023  润新知