.predictpython
时间: 2023-11-03 19:59:52 浏览: 29
根据您提供的代码,问题出现乱码的原因是在执行cmd命令返回结果时,没有将字节流转化为正确的字符编码。为了解决这个问题,您可以尝试使用指定的字符编码来转换字节流。您可以使用以下代码来修改您的方法:
```java
public static String predictPython(String path, String param) throws IOException, InterruptedException {
String exe = "python";
String command = path + "WEB-INF\\classes\\python\\faceRecognition.py";
String[] cmdArr = new String[] { exe, command, param, path + "WEB-INF\\classes\\python\\" };
Process process = Runtime.getRuntime().exec(cmdArr);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")); // 指定字符编码为UTF-8
StringBuilder pythonResult = new StringBuilder(); // 使用StringBuilder拼接多行结果
String line;
while ((line = in.readLine()) != null) {
pythonResult.append(line); // 拼接每行结果
System.out.println("cmd返回结果:" + line);
}
in.close();
int result = process.waitFor();
System.out.println("执行结果:" + result);
return pythonResult.toString(); // 返回拼接的结果字符串
}
```