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 AES加密,兼容Linux和Windows

    项目背景: 需要对下载链接的关键数据进行加密,解码: import java.io.UnsupportedEncodingException; import java.security.Invalid ...

  2. c程序兼容多个linux,个人项目-WC(C/C++ 兼容Linux和Windows)

    一.Github地址 实现的功能: 支持 -c 支持 -w 支持 -l 支持 -s 支持 -a 图形界面 多参数(文件名或通配符表达式) 跨系统平台 多种编程语言 支持中文 二.PSP表格 PSP2. ...

  3. 完美解决:Java微信语音amr格式转mp3格式,兼容Linux/Mac/Windows,支持Maven

    少费话,先上代码 引入 maven 依赖 <dependency><groupId>com.github.dadiyang</groupId><artifac ...

  4. java 微信语音下载电脑版,完美解决:Java微信语音amr格式转mp3格式,兼容Linux/Mac/Windows,支持Maven...

    首先感谢 : 直接上代码 1.首先引入maven依赖(使用maven这点我很喜欢) com.github.dadiyang jave 1.0.4 2.调用 AudioUtils.amrToMp3 方法 ...

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

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

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

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

  7. java代码ping服务IP工具类(Windows,Linux环境)

    public class PingIpUtils {private static Logger logger = LoggerFactory.getLogger(PingIpUtils.class); ...

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

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

  9. 微信H5分享 代码和详细配置步骤 js + Java 代码

    正常分享状态显示: 分享异常状态显示: 一切变得那么的...无助!!! 开始以为在H5页面上添加一些东西即可,后来发现,完全不是自己想象的那个样子. 这个东西,对于一个从未用过微信JS的码农来说,或许 ...

最新文章

  1. 清华博士后用10分钟讲解AlphaCode背后的技术原理,原来程序员不是那么容易被取代的!...
  2. 关于C和C++的一点观点
  3. Docker的安装和Redis容器
  4. css sprite讲解与使用实例
  5. 荒野行动系统推荐观战榜_荒野行动 观战延迟投票结果公示 更新计划抢先看!...
  6. Python:通过命令行发送新浪微博
  7. iOS UITextView和UITextViewDelegate
  8. onvif协议之抓图
  9. 怎么让envi中影像背景为0_eCogniton波段权重设置——基于ENVI的波段信息量计算
  10. delphi xe 服务器发消息,Delphi XE IdTCPClient和IdTCPServer 发送和接收文件
  11. Ribbon原理及使用详解
  12. android电量伪装原理,安卓电量伪装神器
  13. 【微信小程序】表单验证WxValidate.js使用
  14. 微信小程序 教你如何复制页面路径 (以及京东、虎牙、苏宁、拼多多、等大厂的加密路径详解)(多图!!!)
  15. 【Spring Boot 2.0学习之旅-15】SpringBoot2.0响应式编程
  16. 第13章 原始套接字
  17. Android使用字体代替图标
  18. 关于影视剪辑中的pr软件你了解多少?
  19. 点到直线的垂直交点坐标 和 距离计算
  20. 计算机cpu性能过剩吗,如何选择合适的CPU想避免坑就来看看吧

热门文章

  1. 数据库系统概论期末复习以及知识整理
  2. mysql数据库入门教程(13):存储过程
  3. 2--mkdir命令
  4. Spring IoC(二)IoC容器的初始化过程
  5. VTK:交叉点 PolyData 过滤器用法实战
  6. TK:vtkCellTreeLocator用法实战
  7. wxWidgets:wxArrayString类用法
  8. boost::signals2::slot相关的测试程序
  9. boost::polygon::detail::robust_fpt相关的测试程序
  10. boost::remove_edge用法的测试程序