提交 | 用户 | 时间
|
ce910c
|
1 |
package com.iailab.module.data.dev.hikvision.utils; |
H |
2 |
|
|
3 |
import lombok.extern.slf4j.Slf4j; |
|
4 |
|
|
5 |
import java.io.BufferedReader; |
|
6 |
import java.io.InputStreamReader; |
|
7 |
import java.util.ArrayList; |
|
8 |
import java.util.List; |
|
9 |
|
|
10 |
/** |
|
11 |
* @author PanZhibao |
|
12 |
* @Description |
|
13 |
* @createTime 2023年01月11日 11:03:00 |
|
14 |
*/ |
|
15 |
@Slf4j |
|
16 |
public class PythonUtil { |
|
17 |
|
|
18 |
public static List<String> execPy(String[] command) { |
|
19 |
List<String> lines = new ArrayList<>(); |
|
20 |
try { |
|
21 |
Process proc = Runtime.getRuntime().exec(command); |
|
22 |
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"GBK")); |
|
23 |
String line = null; |
|
24 |
|
|
25 |
while ((line = in.readLine()) != null) { |
|
26 |
log.info(line); |
|
27 |
lines.add(line); |
|
28 |
} |
|
29 |
// 0表示成功,1表示远程调用失败,2表示python脚本出错 |
|
30 |
int waitFor = proc.waitFor(); |
|
31 |
log.info("waitFor=" + waitFor); |
|
32 |
if (waitFor != 0) { |
|
33 |
throw new Exception("调用python失败"); |
|
34 |
} |
|
35 |
} catch (Exception ex) { |
|
36 |
ex.printStackTrace(); |
|
37 |
log.error(ex.getMessage()); |
|
38 |
log.error("运行python异常!command=" + command.toString()); |
|
39 |
} |
|
40 |
return lines; |
|
41 |
} |
|
42 |
} |