JAVA的web截图功能实现

来源:互联网 发布:德力西选型软件 编辑:程序博客网 时间:2024/06/14 13:53

需求:在网页上点击按钮,打开截图程序。截图后,弹出页面,填写相关文字性的内容,将所截到的图片以邮件形式发送出去。

不让采购小公司的截图插件,就只能自己搞了。

1、开发客户端截图软件(基于JPanel),接收参数。将截到的图片上传到参数指定的服务器上,并使用本地IE打开邮件发送的页面。

2、在页面中嵌入Applet程序,点击按钮触发程序中的事件,从而打开截图程序。

关键代码:

1、截图程序(代码太多了,没时间搞,先只贴main)

/* * 参数:截图后跳转的URL */public static void main(String[] args) {TestFrame tf=new TestFrame();tf.jtbtnClick();tf.setAlwaysOnTop(true);//顶层显示boolean isCaptured = tf.saveImg();//是否截图if(isCaptured){//上传图片try {ImageUpload.uploadImage(tf,SCUtil.processUrl(args[0]));} catch (Exception ee) {ee.printStackTrace();}while(!tf.getUploaded()){try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}//延迟半秒打开网页try {Thread.sleep(500);} catch (InterruptedException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}//打开本地网页try {Runtime.getRuntime().exec( "\""+System.getenv("ProgramFiles")+"\\Internet Explorer\\IEXPLORE.EXE\"  "+args[0] );//C:\\Program Files\\ScreenShotClient\\temp\\run.html} catch (IOException e) {e.printStackTrace();System.exit(0);}}tf.dispose();System.exit(0);}


2、Applet

import java.applet.Applet;import java.security.Permission;import javax.swing.SwingUtilities;public class OpenExeApplet extends Applet {private class DefaultSecurityManager extends SecurityManager{@Overridepublic void checkPermission(Permission perm) {}@Overridepublic void checkPermission(Permission perm, Object context) {}}public void init() {System.setSecurityManager(new DefaultSecurityManager());}public String openExe(){String path = getParameter("path");if(null == path){path = getParameter("src");}try{Runtime rt = Runtime.getRuntime();String[] cmd = path.split("|&@");rt.exec(cmd);}catch(Exception e){e.printStackTrace();return e.getMessage();}return "success";}

3、页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = "C:/Program Files/ScreenShotClient/screen.exe|&@http://192.25.102.140:8080/BI"; %><html>  <head>    <title>打开截图软件</title>    <script type="text/javascript">    function openApp(){    var str = document.applets[0].openExe();    alert(str);    }    </script>  </head>    <body>    <input type="button" value="open" onclick="openApp()" />    <object classid="clsid:CAFEEFAC-0015-0000-0007-ABCDEFFEDCBA" id="qApplet" name="qApplet" style="top:0;left:0;position:absolute;" width="1" height="1"  codebase="http://192.25.102.128:8080/LocalOpen/download/jre-6u17-windows-i586.exe" ><param name="java_code" value="OpenExeApplet.class"><param name="java_archive" value="/LocalOpen/download/openExe.jar"><param name="type" value="application/x-java-applet;version=1.6"><param name="scriptable" value="true"><PARAM name="MAYSCRIPT" value="true"><param name="boxbgcolor" value="#ffffff"><param name="src" value="<%=path %>"><embed type="application/x-java-applet;version=1.6" id="qApplet" name="qApplet" width="1" height="1" pluginspage="http://java.sun.com/products/plugin/" java_code="OpenExeApplet.class" java_archive="/LocalOpen/download/openExe.jar" src="<%=path %>"scriptable="true" MAYSCRIPT="true"></embed></object>              </body></html>


 4、注册(获取applet操作本地文件的权限)

keytool -genkey -keystore q.store -alias q

keytool -export -keystore q.store -alias q -file q.cert

jarsigner -keystore q.store openExe.jar q

 

效果:

点击交办,则使用javamail发送邮件。

不足:只适用于IE;第一次使用程序运行速度较慢;胖客户端,需要用户安装程序。