调用案例:
/hello?a=/bin/sh&b=-c&cmd=ps -ef
效果图,感觉有局限性,很多命令执行不了
执行返回的code,可以参考这个
Linux process.waitFor()返回值_wang.zhiyuan的博客-CSDN博客_linux waitfor
代码
@RequestMapping("/hello")
public String index(String a,String b,String cmd) {
//sudo xvfb-run wkhtmltopdf --enable-forms 你的URL 文件地址
//String[] cmdList = new String[]{"sudo xvfb-run wkhtmltopdf --enable-forms https://www.baidu.com /root/java/web/testbaidu.pdf"};
//mkdir /root/java/web/a
//String[] cmdList = new String[]{"/bin/sh","-c",cmd};
String[] cmdList = new String[]{a,b,cmd};
System.out.println(cmd);
String res = HtmlToPdf.executeNewFlow(cmdList);
return res;
}
package com.demo.pdf;
import java.io.*;
public class HtmlToPdf {
public static String executeNewFlow(String[] commands) {
try {
Process process = Runtime.getRuntime().exec(commands);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// waitFor 阻塞等待 异步进程结束,并返回执行状态,0代表命令执行正常结束。
System.out.println(process.waitFor());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return "";
}
}



