package com.iailab.common.utils;
|
|
import com.alibaba.fastjson.JSONObject;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.io.BufferedReader;
|
import java.io.InputStreamReader;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @author PanZhibao
|
* @Description
|
* @createTime 2023年01月11日 11:03:00
|
*/
|
@Slf4j
|
public class PythonUtil {
|
|
public static Map<String, Object> execPy(String[] command) throws Exception {
|
List<String> lines = new ArrayList<>();
|
try {
|
Process proc = Runtime.getRuntime().exec(command);
|
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GBK"));
|
String line = null;
|
|
while ((line = in.readLine()) != null) {
|
log.info(line);
|
lines.add(line);
|
}
|
// 0表示成功,1表示远程调用失败,2表示python脚本出错
|
int waitFor = proc.waitFor();
|
log.info("waitFor=" + waitFor);
|
if (waitFor == 1) {
|
throw new Exception("远程调用失败");
|
} else if (waitFor == 2) {
|
throw new Exception("python脚本出错");
|
} else if (waitFor == 9009) {
|
throw new Exception("环境变量配置错误");
|
} else if (waitFor != 0) {
|
throw new Exception("调用python失败");
|
}
|
} catch (Exception ex) {
|
log.error("运行python异常!command=" + command.toString());
|
throw ex;
|
}
|
return JSONObject.parseObject(lines.get(0));
|
}
|
}
|