import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
public class pscp_test {
/**
* @param args
*/
public static void main(String[] args) {
//上传
/* String localFilePath = "E:/google_chrome_download/apache-tomcat-9.0.0.M19.tar.gz";//上传的文件路径
String remoteDestAbsPath = "/usr/local";//linux文件目标目标绝对路径
uploadFile(localFilePath,remoteDestAbsPath);*/
//下载
String remoteFilePath = "/root/my_shell.sh";
String fileType = "";
String localPath = "D:/auto_class_file/";
getFile(remoteFilePath,fileType,localPath);
}
/**
* 上传windows本地文件或目录(已支持目录)
*
* @param localFilePath
* @param remoteDestAbsPath */ public static void uploadFile(String localFilePath,String remoteDestAbsPath){ if(null != localFilePath && null != remoteDestAbsPath){ File localFile = new File(localFilePath); String pscpPath = "D:/auto_class_file/pscp.exe";//pscp.exe命令路径 String commandStr = null; if(localFile.exists()){ if(localFile.isFile()){ commandStr = "cmd.exe /k start && "+pscpPath+" -pw admin "+localFilePath+" root@192.168.56.100:"+remoteDestAbsPath; }else{ commandStr = "cmd.exe /k start && "+pscpPath+" -pw admin -r "+localFilePath+" root@192.168.56.100:"+remoteDestAbsPath; } execCommand(commandStr); } } } /** * 下载远程服务器文件 * @param remoteFilePath * @param fileType * @param localPath */ public static void getFile(String remoteFilePath,String fileType,String localPath){ if(null != remoteFilePath && null != localPath){ File localFile = new File(localPath); String pscpPath = "D:/auto_class_file/pscp.exe";//pscp.exe命令路径 String commandStr = null; if(localFile.exists()){ if(localFile.isDirectory()){ if(fileType.equals("d")){ commandStr = "cmd.exe /k start && "+pscpPath+" -pw admin -r root@192.168.56.100:"+remoteFilePath+" "+localPath; }else{ commandStr = "cmd.exe /k start && "+pscpPath+" -pw admin root@192.168.56.100:"+remoteFilePath+" "+localPath; } execCommand(commandStr); }else{ System.out.println("本地路径为非目录。"); } } } } //执行命令 public static void execCommand(String commandStr){ if(null != commandStr && commandStr.length() > 0){ try { Runtime run = Runtime.getRuntime(); Process process = run.exec(commandStr); InputStream input = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String szline; while ((szline = reader.readLine()) != null) { System.out.println(szline); } reader.close(); process.waitFor(); process.destroy(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("执行完毕。"); } }