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; }