123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package com.fdkankan.fusion.common.util;
- import cn.hutool.core.io.file.FileReader;
- import com.alibaba.fastjson.JSONObject;
- import lombok.extern.slf4j.Slf4j;
- import java.io.BufferedReader;
- import java.io.Closeable;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.nio.charset.StandardCharsets;
- @Slf4j
- public class ShellUtil {
- public final static String LAS_TO_BIN = "bash /home/ubuntu/bin/PotreeConverter.sh @inPath @outPath ''";
- /**
- * 执行系统命令, 返回执行结果
- * @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);
- process = Runtime.getRuntime().exec(cmd);
- // 方法阻塞, 等待命令执行完成(成功会返回0)
- process.waitFor();
- // 获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入)
- bufrIn = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
- bufrError = new BufferedReader(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8));
- // 读取输出
- // String line = null;
- // while ((line = bufrIn.readLine()) != null) {
- // result.append(line).append('\n');
- // }
- // while ((line = bufrError.readLine()) != null) {
- // result.append(line).append('\n');
- // }
- }catch (Exception e){
- e.printStackTrace();
- }finally {
- closeStream(bufrIn);
- closeStream(bufrError);
- // 销毁子进程
- if (process != null) {
- process.destroy();
- }
- }
- // 返回执行结果
- log.info("执行cmd:{},结果:{},耗时:{}", cmd,result.toString(),System.currentTimeMillis() -startTime);
- }
- 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开始");
- String command = "unzip -O GBK/GB18030CP936 " + zipPath + " -d " + dataPath;
- execCmd(command);
- log.info("解压zip完毕:" + command);
- }
- public static void unRar(String rarPath, String dataPath) {
- log.info("解压rar开始");
- String command = "unrar e " + rarPath + " " + dataPath;
- execCmd(command);
- log.info("解压rar完毕:" + command);
- }
- /**
- * 修改cloud.js 文件
- *
- * @param path
- * @return
- * @throws IOException
- */
- public static JSONObject fixCloud(String path) throws IOException {
- FileReader fileReader = new FileReader(path);
- String str = fileReader.readString();
- JSONObject json = JSONObject.parseObject(str);
- String[] pointAttributes = {
- "POSITION_CARTESIAN",
- "COLOR_PACKED",
- "NORMAL_OCT16"
- };
- // String[] pointAttributes = {
- // "POSITION_CARTESIAN",
- // "COLOR_PACKED",
- // "NORMAL_OCT16",
- // "INTENSITY",
- // "CLASSIFICATION"
- // };
- json.put("pointAttributes", pointAttributes);
- return json;
- }
- }
|