package mackimg; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * @Description:根據網頁地址轉換成圖片 * @Author: admin * @CreateDate: 2018年6月22日 */ public class PhantomTools { private static String tempPath = "F:/phantomjs";// 圖片保存目錄 private static String BLANK = " "; // 下面內容能夠在配置文件中配置 private static String binPath = "D:/phantomjs-2.1.1-windows/bin/phantomjs.exe";// 插件引入地址 private static String jsPath = "D:/phantomjs-2.1.1-windows/rasterize.js";// js引入地址 // 執行cmd命令 public static String cmd(String imgagePath, String url) { return binPath + BLANK + jsPath + BLANK + url + BLANK + imgagePath; } //關閉命令 public static void close(Process process, BufferedReader bufferedReader) throws IOException { if (bufferedReader != null) { bufferedReader.close(); } if (process != null) { process.destroy(); process = null; } } /** * @param userId * @param url * @throws IOException */ public static void printUrlScreen2jpg(String url) throws IOException{ String imgagePath = tempPath+"/"+System.currentTimeMillis()+".png";//圖片路徑 //Java中使用Runtime和Process類運行外部程序 Process process = Runtime.getRuntime().exec(cmd(imgagePath,url)); InputStream inputStream = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String tmp = ""; while ((tmp = reader.readLine()) != null) { close(process,reader); } System.out.println("success"); } public static void main(String[] args) throws IOException { String url = "https://www.baidu.com/";//以百度網站首頁爲例 PhantomTools.printUrlScreen2jpg(url); } }
以上能夠參考文章:點我點我html
Exception in thread "main" java.io.IOException: Stream closed at java.io.BufferedReader.ensureOpen(BufferedReader.java:122) at java.io.BufferedReader.readLine(BufferedReader.java:317) at java.io.BufferedReader.readLine(BufferedReader.java:389) at mackimg.PhantomTools.printUrlScreen2jpg(PhantomTools.java:48) at mackimg.PhantomTools.main(PhantomTools.java:59)
String url = "http://www.cnblogs.com/han108/p/9216583.html";
能正常運行,可是後臺沒有圖片.java
我在網上看了別人用的另外一個js文件,我命名爲22.js.內容是:web
var page = require('webpage').create(), system = require('system'), address, output, size; if (system.args.length < 3 || system.args.length > 5) { console.log('Usage: rasterize.js URL filename'); phantom.exit(1); } else { address = system.args[1]; output = system.args[2]; page.viewportSize = { width: 600, height: 600 }; page.open(address, function (status) { // 經過在頁面上執行腳本獲取頁面的渲染高度 var bb = page.evaluate(function () { return document.getElementsByTagName('html')[0].getBoundingClientRect(); }); // 按照實際頁面的高度,設定渲染的寬高 page.clipRect = { top: bb.top, left: bb.left, width: bb.width, height: bb.height }; // 預留必定的渲染時間 window.setTimeout(function () { page.render(output); page.close(); console.log('render ok'); }, 1000); }); }
不懂,不知道,去他媽的windows
while ((tmp = reader.readLine()) != null) { } close(process,reader);
能夠解決拋出異常和後臺沒法獲取圖片的問題,可是若是使用22.js,會出現程序運行完沒法自動中止的問題.網站
window.setTimeout(function () { page.render(output); page.close(); console.log('render ok'); }, 1000); });
js文件執行完會發送一句"render ok",這就致使java代碼中的while ((tmp = reader.readLine()) != null)
沒法跳出,陷入阻塞狀態,沒法理解的是,此時天然沒法執行到close(process,reader);
,可是後臺仍然能夠得到圖片.ui
若是此時把代碼更改成:lua
while ((tmp = reader.readLine()) != null) { close(process,reader); break; }
此時能正常運行,後臺也有圖片.url
代碼更改成:插件
while ((tmp = reader.readLine()) != null) { close(process,reader); break; }