ShellUtil.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.fdkankan.fusion.common.util;
  2. import cn.hutool.core.io.file.FileReader;
  3. import com.alibaba.fastjson.JSONObject;
  4. import lombok.extern.slf4j.Slf4j;
  5. import java.io.BufferedReader;
  6. import java.io.Closeable;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.net.URL;
  10. import java.nio.charset.StandardCharsets;
  11. @Slf4j
  12. public class ShellUtil {
  13. public final static String LAS_TO_BIN = "bash /home/ubuntu/bin/PotreeConverter.sh @inPath @outPath ''";
  14. /**
  15. * 执行系统命令, 返回执行结果
  16. * @param cmd 需要执行的命令
  17. */
  18. public static void execCmd(String cmd) {
  19. StringBuilder result = new StringBuilder();
  20. Process process = null;
  21. BufferedReader bufrIn = null;
  22. BufferedReader bufrError = null;
  23. long startTime = System.currentTimeMillis();
  24. try {
  25. // 执行命令, 返回一个子进程对象(命令在子进程中执行)
  26. log.info("执行cmd:{}",cmd);
  27. process = Runtime.getRuntime().exec(cmd);
  28. // 方法阻塞, 等待命令执行完成(成功会返回0)
  29. process.waitFor();
  30. // 获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入)
  31. bufrIn = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
  32. bufrError = new BufferedReader(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8));
  33. // 读取输出
  34. // String line = null;
  35. // while ((line = bufrIn.readLine()) != null) {
  36. // result.append(line).append('\n');
  37. // }
  38. // while ((line = bufrError.readLine()) != null) {
  39. // result.append(line).append('\n');
  40. // }
  41. }catch (Exception e){
  42. e.printStackTrace();
  43. }finally {
  44. closeStream(bufrIn);
  45. closeStream(bufrError);
  46. // 销毁子进程
  47. if (process != null) {
  48. process.destroy();
  49. }
  50. }
  51. // 返回执行结果
  52. log.info("执行cmd:{},结果:{},耗时:{}", cmd,result.toString(),System.currentTimeMillis() -startTime);
  53. }
  54. private static void closeStream(Closeable stream) {
  55. if (stream != null) {
  56. try {
  57. stream.close();
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63. public static void unZip(String zipPath, String dataPath) {
  64. log.info("解压zip开始");
  65. String command = "unzip -O GBK/GB18030CP936 " + zipPath + " -d " + dataPath;
  66. execCmd(command);
  67. log.info("解压zip完毕:" + command);
  68. }
  69. public static void unRar(String rarPath, String dataPath) {
  70. log.info("解压rar开始");
  71. String command = "unrar e " + rarPath + " " + dataPath;
  72. execCmd(command);
  73. log.info("解压rar完毕:" + command);
  74. }
  75. /**
  76. * 修改cloud.js 文件
  77. *
  78. * @param path
  79. * @return
  80. * @throws IOException
  81. */
  82. public static JSONObject fixCloud(String path) throws IOException {
  83. FileReader fileReader = new FileReader(path);
  84. String str = fileReader.readString();
  85. JSONObject json = JSONObject.parseObject(str);
  86. String[] pointAttributes = {
  87. "POSITION_CARTESIAN",
  88. "COLOR_PACKED",
  89. "NORMAL_OCT16"
  90. };
  91. // String[] pointAttributes = {
  92. // "POSITION_CARTESIAN",
  93. // "COLOR_PACKED",
  94. // "NORMAL_OCT16",
  95. // "INTENSITY",
  96. // "CLASSIFICATION"
  97. // };
  98. json.put("pointAttributes", pointAttributes);
  99. return json;
  100. }
  101. }