一、phantomjs介绍
(1)一个基于webkit内核的无界面浏览器,即没有UI界面,即它就是一个浏览器,只是其内的点击、翻页等人为相关操作需要程序设计实现。
(2)提供javascript API接口,即通过编写js程序可以直接与webkit内核交互,在此之上可以结合java语言等,通过java调用js等相关操作,从而解决了以前c/c++才能比较好的基于webkit开发优质采集器的限制。
(3)提供windows、linux、mac等不同os的安装使用包,也就是说可以在不同平台上二次开发采集项目或是自动项目测试等工作。
二、安装phantomjs
https://phantomjs.org/download.html
我下载的是phantomjs-2.1.1-windows版本,自行根据情况下载
下载完成后解压
为方便使用,我将examples下面的rasterize.js复制到了bin目录下
之后可以通过cmd执行下面命令,进行测试,如果可以正常截图到D:/test.png,说明phantomjs软件没有问题,可以进行代码开发。
D:/phantomjs-2.1.1-windows/bin/phantomjs D:/phantomjs-2.1.1-windows/bin/rasterize.js https://www.bilibili.com/read/cv28388151/ D:/test.png
三、Java代码实现截图
我将phantomjs .exe和rasterize.js放到了resources根目录下
CutPictureController.java
@GetMapping("/getCutPictureByPhantomjs")
public void getCutPictureByPhantomjs(String url, HttpServletResponse response) {
cutPictureService.getCutPictureByPhantomjs(url,response) ;
}
CutPictureService.java
我这里实现的方案是将项目种的文件复制到对应系统的目录下,你也可以直接将phantomjs .exe和rasterize.js放到系统中,之后在代码里指定好路径就行。就不用我这么麻烦!
public void getCutPictureByPhantomjs(String url, HttpServletResponse response) {
try {
String os = System.getProperty("os.name").toLowerCase();
ClassPathResource jsResource = new ClassPathResource("rasterize.js");
InputStream jsInputStream = jsResource.getInputStream(