一、前期准备
1、下载phantomjs工具
 地址:https://phantomjs.org/download.html
 解压到指定文件夹,后续代码要调用该工具,记住路径
 2、准备好模板NetToPicMoban.js
 用于给phantomjs提供需要执行的js,具体放在那看自己的需求,我放在:D:/template/NetToPicMoban.js内容:
var page = require('webpage').create();
page.open(url, function(success){
    if(success==='success'){
        console.log('success');
        page.render(savename);
        phantom.exit();
    }else{
        console.log('error');
        phantom.exit();
    }
});
二、代码分析
1、从测试的main函数看起
public static void main(String[] args){
        //目标网页
        String url = "https://www.baidu.com" ;
        //生成的图片名称
        String picname = System.currentTimeMillis()+"sina.png" ;
        //要构建的目标js
        String jsname = "sina.js";
        //生成js
        UrlToImgSaveUtil.reload( url, picname,jsname);
        //调用系统的cmd 执行phantomjs.exe
        // cdm 表示命令行 
        // /c 表示执行后关闭窗口  
        // F: 表示转到F盘 看你的phantomjs.exe工具放在哪个盘就要转到哪个盘,否则生成不了图片
        // && 表示多个命令行关联,即下面字符串待执行三个命令行
        // cd 表示转到某个文件夹下,现在要转到phantomjs的bin目录下
        //  phantomjs.exe  xxxxxx.js  表示工具执行某个js文件
        String cmd1 = "cmd /c F: && cd F:\\tools\\phantomjs-2.1.1-windows\\bin\\" ;
        String cmd  = cmd1 + " && phantomjs.exe " + "D:\\template\\"+jsname;
        //执行cmd
        UrlToImgSaveUtil.implcmd(cmd);
    }
2、生成目标js文件
/**
     * 构建js文件
     * @param url
     * @param picname
     * @param jsname
     */
    public static void  reload(String url,String picname,String jsname){
        //这里面的路径都是相对路径
        String content = "";
        //netToPicMoban.js这个phantomjs 的一个js模版,修改相应的参数就可以实现我们要的功能
        String str = read(new File("D:/template/NetToPicMoban.js"));
        String content1 = str.replace("url", "'"+url+"'");
        content = content1.replace("savename", "'"+picname+"'");
        write(content,"D:/template/", jsname );
    }
3、文件读取和文件写法方法
 (可以封装到一个工具类)
/**
     * 文件读取
     * @param file
     * @return
     */
    public static String read(File file) {
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] b = new byte[(int) file.length()];
            fis.read(b);
            return new String(b, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 文件写入
     * @param content  文件内容
     * @param dirPath   保存路径
     * @param fileName   文件名称
     */
    public static void write(String content, String dirPath, String fileName) {
        File file = createFile(dirPath, fileName);
        try (FileWriter writer = new FileWriter(file)) {
            writer.write(content);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 创建文件
     * @param dirPath  文件路径
     * @param fileName  文件名称
     * @return
     */
    private static File createFile(String dirPath, String fileName) {
        String filePath = "";
        if (Objects.isNull(dirPath) || dirPath.isEmpty()) {
            filePath = fileName;
        } else {
            if (dirPath.endsWith("/")) {
                filePath = dirPath + fileName;
            } else {
                filePath = dirPath + "/" + fileName;
            }
            File dir = new File(dirPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
        }
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }
4、执行cmd
/**
 * 执行cmd
 * @param cmd
 */
public static void  implcmd(String cmd){//在java中调用执行cmd命令
    Process p;
    System.out.println(cmd);
    try {
        p = Runtime.getRuntime().exec(cmd);
        // 等待进程执行完成
        int exitCode = p.waitFor();
        //命令行运行内容打印
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        // 打印命令执行结果  为0表示成功
        System.out.println("Command executed with exit code: " + exitCode);
    } catch (IOException e) {
        System.out.println("e==="+e.getMessage());
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
三、整体代码
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
/**
 * @author Administrator
 */
public class UrlToImgSaveUtil {
    /**
     * 构建js文件
     * @param url
     * @param picname
     * @param jsname
     */
    public static void  reload(String url,String picname,String jsname){
        //这里面的路径都是相对路径
        String content = "";
        //netToPicMoban.js这个phantomjs 的一个js模版,修改相应的参数就可以实现我们要的功能
        String str = read(new File("D:/template/NetToPicMoban.js"));
        String content1 = str.replace("url", "'"+url+"'");
        content = content1.replace("savename", "'"+picname+"'");
        write(content,"D:/template/", jsname );
    }
    /**
     * 文件读取
     * @param file
     * @return
     */
    public static String read(File file) {
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] b = new byte[(int) file.length()];
            fis.read(b);
            return new String(b, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 文件写入
     * @param content
     * @param dirPath
     * @param fileName
     */
    public static void write(String content, String dirPath, String fileName) {
        File file = createFile(dirPath, fileName);
        try (FileWriter writer = new FileWriter(file)) {
            writer.write(content);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 创建文件
     * @param dirPath
     * @param fileName
     * @return
     */
    private static File createFile(String dirPath, String fileName) {
        String filePath = "";
        if (Objects.isNull(dirPath) || dirPath.isEmpty()) {
            filePath = fileName;
        } else {
            if (dirPath.endsWith("/")) {
                filePath = dirPath + fileName;
            } else {
                filePath = dirPath + "/" + fileName;
            }
            File dir = new File(dirPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
        }
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }
    /**
     * 执行cmd
     * @param cmd
     */
    public static void  implcmd(String cmd){//在java中调用执行cmd命令
        Process p;
        System.out.println(cmd);
        try {
            p = Runtime.getRuntime().exec(cmd);
            // 等待进程执行完成
            int exitCode = p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 打印命令执行结果
            System.out.println("Command executed with exit code: " + exitCode);
        } catch (IOException e) {
            System.out.println("e==="+e.getMessage());
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args){
        String url = "https://www.baidu.com" ;
        String picname = System.currentTimeMillis()+"sina.png" ;
        String jsname = "sina.js";
        //生成js
        UrlToImgSaveUtil.reload( url, picname,jsname);
        //调用系统的cmd 执行phantomjs.exe
        String cmd1 = "cmd /c F: && cd F:\\tools\\phantomjs-2.1.1-windows\\bin\\" ;
        String cmd  = cmd1 + " && phantomjs.exe " + "D:\\template\\"+jsname;
        //执行cmd
        UrlToImgSaveUtil.implcmd(cmd);
    }
}
四、运行分析
1、代码运行打印
 
2、目标sina.js生成
 
3、图片位置在工具的bin目录下
 
五、注意事项
1、phantomjs工具安装位置
 2、js模板和目标模板位置
 3、cmd命令的写法与这些位置息息相关,注意细节
 4、在linux处理的话要下载phantomjs的linux版本,具体命令执行方式、文件路径书写方式都与windows有差异
六、 参考资料:
https://blog.csdn.net/sh_c1991/article/details/37992055
 https://blog.csdn.net/sunnyzyq/article/details/98726085














![[Vulnhub] BrainPan BOF缓冲区溢出+Man权限提升](https://img-blog.csdnimg.cn/img_convert/1f6f3d1b221242eb28cf37f9067b478e.jpeg)




