代替OCX Activex等IE浏览器插件的一种方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yhld456/article/details/88920470

目录
一、 获得ssl证书 4
二、 搭建springboot 10
三、 开发托盘程序 16
四、 使用C#创建com组件 20
五、 Springboot调用com 27
六、 使用exe4j将springboot打包成exe 31
七、 打包安装包【我用的是vs2010】 34
八、 创建调用插件服务器的web页面 45
九、 测试 49
十、 后记 51

需求:在网页上调用本地插件处理数据返回结果。(需要支持谷歌浏览器等新浏览器)
原理:网页通过https跨域请求用户本地的服务器,本地服务器调用本地插件处理后通过本地服务器返回结果。
技术:springboot,https,jacob,c#类库
工具:eclipse,maven,vs2017,exe4j,xca
一、获得ssl证书
1.使用xca生成证书,也可以购买证书
2.打开xca

3.新建xca数据库,File->new Database,选择保存路径并输入密码123456 (密码自己设置)
4.新建根证书

5.选择类型

6.输入有效期点击应用

7.输入根证书信息,点击确定

8.点击选择根证书,点击创建证书

9.选择证书类型

10.输入有效期点击确定,证书的有效期比根证书小

11.输入友好名称

12.点击修改,点击添加,选择DNS,输入域名localhost,点击应用

13.输入信息,创建私钥

14.点击确定创建成功
15.导出根证书

16.导出证书

17.输入密码,这个密码记住springboot配置用
18.客户端需要双击此证书安装受信任的根证书或者使用管理员权限用以下命令安装到根证书列表(买的证书不需要)
certutil -addstore root CAROOT.cer
二、搭建springboot
1.Eclipse安装spring插件
2.创建springboot项目

3.输入基本信息

4.选择组件点击确定

5.创建完毕后将配置文件后缀改为yml

6.配置pom.xml中依赖如下


org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-logging


	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>

	<dependency>
		<groupId>com.jacob</groupId>
		<artifactId>jacob</artifactId>
		<version>1.19</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-log4j2</artifactId>
	</dependency>

</dependencies>

7.其中jacob需要自己手动安装到本地maven库
8.Jacob下载地址https://sourceforge.net/projects/jacob-project/
9.点击
10.下载完后解压

11.Jacob是java调用com组件的一个开源库
12.在解压后的目录中执行下面命令安装到maven
mvn install:install-file -Dfile=jacob.jar -DgroupId=com.jacob -DartifactId=jacob -Dversion=1.19 -Dpackaging=jar

13.将上面生成的证书拷贝到此处

14.修改applocation.yml
server:
ssl:
key-store: classpath:localhost.p12
key-store-password: 123456
key-store-type: JKS
key-alias: localhost
port: 8444

15.配置log4j2 /resources/log4j2-spring.xml

<?xml version="1.0" encoding="UTF-8"?> D:/log [%d{yyyy-MM-dd HH:mm:ss:SSS}][%p][%t][%l]%n%m%n

16.在main方法中run as 运行
17.在谷歌浏览器访问(如果没有配置controller的话IE可能无法访问)

三、开发托盘程序
1.下载16*16像素的png图标文件到static

2.创建托盘工具类,此类的功能是在桌面托盘展示一个图标,可以右击点击退出

package com.activexocx.ui;

import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Tray {
public Tray() {
// 判断是否支持系统托盘
if (SystemTray.isSupported()) {
// 获取图片所在的URL
URL url = Tray.class.getResource("/static/20160125094105700.png");
// 实例化图像对象
ImageIcon icon = new ImageIcon(url);
// 获得Image对象
Image image = icon.getImage();
// 创建托盘图标
TrayIcon trayIcon = new TrayIcon(image);
// 为托盘添加鼠标适配器
trayIcon.addMouseListener(new MouseAdapter() {
// 鼠标事件
public void mouseClicked(MouseEvent e) {
// 判断是否双击了鼠标
if (e.getClickCount() == 2) {
JOptionPane.showMessageDialog(null, “ocx service”);
}
}
});
// 添加工具提示文本
trayIcon.setToolTip(“ocx service”);
// 创建弹出菜单
PopupMenu popupMenu = new PopupMenu();
MenuItem menuItem = new MenuItem(“Exit”);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
popupMenu.add(menuItem);

				// 为托盘图标加弹出菜弹
				trayIcon.setPopupMenu(popupMenu);
				// 获得系统托盘对象
				SystemTray systemTray = SystemTray.getSystemTray();
				try {
					// 为系统托盘加托盘图标
					systemTray.add(trayIcon);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				JOptionPane.showMessageDialog(null, "not support");
			}
}

}

3.在springboot的main方法中新建对象

4.下面的代码是初始化的,防止服务器多次启动
package com.activexocx;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.activexocx.ui.Tray;

@SpringBootApplication
public class ActivexocxApplication {

private static Logger log=LoggerFactory.getLogger(ActivexocxApplication.class);

public static void main(String[] args) {
	try {
		init();
	} catch (Exception e) {
		log.error("初始化失败,正在停止",e);
		System.exit(0);
	}
	new Tray();
	SpringApplication.run(ActivexocxApplication.class, args);
}

/**
 * 服务器进行初始化
 * @throws Exception 
 */
public static void init() throws Exception {
	//获取临时目录
	String path = System.getProperty("java.io.tmpdir");
	//创建临时文件
	final File file=new File(path,"ActivexocxApplication.stop");
	if(file.exists()) {
		file.delete();
	}
	Thread.sleep(6000L);
	file.createNewFile();
	Thread t=new Thread() {
		public void run() {
			while(true) {
				if(!file.exists()) {
					System.exit(0);
				}
				try {
					Thread.sleep(3000L);
				} catch (InterruptedException e) {
					log.error("线程异常",e);
				}
			}
		};
	};
	t.setDaemon(true);
	t.start();
}

}

5.观察托盘是否出现

四、使用C#创建com组件
1.打开vs2017,2010也可以(使用管理员权限启动)
2.创建基于.net4.0的类库项目

3.删除自带的类

4.在项目右击新建项

5.给界面拖一个文本框和一个按钮

6.双击安装编辑点击事件
7.整个类文件内容如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace activexocx
{
public partial class Form1 : Form
{
public string msg;
public Form1(string str)
{
InitializeComponent();
this.textBox1.Text = str;
System.Timers.Timer t = new System.Timers.Timer(500);//实例化Timer类,设置间隔时间为10000毫秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick);//到达时间的时候执行事件;
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件
}

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.TopMost = false;
        this.BringToFront();
        this.TopMost = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.msg = this.textBox1.Text;
        this.Close();
    }
}

}

8.其中 InitializeComponent();后面的和timer1_Tick方法为了实现界面置顶
9.创建一个类作为控件的入口

10.在vs2017上面的工具-》创建GUID,点击新建,点击复制

11.粘贴到创建的类上,并导入指定的库,类是public的

12.类中编写如下,功能是调用handle方法后启动界面,将参数str显示到文本框,用户编辑后点击按钮将修改后的字符串存到对象f的一个变量里,然后返回。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace activexocx
{
[Guid(“A559C356-D495-4070-BEAB-61B175B35201”)]
public class Main
{
public string handle(string str)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 f = new Form1(str);
Application.Run(f);
return f.msg;
}
}
}

13.在项目上右击点击属性

14.点击程序集,选中指定勾选

15.在生成中,点击上面选项

16.点击配置管理器配置如下

17.将此处勾选

18.生成成功

19.属性中默认命名空间是activexocx,类名是Main,调用时可以通过activexocx.Main调用,也可以通过Main类上的 [Guid(“A559C356-D495-4070-BEAB-61B175B35201”)]调用,推荐后者
五、Springboot调用com
1.Eclipse中新建ocx对应工具类

package cn.nb12.activex.ocx;

import com.jacob.activeX.ActiveXComponent;

/**

  • 测试控件
  • @author yh

*/
public class TestAddOcx {
private ActiveXComponent com=null;
public TestAddOcx() {
try {
com=new ActiveXComponent(“CLSID:9A2E3D0E-C084-43FC-8153-3D0C90A51B08”);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
 * 调用控件方法
 * @return
 */
public String add() {
	try {
		return com.invoke("add").getString();
	} catch (Exception e) {
		return e.getMessage();
	}
	
}

}

2.新建测试controller

package com.activexocx.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.activexocx.ocx.TestOcx;

@RestController
public class TestController {

private Logger log=LoggerFactory.getLogger(this.getClass());

/**
 * 获得版本
 * @return
 */
@RequestMapping("/getVersion.do")
public Map<String,String> version() {
	Map<String,String> result=new HashMap<String,String>();
	result.put("version", "1");
	return result;
}

@RequestMapping("/doHandle.do")
public Map<String,String> handle(String msg){
	Map<String,String> result=new HashMap<String,String>();
	try {
		String resultMsg = new TestOcx().handle(msg);
		result.put("status", "success");
		result.put("msg", resultMsg);
	} catch (Exception e) {
		log.error("错误",e);
		result.put("status", "error");
		result.put("msg", e.getMessage());
	}
	return result;
}

}

3.如果前面vs2017中选择的是x86则将jacob-1.19-x86.dll复制到32位jdk的bin目录,将这个jdk设置为此项目的jdk。如果是64位的则将jacob-1.19-x64.dll复制到64位jdk的bin目录,将这个jdk设置为此项目的jdk。
4.运行项目,在谷歌浏览器输入https://localhost:8444/doHandle.do

5.在弹出的框中输入文字点击按钮

6.页面展示结果

7.Springboot设置cors跨域(到时候记得设置只能通过localhost访问)

六、使用exe4j将springboot打包成exe
1.在eclipse项目上右击选择run as maven build

2.之后将打包好的jar复制到一个目录,同时将32位的jre也复制到这个目录

3.Jre的bin目录要有jacob-1.19-x86.dll文件
4.打开exe4j
5.选择jar转exe

6.输入应用名称和输出路径

7.选择GUI并输入导出的文件名,推荐英文

8.默认32位不用管

9.选择springboot导出的jar

10.选择springboot的main方法

11.设置最大最小jre版本
12.删除默认的查找jre的方式,这三个都删除

13.添加jre路径点击确定,使用相对路径

14.点击生成

15.生成后双击是否启动正常
16.最后得到这两个东西

七、打包安装包【我用的是vs2010】
1.创建安装项目

2.复制下面文件

3.在这儿粘贴

4.选中添加的dll文件

5.在属性栏修改为

6.选择项目,点击注册表

7.在下面图片上的位置右击点击导入

8.将下面复制到一个文件1.reg,导入到上方(这是注册url协议,可以通过url启动exe程序)
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\activexocx]
“Language”=“1”
“URL Protocol”=""
“EditFlags”=hex:02,00,00,00
@=“URL:activexocx Protocol”

[HKEY_CLASSES_ROOT\activexocx\DefaultIcon]
@=""[TARGETDIR]activexocx.exe",0"

[HKEY_CLASSES_ROOT\activexocx\shell]

[HKEY_CLASSES_ROOT\activexocx\shell\open]

[HKEY_CLASSES_ROOT\activexocx\shell\open\command]
@=""[TARGETDIR]activexocx.exe" “%1"”

9.点击项目在下面的属性修改这个

10.点击依赖项

11.选择左边的

12.在右下角选择.net4.0

13.在项目右击点击属性

14.项目上右击点击重新生成
15.生成之后将根证书拷贝

16.编写批处理

17.创建自解压格式打包

18.最后生成一个自解压格式的文件

八、创建调用插件服务器的web页面
1.新建一个html页面

2.拷贝上面的自解压格式文件Release.exe到与上面那个html文件同级目录
3.新建一个虚拟机,访问上面那个html(html最好部署到服务器中)
九、测试
1.访问服务器上的html页面

2.选择取消
3.运行

4.安装完成

5.重新点击初始化(如果第一次初始化时要同意什么直接同意,然后初始化失败后重新刷新点击初始化,不要重复安装插件)

6.输入文字点击测试会弹出程序框

7.在程序框中修改字符串并点击按钮,新的字符串回写到网页的文本框

十、后记
1.Springboot可以用C#或者C++等程序代替只要实现https跨域访问就行。

因图片较多上传不方便,直接放在word文件里面
https://download.csdn.net/download/yhld456/11072949

猜你喜欢

转载自blog.csdn.net/yhld456/article/details/88920470