phantomjs拋出IOException

使用phantomjs對網頁進行截圖遇到的問題

問題描述:

  1. 使用的phantomjs版本:phantomjs-2.1.1-windows
  2. 使用的截圖js文件,\phantomjs-2.1.1-windows\examples\rasterize.js
  3. 使用的java驅動代碼:
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文件

我在網上看了別人用的另外一個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);
    });
}
  1. 使用百度連接,拋出上面提到的異常.後臺沒有圖片
  2. 使用cnblogs連接,拋出上面的異常,後臺有圖片

問題分析

不懂,不知道,去他媽的windows

問題解決

  1. 把代碼更改成:
while ((tmp = reader.readLine()) != null) {  
        }
close(process,reader);

能夠解決拋出異常和後臺沒法獲取圖片的問題,可是若是使用22.js,會出現程序運行完沒法自動中止的問題.網站

  1. 注意到,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

  1. 按照第二種更改後的條件下,在把js文件更改成:\phantomjs-2.1.1-windows\examples\rasterize.js,程序能正常運行,後臺有圖片;

推薦解決辦法

代碼更改成:插件

while ((tmp = reader.readLine()) != null) {  
            close(process,reader);
            break;
        }
相關文章
相關標籤/搜索