|
1、最直接的方式——使用Robot
方法详解:该方法利用Robat提供的强大桌面操作能力,硬性调用浏览器打开指定网页,并将网页信息保存到本地。
优势:简单易用,不需要任何第三方插件。
缺点:不能同时处理大量数据,技术含量过低,属于应急型技巧。
实现方法:使用如下代码即可。
- public static void main(String[] args) throws MalformedURLException,
- IOException, URISyntaxException, AWTException {
- //此方法仅适用于JdK1.6及以上版本
- Desktop.getDesktop().browse(
- new URL("http://google.com/intl/en/").toURI());
- Robot robot = new Robot();
- robot.delay(10000);
- Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
- int width = (int) d.getWidth();
- int height = (int) d.getHeight();
- //最大化浏览器
- robot.keyRelease(KeyEvent.VK_F11);
- robot.delay(2000);
- Image image = robot.createScreenCapture(new Rectangle(0, 0, width,
- height));
- BufferedImage bi = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- Graphics g = bi.createGraphics();
- g.drawImage(image, 0, 0, width, height, null);
- //保存图片
- ImageIO.write(bi, "jpg", new File("google.jpg"));
- }
复制代码
|
|