java自动化上传文件_java用 pscp 命令方式上传文件,实现自动化部署的一环。

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("执行完毕。");     } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值