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
/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/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;
is = process.getinputstream();
bufferedreader br = new bufferedreader(new inputstreamreader(is));
sbf = new stringbuffer();
string tmp = "";
while ((tmp = br.readline()) != null) {
sbf.append(tmp);
}
} 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);
});