吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3906|回复: 6
收起左侧

[Java 转载] 给定一个网址,实现该网页的快照功能。

[复制链接]
llsydn 发表于 2019-11-18 14:03
本帖最后由 wushaominkk 于 2019-11-18 20:19 编辑


[Java] 纯文本查看 复制代码
//网页快照工具类
package com.snap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

public class SnapUtills {

    static String phantomjsPath;

    static {
        // 判断是什么环境
        String systemType = System.getProperties().getProperty("os.name");
        if (systemType != null && systemType.toLowerCase().indexOf("windows") > -1) {
            phantomjsPath = System.getProperty("user.dir") + "\\phantomjs\\phantomjs-2.1.1-windows";
        } else {
            phantomjsPath = System.getProperty("user.dir") + "\\phantomjs\\phantomjs-2.1.1-linux-x86_64";
        }
    }

    /**
     * 生成网页缩略图
     */
    public static String snapUrl(String url, String savePath) throws Exception {
        String cmd = phantomjsPath + "/bin/phantomjs ";
        String jspath = phantomjsPath + "/examples/rasterize.js";

        // 执行命令,超时60秒退出
        execCMD(cmd + jspath + " " + url + " " + savePath, 60);
        return savePath;
    }

    /**
     * 命令执行(超过多少秒退出)
     *
     * @Param cmd     命令
     * @param timeout 秒
     */
    private static String execCMD(String cmd, int timeout) throws Exception {
        Process process = null;
        StringBuilder sbStd = new StringBuilder();
        StringBuilder sbErr = new StringBuilder();

        long start = System.currentTimeMillis() / 1000;
        try {
            process = Runtime.getRuntime().exec(cmd);

            BufferedReader brStd = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            BufferedReader brErr = new BufferedReader(new InputStreamReader(
                    process.getErrorStream()));
            String line = null;

            while (true) {
                if (brStd.ready()) {
                    line = brStd.readLine();
                    sbStd.append(line + "\n");
                    continue;
                }
                if (brErr.ready()) {
                    line = brErr.readLine();
                    sbErr.append(line + "\n");
                    continue;
                }
                if (process != null) {
                    try {
                        process.exitValue();
                        break;
                    } catch (IllegalThreadStateException e) {
                    }
                }
                if (System.currentTimeMillis() / 1000 - start > timeout) {
                    sbErr.append("\ntimeout");
                    break;
                }

                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                }
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
        if (sbErr.length() > 0) {
            throw new Exception(sbErr.toString());
        } else {
            return sbStd.toString();
        }
    }
}

//测试
package com.snap;

public class Test {

    public static void main(String[] args) throws Exception {
        String savePath = System.getProperty("user.dir") + "\\snap-image\\" + System.currentTimeMillis() + ".jpg";
        String snapUrl = SnapUtills.snapUrl("https://www.baidu.com", savePath);
        System.out.println("生成网页快照图片的路径:" + snapUrl);
    }
}

具体项目代码:https://download.csdn.net/download/qq_24101357/11970303

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

490694561 发表于 2019-11-18 15:10
puppteer为啥不试试这个东西呢,自动生成jpg
派大星星 发表于 2019-11-19 15:17
838384855 发表于 2019-12-17 17:14
幻--想 发表于 2021-4-9 15:10
请问能分享一下希赛的资料吗
幻--想 发表于 2021-4-9 15:13

请问能分享一下希赛的资料吗,320264603@qq.com
 楼主| llsydn 发表于 2021-4-19 08:37
幻--想 发表于 2021-4-9 15:13
请问能分享一下希赛的资料吗,

希赛的啥资料呀
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-12 19:24

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表