ShellUtil.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.fdkankan.manage.util;
  2. import com.fdkankan.manage.common.CacheUtil;
  3. import lombok.extern.slf4j.Slf4j;
  4. import java.io.*;
  5. @Slf4j
  6. public class ShellUtil {
  7. /**
  8. * 执行系统命令, 返回执行结果
  9. * @param cmd 需要执行的命令
  10. */
  11. public static void execCmd(String cmd) {
  12. StringBuilder result = new StringBuilder();
  13. Process process = null;
  14. BufferedReader bufrIn = null;
  15. BufferedReader bufrError = null;
  16. long startTime = System.currentTimeMillis();
  17. try {
  18. // 执行命令, 返回一个子进程对象(命令在子进程中执行)
  19. log.info("执行cmd:{}",cmd);
  20. String[] cmd2 = new String[]{"/bin/sh", "-c", cmd};
  21. process = Runtime.getRuntime().exec(cmd2);
  22. // 获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入)
  23. //处理InputStream的线程
  24. threadRun(process);
  25. // 方法阻塞, 等待命令执行完成(成功会返回0)
  26. process.waitFor();
  27. }catch (Exception e){
  28. log.error("cmd:{},error:{}",cmd,e);
  29. }finally {
  30. closeStream(bufrIn);
  31. closeStream(bufrError);
  32. // 销毁子进程
  33. if (process != null) {
  34. process.destroy();
  35. }
  36. }
  37. // 返回执行结果
  38. log.info("执行cmd:{},结果:{},耗时:{}", cmd,result.toString(),System.currentTimeMillis() -startTime);
  39. }
  40. private static void threadRun(Process process) {
  41. new Thread() {
  42. @Override
  43. public void run() {
  44. BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
  45. String line = null;
  46. try {
  47. while((line = in.readLine()) != null) {
  48. log.debug("output: " + line);
  49. }
  50. }
  51. catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. finally {
  55. try {
  56. in.close();
  57. }
  58. catch (IOException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63. }.start();
  64. new Thread() {
  65. @Override
  66. public void run() {
  67. BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  68. String line = null;
  69. try {
  70. while((line = err.readLine()) != null) {
  71. log.debug("err: " + line);
  72. }
  73. }
  74. catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. finally {
  78. try {
  79. err.close();
  80. }
  81. catch (IOException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. }
  86. }.start();
  87. }
  88. private static void closeStream(Closeable stream) {
  89. if (stream != null) {
  90. try {
  91. stream.close();
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. }
  97. public static void unZip(String zipPath, String dataPath) {
  98. log.info("解压zip开始");
  99. if(!new File(dataPath).getParentFile().exists()){
  100. new File(dataPath).getParentFile().mkdirs();
  101. }
  102. String command = "unzip -O GBK/GB18030CP936 " + zipPath + " -d " + dataPath;
  103. execCmd(command);
  104. log.info("解压zip完毕:" + command);
  105. }
  106. public static void zip(String dataPath, String zipPath) {
  107. log.info("打包zip开始");
  108. String command ="cd " + dataPath + " && zip -r " + zipPath + " * " ;
  109. execCmd(command);
  110. log.info("解压zip完毕:" + command);
  111. }
  112. /**
  113. * oss文件上传命令 bash /opt/ossutil/fyun-upload.sh {bucket} {srcPath} {destPath} {fyunType} {opType}
  114. * opType: file or folder
  115. * //@param bucket 桶名
  116. * @param srcPath 源文件路径
  117. * @param destPath 目标文件路径
  118. * //@param fyunType oss or aws
  119. */
  120. public static void yunUpload(String srcPath,String destPath){
  121. String opType = srcPath.contains(".")? "file":"folder" ;
  122. String cmd = String.format(ShellCmd.FYUN_UPLOAD, "4dkankan",srcPath,destPath, CacheUtil.uploadType,opType);
  123. execCmd(cmd);
  124. }
  125. public static void yunDownload(String srcPath,String destPath){
  126. String opType = srcPath.contains(".")? "file":"folder" ;
  127. String cmd = String.format(ShellCmd.FYUN_DOWN,"4dkankan",srcPath,destPath, CacheUtil.uploadType,opType);
  128. execCmd(cmd);
  129. }
  130. }