本文已参与「新人创作礼」活动,一起开启掘金创作之路。
调用案例:
/hello?a=/bin/sh&b=-c&cmd=ps -ef
代码:
@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 "";
}
} `