ShellUtil.java 4.2 KB

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