/**
* 获取电子地图并保存为图片
*/
@RequestMapping("/getDzdtTp")
public R getDzdtTp(String params){
System.setProperty("java.awt.headless", "false");
if (!java.awt.Desktop.isDesktopSupported()) {
System.err.println("Desktop is not supported (fatal)");
System.exit(1);
}
Desktop desktop = Desktop.getDesktop();
if (!desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
System.err.println("Desktop doesn't support the browse action (fatal)");
System.exit(1);
}
try {
String getQyhxurl = customProperties.getWeburl()+params;//电子地图访问页面
URI uri = URI.create(getQyhxurl);
desktop.browse(uri);
Thread.sleep(8000); // 8秒足以加载任意页面
Robot robot = new Robot();
// Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize() );
Rectangle rectangle = new Rectangle(300, 90, 1366, 900);
BufferedImage image = robot.createScreenCapture(rectangle);
String url = EBaseInfoController.class.getClassLoader().getResource("/").getPath()+"static/dzdt/";
File outputfile = new File(url);
if(!outputfile.exists()){//如果文件夹不存在
outputfile.mkdir();//创建文件夹
}
ImageIO.write(image, "jpg", new File(url+params+".jpg"));
} catch (IOException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return R.ok();
}
效果图: