一、前言
通过ganymed-ssh2(262)的ch.ethz.ssh2.SCPClient的定义ScpClient远程客户端类,实现java实现关于linux的scp命令代码示例。
二、代码示例
package com.xwood.fs.util;@b@@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@@b@import ch.ethz.ssh2.Connection;@b@import ch.ethz.ssh2.SCPClient;@b@import ch.ethz.ssh2.SCPInputStream;@b@import ch.ethz.ssh2.SCPOutputStream;@b@@b@public class ScpClient {@b@ @b@ private static ScpClient instance;@b@ @b@ private String ip;@b@ @b@ private int port;@b@ @b@ private String name;@b@ @b@ private String password;@b@ @b@ /**@b@ * 私有化默认构造函数@b@ * 实例化对象只能通过getInstance@b@ */@b@ private ScpClient(){@b@ @b@ }@b@ @b@ /**@b@ * 私有化有参构造函数@b@ * @param ip 服务器ip@b@ * @param port 服务器端口 22@b@ * @param name 登录名@b@ * @param password 登录密码@b@ */@b@ private ScpClient(String ip,int port,String name,String password){@b@ this.ip = ip ;@b@ this.port = port;@b@ this.name = name;@b@ this.password = password;@b@ }@b@ @b@ /**@b@ * download@b@ * @param remoteFile 服务器上的文件名@b@ * @param remoteTargetDirectory 服务器上文件的所在路径@b@ * @param newPath 下载文件的路径@b@ */@b@ public void downloadFile(String remoteFile, String remoteTargetDirectory,String newPath){@b@ Connection connection = new Connection(ip,port);@b@ @b@ try {@b@ connection.connect();@b@ boolean isAuthenticated = connection.authenticateWithPassword(name,password);@b@ if(isAuthenticated){@b@ SCPClient scpClient = connection.createSCPClient();@b@ SCPInputStream sis = scpClient.get(remoteTargetDirectory + "/" + remoteFile);@b@ File f = new File(newPath);@b@ if(!f.exists()){@b@ f.mkdirs();@b@ }@b@ File newFile = new File(newPath + remoteFile);@b@ FileOutputStream fos = new FileOutputStream(newFile);@b@ byte[] b = new byte[4096];@b@ int i;@b@ while ((i = sis.read(b)) != -1){@b@ fos.write(b,0, i);@b@ }@b@ fos.flush();@b@ fos.close();@b@ sis.close();@b@ connection.close();@b@ System.out.println("download ok");@b@ }else{@b@ System.out.println("连接建立失败");@b@ }@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ @b@ /**@b@ * 获取服务器上相应文件的流@b@ * @param remoteFile 文件名@b@ * @param remoteTargetDirectory 文件路径@b@ * @return@b@ * @throws IOException@b@ */@b@ public SCPInputStream getStream(String remoteFile, String remoteTargetDirectory) throws IOException {@b@ Connection connection = new Connection(ip,port);@b@ connection.connect();@b@ boolean isAuthenticated = connection.authenticateWithPassword(name,password);@b@ if(!isAuthenticated){@b@ System.out.println("连接建立失败");@b@ return null;@b@ }@b@ SCPClient scpClient = connection.createSCPClient();@b@ return scpClient.get(remoteTargetDirectory + "/" + remoteFile);@b@ }@b@ @b@ /**@b@ * 上传文件到服务器@b@ * @param f 文件对象@b@ * @param length 文件大小@b@ * @param remoteTargetDirectory 上传路径@b@ * @param mode 默认为null@b@ */@b@ public void uploadFile(File f, String remoteTargetDirectory, String mode) {@b@ Connection connection = new Connection(ip,port);@b@ @b@ try {@b@ connection.connect();@b@ boolean isAuthenticated = connection.authenticateWithPassword(name,password);@b@ if(!isAuthenticated){@b@ System.out.println("连接建立失败");@b@ return ;@b@ }@b@ SCPClient scpClient = new SCPClient(connection);@b@ SCPOutputStream os = scpClient.put(f.getName(),f.length(),remoteTargetDirectory,mode);@b@ byte[] b = new byte[4096];@b@ FileInputStream fis = new FileInputStream(f);@b@ int i;@b@ while ((i = fis.read(b)) != -1) {@b@ os.write(b, 0, i);@b@ }@b@ os.flush();@b@ fis.close();@b@ os.close();@b@ connection.close();@b@ System.out.println("upload ok");@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ @b@ @b@ /**@b@ * 单例模式@b@ * 懒汉式@b@ * 线程安全@b@ * @return@b@ */@b@ public static ScpClient getInstance(){@b@ if(null == instance){@b@ synchronized (ScpClient.class){@b@ if(null == instance){@b@ instance = new ScpClient(); @b@ }@b@ }@b@ }@b@ return instance;@b@ }@b@ @b@ @b@ public static ScpClient getInstance(String ip,int port,String name,String password){@b@ if(null == instance){@b@ synchronized (ScpClient.class){@b@ if(null == instance){@b@ instance = new ScpClient(ip,port,name,password);@b@ }@b@ }@b@ }@b@ return instance;@b@ }@b@ @b@ public String getIp() {@b@ return ip;@b@ }@b@ @b@ public void setIp(String ip) {@b@ this.ip = ip;@b@ }@b@ @b@ public int getPort() {@b@ return port;@b@ }@b@ @b@ public void setPort(int port) {@b@ this.port = port;@b@ }@b@ @b@ public String getName() {@b@ return name;@b@ }@b@ @b@ public void setName(String name) {@b@ this.name = name;@b@ }@b@ @b@ public String getPassword() {@b@ return password;@b@ }@b@ @b@ public void setPassword(String password) {@b@ this.password = password;@b@ }@b@ @b@ @b@ public static void main(String[] args){@b@ @b@ File f1=new File("C:/Users/nijun/Desktop/QQ截图20181013162821副本.jpg");@b@ @b@ ScpClient.getInstance("192.168.1.10",22, "root","123456").uploadFile(f1, "/home/temp/", null); @b@ @b@ }@b@@b@@b@}