import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;java
public class pscp_test {linux
/**
* @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("執行完畢。");
}
}
chrome