涓嬭浇linux鎸囧畾鐩綍涓嬬殑鏂囦欢

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了涓嬭浇linux鎸囧畾鐩綍涓嬬殑鏂囦欢相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/byte' title='byte'>byte   exce   util   with   for   param   download   path   瀹夊叏   

闇€瑕佺敤鍒扮殑JAR鍖?/span>

<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>銆€

宸ュ叿绫?/span>

public class ScpClient {
    private static ScpClient instance;

    private String ip="114.116.23.456";

    private int port=22;

    private String name="root";

    private String password="123456";

    /**
     * 绉佹湁鍖栭粯璁ゆ瀯閫犲嚱鏁?瀹炰緥鍖栧璞″彧鑳介€氳繃getInstance
     */
    private ScpClient() {

    }

    /**
     * 绉佹湁鍖栨湁鍙傛瀯閫犲嚱鏁?     * 
     * @param ip       鏈嶅姟鍣╥p
     * @param port     鏈嶅姟鍣ㄧ鍙?22
     * @param name     鐧诲綍鍚?     * @param password 鐧诲綍瀵嗙爜
     */
    private ScpClient(String ip, int port, String name, String password) {
        this.ip = ip;
        this.port = port;
        this.name = name;
        this.password = password;
    }

    /**
     * download
     * 
     * @param remoteFile            鏈嶅姟鍣ㄤ笂鐨勬枃浠跺悕
     * @param remoteTargetDirectory 鏈嶅姟鍣ㄤ笂鏂囦欢鐨勬墍鍦ㄨ矾寰?     * @param newPath               涓嬭浇鏂囦欢鐨勮矾寰?     */
    public void downloadFile(String remoteFile, String remoteTargetDirectory, String newPath) {
        Connection connection = new Connection(ip, port);

        try {
            connection.connect();
            boolean isAuthenticated = connection.authenticateWithPassword(name, password);
            if (isAuthenticated) {
                SCPClient scpClient = connection.createSCPClient();
                SCPInputStream sis = scpClient.get(remoteTargetDirectory + "/" + remoteFile);
                File f = new File(newPath);
                if (!f.exists()) {
                    f.mkdirs();
                }
                File newFile = new File(newPath + remoteFile);
                FileOutputStream fos = new FileOutputStream(newFile);
                byte[] b = new byte[4096];
                int i;
                while ((i = sis.read(b)) != -1) {
                    fos.write(b, 0, i);
                }
                fos.flush();
                fos.close();
                sis.close();
                connection.close();
                System.out.println("download ok");
            } else {
                System.out.println("杩炴帴寤虹珛澶辫触");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * download
     * 
     * @param remoteFile            鏈嶅姟鍣ㄤ笂鐨勬枃浠跺悕
     * @param remoteTargetDirectory 鏈嶅姟鍣ㄤ笂鏂囦欢鐨勬墍鍦ㄨ矾寰?/span>*/
    public void download(String remoteFile, String remoteTargetDirectory,HttpServletResponse response) {
        Connection connection = new Connection(ip, port);
        try {
             //1.璁剧疆鏂囦欢ContentType绫诲瀷锛岃繖鏍疯缃紝浼氳嚜鍔ㄥ垽鏂笅杞芥枃浠剁被鍨?  
            response.setContentType("multipart/form-data");   
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(remoteFile, "UTF-8"));//
            connection.connect();
            boolean isAuthenticated = connection.authenticateWithPassword(name, password);
            if (isAuthenticated) {
                SCPClient scpClient = connection.createSCPClient();
                SCPInputStream sis = scpClient.get(remoteTargetDirectory + "/" + remoteFile);
                OutputStream outs=response.getOutputStream();
                byte[] b = new byte[4096];
                int i;
                while ((i = sis.read(b)) != -1) {
                    outs.write(b, 0, i);
                }
                outs.flush();
                outs.close();
                sis.close();
                connection.close();
                System.out.println("download ok");
            } else {
                System.out.println("杩炴帴寤虹珛澶辫触");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(instance!=null) {
                instance=null;
            }
        }
    }

    /**
     * 鑾峰彇鏈嶅姟鍣ㄤ笂鐩稿簲鏂囦欢鐨勬祦
     * 
     * @param remoteFile            鏂囦欢鍚?     * @param remoteTargetDirectory 鏂囦欢璺緞
     * @return
     * @throws IOException
     */
    public SCPInputStream getStream(String remoteFile, String remoteTargetDirectory) throws IOException {
        Connection connection = new Connection(ip, port);
        connection.connect();
        boolean isAuthenticated = connection.authenticateWithPassword(name, password);
        if (!isAuthenticated) {
            System.out.println("杩炴帴寤虹珛澶辫触");
            return null;
        }
        SCPClient scpClient = connection.createSCPClient();
        return scpClient.get(remoteTargetDirectory + "/" + remoteFile);
    }

    /**
     * 涓婁紶鏂囦欢鍒版湇鍔″櫒
     * 
     * @param f                     鏂囦欢瀵硅薄
     * @param length                鏂囦欢澶у皬
     * @param remoteTargetDirectory 涓婁紶璺緞
     * @param mode                  榛樿涓簄ull
     */
    public void uploadFile(File f, String remoteTargetDirectory, String mode) {
        Connection connection = new Connection(ip, port);

        try {
            connection.connect();
            boolean isAuthenticated = connection.authenticateWithPassword(name, password);
            if (!isAuthenticated) {
                System.out.println("杩炴帴寤虹珛澶辫触");
                return;
            }
            SCPClient scpClient = new SCPClient(connection);
            SCPOutputStream os = scpClient.put(f.getName(), f.length(), remoteTargetDirectory, mode);
            byte[] b = new byte[4096];
            FileInputStream fis = new FileInputStream(f);
            int i;
            while ((i = fis.read(b)) != -1) {
                os.write(b, 0, i);
            }
            os.flush();
            fis.close();
            os.close();
            connection.close();
            System.out.println("upload ok");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 鍗曚緥妯″紡 鎳掓眽寮?绾跨▼瀹夊叏
     * 
     * @return
     */
    public static ScpClient getInstance() {
        if (null == instance) {
            synchronized (ScpClient.class) {
                if (null == instance) {
                    instance = new ScpClient();
                }
            }
        }
        return instance;
    }

    public static ScpClient getInstance(String ip, int port, String name, String password) {
        if (null == instance) {
            synchronized (ScpClient.class) {
                if (null == instance) {
                    instance = new ScpClient(ip, port, name, password);
                }
            }
        }
        return instance;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

    public static void main(String[] args) {

        File f1 = new File("C:/Users/Administrator/Desktop/1577926953aaaa.png");
        System.out.println(IPUtil.getServerIpAndPort());
        ScpClient.getInstance("114.116.124.124", 22, "root", "123456").uploadFile(f1, "/usr/local", null);      ScpClient.getInstance().downloadFile("aaa.pang","/usr/local","E:/");
    }
}

以上是关于涓嬭浇linux鎸囧畾鐩綍涓嬬殑鏂囦欢的主要内容,如果未能解决你的问题,请参考以下文章

Linux WordPress 璁块棶鍩熷悕涓嬭浇鏂囦欢

PHP鍩轰簬MVC妯″紡涓嬬殑鍋滆溅鍦鸿溅浣嶇鐞嗙郴缁熴€?48婧愮爜涓嬭浇

nginx鎸囧畾鏂囦欢璺緞鏈変袱绉嶆柟寮弐oot鍜宎lias

JavaScript 瀹炵幇鏂囦欢涓嬭浇骞堕噸鍛藉悕

PM璇讳骇鍝佸惎绀哄綍涓€涔?瀵规瘮寰蒋鏁忔嵎寮€鍙戞祦绋?鍘熶功PDF涓嬭浇)