开发者

Tomcat/JSP: opening a file on the server and taking a screenshot

I'm setting up a web interface for a testing server. Some of the tests involve opening a specific file, capturing the screen, and then saving the resulting image. I use Runtime.getRuntime().exec() and Robot for this.

If I access the server through remote d开发者_开发百科esktop and run the tests, everything works out fine; the file is opened and the screenshot is saved. If I try to run the same tests through a JSP file with Tomcat, no file is opened (though, the process can be found in task manager) and the image is completely black.

Here is a simple example that would result in a black/blank image (from a JSP file):

/* initializing stuff goes here */
Runtime.getRuntime().exec("C:\\Windows\\System32\\notepad.exe");
BufferedImage screenCap = robot.createScreenCapture(rect);
File savedImage = new File("C:\\test.png");
ImageIO.write(screenCap, "png", savedImage);

Is what I'm trying to do possible?


On MS Windows: If tomcat is run as service it may miss permissions to interact with the desktop and may therefore not be able to start programs that open windows.

There is a tick in the service property dialog.


JSP has to be served by a webserver and browsed by a webbrowser. Use Desktop#browse() on http://localhost to browse it and take screenshot only after a while (5 seconds?), the browser of course needs time to be started and load the page fully.

Kickoff example:

Desktop.getDesktop().browse(new URI("http://localhost:8080/page.jsp"));
Thread.sleep(5000);
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("/screen.png"));

You however have to take into account that this job needs to be queued to avoid that different screen capture requests are interfering each other. Also note that this wouldn't work in a headless server (a server machine without a monitor).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜