1、  在使用phantomjs的时候需要下载phantomjs,网上有window版本和Linux版本。将phantomjs放在Linux上的指定位置之后(如下面的/home/tpl/phantomjs-1.9.7-linux-x86_64/处),

2、  按照类似如下命令进行测试,是否可以生成图片(执行前检查phantomjs的权限,要有执行权限):

/home/tpl/phantomjs-1.9.7-linux-x86_64/bin/phantomjs

/home/tpl/phantomjs-1.9.7-linux-x86_64/examples/test.js

http://www.baidu.com

/home/test/index.png

注意上面的命令,每个之间用空格隔开,其中:

/home/tpl/phantomjs-1.9.7-linux-x86_64/bin/phantomjs表示的是phantomjs所在的位置

/home/tpl/phantomjs-1.9.7-linux-x86_64/examples/test.js表示使用快照的时候需要的一个js

http://www.baidu.com  要生成快照的页面

/home/test/index.png  表示要将截的图放在的位置

3、要使用程序生成快照,需要执行shell命令或者cmd命令

能够兼容java和windows的一段java代码如下:

package xxxxx.webservice.snapshot;

import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.URL;

import com.ucap.utils.OsUtils;

/**

* SnapshotServiceImpl.java 用于生成快照的服务类

*

* @attention 使用注意事项

* @author

* @date

* @note begin modify by 涂作权 2016-5-19 判断在Linux上的情况

*/

public class SnapshotServiceImpl implements SnapshotService {

/**

* phantomjsPath   :phantomjs

*     /home/tpl/phantomjs-1.9.7-linux-x86_64/bin/phantomjs

*     /home/tpl/phantomjs-1.9.7-linux-x86_64/examples/test.js

*     http://xxxxx:5555/tpl/special/2016/5/19/2d0cd5d12a974e438a2f4a13300f60d2/index.html

*

* exportPath      :导出路径,这是一个绝对路径

*

* 关于phantomjs的执行命令如:

*     /home/tpl/phantomjs-1.9.7-linux-x86_64/bin/phantomjs

*     /home/tpl/phantomjs-1.9.7-linux-x86_64/examples/test.js

*     http://xxxx:5555/tpl/special/2016/5/19/2d0cd5d12a974e438a2f4a13300f60d2/index.html

*     /home/test/index.png

*/

public String createSnapshot(String phantomjsPath, String exportPath) {

URL path = this.getClass().getClassLoader().getResource("/");

String str = path.getPath();

File file = new File(str);

str = file.getParentFile().getParentFile().getParentFile()

.getAbsolutePath()

+ "/temp" + exportPath;

String[] strArray = null;

// 判断linux的执行情况

if (OsUtils.IS_LINUX) {

strArray = new String[3];

strArray[0] = "/bin/sh";

strArray[1] = "-c";

strArray[2] = phantomjsPath + " " + str;

} else if (OsUtils.IS_WINDOWS) {

strArray = new String[4];

strArray[0] = "cmd.exe";

strArray[1] = "/C";

strArray[2] = phantomjsPath;

strArray[3] = str;

}

File fileInfo = new File(str);

File fi = new File(fileInfo.getParentFile().getAbsolutePath());

if (!fi.exists()) {

fi.mkdirs();

}

// windows下phantomjs位置

Runtime rt = Runtime.getRuntime();

Process process = null;

try {

process = rt.exec(strArray);

} catch (IOException e) {

e.printStackTrace();

}

StringBuffer sbf = null;

InputStream is = null;

try {

is = process.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(is));

sbf = new StringBuffer();

String tmp = "";

while ((tmp = br.readLine()) != null) {

sbf.append(tmp);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (is != null) {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

is = null;

}

}

return sbf.toString();

}

}

其中一个依赖的test.js如下:

var page = require('webpage').create();

//-----------------------------获取窗口的宽高信息,并设置,write by 涂作权 start------------------------------------

var winWidth = null;

var winHeight = null;

// 获取窗口宽度

if (window.innerWidth) {

winWidth = window.innerWidth;

} else if ((document.body) && (document.body.clientWidth)) {

winWidth = document.body.clientWidth;

}

// 获取窗口高度

if (window.innerHeight) {

winHeight = window.innerHeight;

} else if ((document.body) && (document.body.clientHeight)) {

winHeight = document.body.clientHeight;

}

// 通过深入 Document 内部对 body 进行检测,获取窗口大小

if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {

winHeight = document.documentElement.clientHeight;

winWidth = document.documentElement.clientWidth;

}

page.viewportSize = {width:winWidth,height:winHeight};

page.clipRect = {top:0,left:0,width:winWidth,height:winHeight};

//-----------------------------获取窗口的宽高信息,并设置end--------------------------------------

//-----------------------------------------------------------------------------------------------

page.settings = {

javascriptEnabled:false,

loadImages:true,

userAgent:'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) PhantomJS/19.0'

};

//-----------------------------------------------------------------------------------------------

system = require('system');

//var url = 'http://yule.club.sohu.com/gifttrade/thread/2m2efbrpfui';

var address;

var loca;

if(system.args.length == 1){

phantom.exit();

}else{

adress = system.args[1];

loca = system.args[2];

page.open(adress, function (status){

if (status != "success"){

console.log('FAIL to load the address');

phantom.exit();

}

page.evaluate(function(){

//此函数在目标页面执行的,上下文环境非本phantomjs,所以不能用到这个js中其他变量

//window.scrollTo(0,10000);//滚动到底部

//滚动到自适应高度

//window.document.body.scrollTop = document.body.scrollHeight;

/* window.setTimeout(function(){

var plist = document.querySelectorAll("a");

var len = plist.length;

while(len)

{

len--;

var el = plist[len];

el.style.border = "1px solid red";

}

},5000);*/

});

window.setTimeout(function (){

//在本地生成截图

page.render(loca);

//console.log(loca);

phantom.exit();

}, 1000);

});

}

phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本)的更多相关文章

  1. java中构建同时兼容linux和windows程序时遇到的文件路径分割符问题解决方案

    最近在做一个自动上传文件的客户端,因为 file.getAbsolutePath()  在Mac和linux下的分割符是“/”,而在windows操作系统下的分割符则是“\”,我们程序中固然可以通过调 ...

  2. "如何用70行Java代码实现深度神经网络算法" 的delphi版本

     http://blog.csdn.net/hustjoyboy/article/details/50721535 "如何用70行Java代码实现深度神经网络算法" 的delphi ...

  3. CVE漏洞爬虫java代码依赖-TestNG

    TestNG是Java中的一个测试框架,而该CVE漏洞爬虫示例中所涉及到的java代码中, \Crawler\src\com\***\ThreaderRun.java文件在导入import org.t ...

  4. java生成zip包兼容Linux

    /* 这个方法只用在windows中用服务器为Linux就不行 */ package common.util; import java.io.File;import java.io.FileInput ...

  5. 个人项目-WC(C/C++ 兼容Linux和Windows)

    一.Github地址 https://github.com/S-TRAVELER/WC 实现的功能: 支持 -c 支持 -w 支持 -l 支持 -s 支持 -a 图形界面 多参数(文件名或通配符表达式 ...

  6. 使用java代码关闭指定端口的程序-windows

    转载请请在页首注明作者与出处 一:问题由史 今天遇到一个问题,就是在实现自动化灾备的时候,发现原有死掉的程序没有完全关闭,当然这都不是本文的重点,重点是这个时候,我得把它完全关闭,所以才有了这篇文章. ...

  7. java读properties的通用类,兼容linux和windows

    package util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; / ...

  8. 给大家分享下坐标转换的代码的JS和Python两个版本的源码【转】

    /** * Created by Wandergis on 2015/7/8. * 提供了百度坐标(BD09).国测局坐标(火星坐标,GCJ02).和WGS84坐标系之间的转换 */ /** * 百度 ...

  9. java 如何判断操作系统是Linux还是Windows

    String os = System.getProperty("os.name"); if(os.toLowerCase().startsWith("win") ...

随机推荐

  1. Linux 管理软件

    公司的openfire先前运行在windows上的,但由于在windows上openfire内存机制问题,最多只能占用2GB内存,且时间稍微长久一些就会自动挂掉,用户无法登陆和连接,因此迁移到了Cen ...

  2. 如何去掉修改Joomla、joomlart及其模版版权、标志、图标的方法

    Joomla是遵循GNU通用公共授权(GPL)的自由软件,我们虽然不推荐将Joomla的所有版权删除,但有些必要的信息还是需要修改的,下面以JoomlArt.com 的JA_teline_iii_v2 ...

  3. 进程间通信——XSI IPC之消息队列

    进程间通信XSI IPC有3种:消息队列.共享内存.信号量.它们之间有很多相似之处,但也有各自的特殊的地方.消息队列作为其中比较简单的一种,它会有些什么东西呢,来一起探讨探讨.. 消息队列结构 消息队 ...

  4. Winform DevExpress控件库(二) 使用SplashScreenManager控件定制程序加载页面

    SplashScreenManager控件:主要作用是显示在进行耗时操作时的等待界面: 位于 工具箱 -> Navigation & Layout(导航栏与布局类控件) 目录下: 在工具 ...

  5. Spring声明式事务总结

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  6. Zookeeper的安装配置及基本开发

    一.简介 Zookeeper 是分布式服务框架,主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集群管理.分布式应用配置项的管理等等. ZooKeeper的目标就 ...

  7. 23 服务的启动Demo2

    MainActivity.java package com.qf.day23_service_demo2; import android.app.Activity; import android.co ...

  8. 【安卓开发】Layout Inflation不能这么用

    Layout inflation在Android上下文环境下转换XML文件成View结构对象的时候需要用到. LayoutInflater这个对象在Android的SDK中很常见,但是你绝对没想到竟然 ...

  9. 通过grub-install命令把grub安装到u盘

    通过grub-install命令把grub安装到u盘 ①准备一个u盘,容量不限,能有1MB都足够了. ②把u盘格式化(我把u盘格式化成FAT.fat32格式了,最后证明也是成功的).③开启linux系 ...

  10. [端口扫描]S扫描器跨网段扫描

    最近看了下端口扫描,用了几款扫描器,nmap啊,x-sacn等.之前很少关注安全方面的东西,所以也比较菜. 其中有一款叫做 "S扫描器"的,扫描速度非常快,可以大网段的扫描,几十万 ...