六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 78|回复: 0

url---〉image(1)

[复制链接]

升级  12%

16

主题

16

主题

16

主题

秀才

Rank: 2

积分
68
 楼主| 发表于 2013-1-29 07:48:12 | 显示全部楼层 |阅读模式
1、协议支持简单
2、不支持css
3、http协议需要改进

import java.io.File;import java.io.IOException;import java.net.URL;import java.awt.*;import java.awt.image.*;import javax.swing.*;import javax.swing.text.*;import javax.swing.text.html.*;public class WebCapture {    public static void main(String[] args) throws IOException {    args = new String[2];    args[0] = "file:D:\\sly\\hp\\js\\index2.html";    args[1] = "c:\\file.bmp";        if (args.length < 2) {            System.err.println(                    "Usage: java " +                    WebCapture.class.getName() +                    " url file");            System.exit(1);        }        new WebCapture().go(            new URL(args[0]),            new File(args[1]));    }    public void go(URL url, File file) throws IOException {        JEditorPane editor = new JEditorPane();        EditorKit ek = new HTMLEditorKit() {            public ViewFactory getViewFactory() {                return new HTMLEditorKit.HTMLFactory() {                    public View create(Element element) {                        View view = super.create(element);                        if (view instanceof ImageView) {                            ((ImageView)view).setLoadsSynchronously(true);                        }                        return view;                    }                };            }            public Document createDefaultDocument() {                AbstractDocument document =                    (AbstractDocument)super.createDefaultDocument();                document.setAsynchronousLoadPriority(-1);                return document;            }        };        editor.setEditorKit(ek);        editor.setPage(url);        editor.setSize(editor.getPreferredSize());        javax.imageio.ImageIO.write(            getImage(editor),            getFormatName(file.getName()),            file);    }    private RenderedImage getImage(Component component) {        Dimension dim = component.getSize();        BufferedImage bi = new BufferedImage(            dim.width, dim.height, BufferedImage.TYPE_INT_RGB);        Graphics2D g2 = bi.createGraphics();        component.paint(g2);        g2.dispose();        return bi;    }    private String getFormatName(String fileName) {        int n = fileName.lastIndexOf(".");        if (n == -1) {            return null;        }        return fileName.substring(n+1);    }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

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