123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package com.fdkankan.manage.util;
- import com.fdkankan.manage.common.CacheUtil;
- import lombok.extern.slf4j.Slf4j;
- import java.io.*;
- @Slf4j
- public class ShellUtil {
- /**
- * 执行系统命令, 返回执行结果
- * @param cmd 需要执行的命令
- */
- public static void execCmd(String cmd) {
- StringBuilder result = new StringBuilder();
- Process process = null;
- BufferedReader bufrIn = null;
- BufferedReader bufrError = null;
- long startTime = System.currentTimeMillis();
- try {
- // 执行命令, 返回一个子进程对象(命令在子进程中执行)
- log.info("执行cmd:{}",cmd);
- String[] cmd2 = new String[]{"/bin/sh", "-c", cmd};
- process = Runtime.getRuntime().exec(cmd2);
- // 获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入)
- //处理InputStream的线程
- threadRun(process);
- // 方法阻塞, 等待命令执行完成(成功会返回0)
- process.waitFor();
- }catch (Exception e){
- log.error("cmd:{},error:{}",cmd,e);
- }finally {
- closeStream(bufrIn);
- closeStream(bufrError);
- // 销毁子进程
- if (process != null) {
- process.destroy();
- }
- }
- // 返回执行结果
- log.info("执行cmd:{},结果:{},耗时:{}", cmd,result.toString(),System.currentTimeMillis() -startTime);
- }
- private static void threadRun(Process process) {
- new Thread() {
- @Override
- public void run() {
- BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
- String line = null;
- try {
- while((line = in.readLine()) != null) {
- log.debug("output: " + line);
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- try {
- in.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
- new Thread() {
- @Override
- public void run() {
- BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
- String line = null;
- try {
- while((line = err.readLine()) != null) {
- log.debug("err: " + line);
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- try {
- err.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
- }
- private static void closeStream(Closeable stream) {
- if (stream != null) {
- try {
- stream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public static void unZip(String zipPath, String dataPath) {
- log.info("解压zip开始");
- if(!new File(dataPath).getParentFile().exists()){
- new File(dataPath).getParentFile().mkdirs();
- }
- String command = "unzip -O GBK/GB18030CP936 " + zipPath + " -d " + dataPath;
- execCmd(command);
- log.info("解压zip完毕:" + command);
- }
- public static void zip(String dataPath, String zipPath) {
- log.info("打包zip开始");
- String command ="cd " + dataPath + " && zip -r " + zipPath + " * " ;
- execCmd(command);
- log.info("解压zip完毕:" + command);
- }
- /**
- * oss文件上传命令 bash /opt/ossutil/fyun-upload.sh {bucket} {srcPath} {destPath} {fyunType} {opType}
- * opType: file or folder
- * //@param bucket 桶名
- * @param srcPath 源文件路径
- * @param destPath 目标文件路径
- * //@param fyunType oss or aws
- */
- public static void yunUpload(String srcPath,String destPath){
- String opType = srcPath.contains(".")? "file":"folder" ;
- String cmd = String.format(ShellCmd.FYUN_UPLOAD, "4dkankan",srcPath,destPath, CacheUtil.uploadType,opType);
- execCmd(cmd);
- }
- public static void yunDownload(String srcPath,String destPath){
- String opType = srcPath.contains(".")? "file":"folder" ;
- String cmd = String.format(ShellCmd.FYUN_DOWN,"4dkankan",srcPath,destPath, CacheUtil.uploadType,opType);
- execCmd(cmd);
- }
- }
|