defrag_sly 发表于 2013-1-29 07:48:12

url---〉image(1)

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;    args = "file:D:\\sly\\hp\\js\\index2.html";    args = "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),            new File(args));    }    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);    }}
页: [1]
查看完整版本: url---〉image(1)