修改执行部分的代码,改成用InputStream.read(byte[])的方法从流中读取数据
package com.example.demo.utils;
import java.io.*;
public class CMDExecute {
public synchronized String run(String cmd) throws IOException {
String line = null;
String result = "";
try {
String[] commands={"/bin/sh", "-c",cmd};
ProcessBuilder builder = new ProcessBuilder(commands);
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while (in.read(re) != -1) {
System.out.println(new String(re));
result = result + new String(re);
}
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}