URL截图

根据URL网页截图,看似很难,其实很简单,只要写个main函数调用一下方法即可。

下载phantomjs解压放在D盘根目录下;下载地址:http://phantomjs.org/

public static void saveImage(String url, String path) {  
        if (!url.startsWith("http://")) {  
            url = "http://" + url;  
        }  
        String phantomDir = "D:/phantomjs";  
        
        StringBuffer buffer = new StringBuffer();  
        buffer.append(phantomDir + "/phantomjs.exe ");  
        buffer.append(" --ignore-ssl-errors=yes ")
            .append("'"+phantomDir + "/examples/rasterize.js' '")
            .append(url + "' ")
            .append(path);  
          
        try { 
            Process process = Runtime.getRuntime().exec(buffer.toString());  
            InputStream eis = process.getErrorStream();  
            byte[] buf = new byte[1024];  
            int len = 0;  
            while ((len = eis.read(buf)) != -1) {  
                System.out.println(new String(buf, 0, len));  
            }  
            eis.close();  
  
            InputStream is = process.getInputStream();  
            buf = new byte[1024];  
            while ((len = is.read(buf)) != -1) {  
                System.out.println(new String(buf, 0, len));  
            }  
            is.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }

 

posted @ 2014-01-08 16:52  簡單D幸福  阅读(491)  评论(0编辑  收藏  举报