A description of the problem that is encountered with the Web page using PHANTOMJS:
- Version of PHANTOMJS used: phantomjs-2.1.1-windows
- Using the JS file, \phantomjs-2.1.1-windows\examples\rasterize.js
- Java Driver code used:
Package Mackimg;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.inputstream;import java.io.inputstreamreader;/** * @Description: Converted to image according to Web address * @Author: admin * @CreateDate: June 22, 2018 */public class Phanto Mtools {private static string TempPath = "F:/PHANTOMJS";//Picture Save directory private static string BLANK = ""; The following content can be configured in the configuration file for private static String BinPath = "D:/phantomjs-2.1.1-windows/bin/phantomjs.exe";//plug-in introduction address private s Tatic string jspath = "d:/phantomjs-2.1.1-windows/rasterize.js";//JS Introduction address//execute CMD command public static String cmd (strin G imgagepath, String URL) {return binpath + blank + jspath + blank + URL + blank + imgagepath; }//Close command public static void close (process process, BufferedReader BufferedReader) throws IOException {if (bu Fferedreader = 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";//Picture path//java using Runtime and Pro The Cess class runs the external program 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/";//Baidu website first Page for example phantomtools.printurlscreen2jpg (URL); }}
The above can refer to the article: Dot I dot I
An exception occurred after running:
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)
Change URL:
String url = "http://www.cnblogs.com/han108/p/9216583.html";
Works correctly, but there are no pictures in the background.
Replace JS file
I saw another JS file that someone used on the internet, I named 22.js. The content is:
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); });}
- Use Baidu link, throw the above mentioned exception. No pictures in the background
- Use Cnblogs link, throw above exception, background with picture
Problem analysis
Don't understand, don't know, go fuck
Problem solving
- Change the code to:
while ((tmp = reader.readLine()) != null) { }close(process,reader);
You can solve the problem of throwing exceptions and not getting pictures in the background, but if you use 22.js, the program will not stop automatically when it finishes running.
- Notice that the last few lines of the 22.js file are:
window.setTimeout(function () { page.render(output); page.close(); console.log(‘render ok‘); }, 1000); });
JS file execution will send a "render OK", which causes the Java code while ((tmp = reader.readLine()) != null)
can not jump out of the block state, it is impossible to understand that the nature can not be executed at this time close(process,reader);
, but still get the picture in the background.
If you change the code at this point to:
while ((tmp = reader.readLine()) != null) { close(process,reader); break; }
At this point can be normal operation, background also has pictures.
- Under the condition of the second change, the JS file is changed to: \phantomjs-2.1.1-windows\examples\rasterize.js, the program can run normally, the background has pictures;
Recommended Solutions
The code changes to:
while ((tmp = reader.readLine()) != null) { close(process,reader); break; }
Phantomjs throws IOException