今天在用Runtime.getRuntime().exec()时代码走了一个或者两个小时后就会自动阻塞,网上找到了原因,是因为没有对Process的输出信息及时清理导致进程阻塞,服务失效。于是,在Runtime.getRuntime().exec()之后,p.waitFor()之前加入如下线程代码:
String cmds_ = "/usr/bin/pdf2htmlEX --no-drm 1 --split-pages 1 " + "--embed-css 0 --embed-javascript 0 --embed-image 0 --embed-font 0 --css-filename html.css " + "--fit-width 700 --bg-format jpg --auto-hint 1 --svg-node-count-limit 1 --auto-hint 1 " + "--embed-external-font 0 --dest-dir " + htmlPath + " --page-filename " + path[path.length - 1].replace(".pdf", "") + "-%d.page " + pdfPath; String[] cmds = { "/bin/sh", "-c", cmds_ }; Process pro = Runtime.getRuntime().exec(cmds); StreamGobbler errorGobbler = new StreamGobbler(pro.getErrorStream(), "Error"); StreamGobbler outputGobbler = new StreamGobbler(pro.getInputStream(), "Output"); errorGobbler.start(); outputGobbler.start(); pro.waitFor();
StreamGobbler类: <pre name="code" class="java">public class StreamGobbler extends Thread { InputStream is; String type; public StreamGobbler(InputStream is, String type) { this.is = is; this.type = type; } public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { if (type.equals("Error")) { System.out.println("Error :" + line); } else { System.out.println("Debug:" + line); } } } catch (IOException ioe) { ioe.printStackTrace(); } } }