java 将网页转为jpg

package com.job36.util.pdf;

/**
* 将网址转化为JPG
* @author wangzi6hao
*
*/
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class UrlToJpg extends JFrame {

    private String url;
    private File file;

    /**
    * 将打开的网址转换成jpg图片<br>
    * 不支持div层网站
    *
    * @param url
    *            网站地址
    * @param file
    *            图片存放地址
    * @throws Exception
    */
    public UrlToJpg(String url, File file) {
        this.url = url;
        this.file = file;
    }

    /**
    * 根据构造函数内容将url抓屏后的页面生成图片放在file路径对应位置
    *
    * @throws Exception
    */
    public String producePic() throws Exception {
        JEditorPane editorPane = new JEditorPane();
        editorPane.setEditable(false);
        editorPane.setPage(url);
        JScrollPane jsp = new JScrollPane(editorPane);
        getContentPane().add(jsp);
        this.setLocation(0, 0);

        Thread.sleep(5 * 1000);

        setSize(10000, 10000);
        pack();
        BufferedImage image = new BufferedImage(editorPane.getWidth(),
                editorPane.getHeight(), BufferedImage.TYPE_USHORT_565_RGB);
        Graphics2D graphics2D = image.createGraphics();
        editorPane.paint(graphics2D);
        ImageIO.write(image, "jpg", file);
        dispose();
        return file.getName();
    }

    public static void main(String[] args) throws Exception {
        String url = "http://192.168.3.160/crm/contractAction.do?method=previewContract&contractId=LT04011405&printJob=userContractList";
        String filePath = System.getProperties().get("user.dir")
                + "\\webapps\\protocol_html\\file.jpg";
        //new UrlToJpg(url, new File(filePath));
        System.out.println("ok");
    }
}
 

对div类型的转换并不是很好.
对table的转换还不错.
对网页内容的要求比较高.
如果是抓取自己的网页,改一下应该还可以用.对于高要求的,可能要改一下了.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值