web 调用本地服务 --> 生成注册文件
实现web 调用本地服务,开启本地服务的功能
分如下几部:
1:生成ref文件,上传到服务器;
//实体类中的相关字段
private String name; // 启动程序名称
private String sysType; //类型
private String regeditName; //注册表名称
private String sysHref; //应用程序地址
private String regeditPath; //注册表路径
private String iconPath; //图标路径
//这是controller层代码
@RequestMapping(value = "save")
public String save(SysInfo sysInfo, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request ) {
if (!beanValidator(model, sysInfo)) {
return form(sysInfo, model);
}
if (sysInfo.getSysType().equals("0") && !sysInfo.getSysHref().startsWith("http://")) {
sysInfo.setSysHref("http://" + sysInfo.getSysHref());
}
String zp = request.getSession().getServletContext().getRealPath("/") + "uploadfile\\regedit\\";
sysInfoService.save(sysInfo,zp);
addMessage(redirectAttributes, "保存生成启动程序路径成功");
return "redirect:" + Global.getAdminPath() + "/sso/sysInfo/?repage";
}
/**
* 生成注册表文件
*/
@Transactional(readOnly = false)
public void save(SysInfo systemInfo, String zp) {
String regeditName = null; // 注册表名称
String path = null; // 系统安装路径
// 本地系统类型 写出reg文件上传服务器
if (systemInfo.getSysType().equals("1")) {
if (systemInfo.getRegeditName() != null || !systemInfo.equals("")) {
regeditName = systemInfo.getRegeditName();
}
if (systemInfo.getSysHref() != null || !systemInfo.equals("")) {
path = systemInfo.getSysHref().replaceAll("\\\\", "\\\\\\\\");
}
// 1、创建源
File dest = new File(zp + regeditName + ".reg");
File fileParent = dest.getParentFile();
if (!fileParent.exists()) {
fileParent.mkdirs();
}
try {
dest.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
// 2、选择流,regeditName :注册表路径
Writer writer = null;
try {
writer = new FileWriter(dest);
writer.append("Windows Registry Editor Version 5.00\r\n\r\n")
.append("[HKEY_CLASSES_ROOT\\" + regeditName + "]\r\n")
.append("@=\"" + regeditName + "Protocol\"\r\n").append("\"URL Protocol\"=\"\"\r\n\r\n")
.append("[HKEY_CLASSES_ROOT\\" + regeditName + "\\DefaultIcon]\r\n")
.append("@=\"" + path + "\"\r\n\r\n")
.append("[HKEY_CLASSES_ROOT\\" + regeditName + "\\shell]\r\n\r\n")
.append("[HKEY_CLASSES_ROOT\\" + regeditName + "\\shell\\open]\r\n\r\n")
.append("[HKEY_CLASSES_ROOT\\" + regeditName + "\\shell\\open\\command]\r\n")
.append("@=\"" + path + "\"");
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4、释放资源
try {
if (null != writer) {
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
systemInfo.setRegeditPath(zp + regeditName + ".reg");
}
super.save(systemInfo);
}
//下载注册表reg文件
/**
* 下载.reg文件
*
* @param sysInfoId
* @throws IOException
*/
public void download(String sysInfoId, HttpServletRequest request, HttpServletResponse response)
throws IOException {
OutputStream out = null;
InputStream input = null;
try {
SysInfo sysInfo = super.get(sysInfoId);
if (sysInfo != null && sysInfo.getRegeditPath() != null) {
// 获取下载路径
String regeditPath = sysInfo.getRegeditPath();
// regeditPath是指欲下载的文件的路径。
File file = new File(regeditPath);
// 取得文件名
String filename = file.getName();
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition",
"attachment;filename=" + new String(filename.getBytes(), "utf-8"));
input = new BufferedInputStream(new FileInputStream(regeditPath));
int len = 0;
byte[] buffer = new byte[1024];
out = response.getOutputStream();
while ((len = input.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
out.flush();
out.close();
}
}
}
//通过调用注册表文件,实现启动本地服务;
至此,生成注册表文件完成;
之前我一直想用cmd 命令的方式,实现本地应用程序的调用,如下:
// 获取路径
// String path = sysInfo.getPath();
// String command = "D:\\Develop\\nox\\nox_setup_v6.6.1.1_full.exe";
// String command = path;
// Runtime.getRuntime().exec(command);
//这种方式能调动本地应用程序,但是,web是要部署到服务器的呀,用户是不固定的呀,所以,需要使用注册表的方式实现启动应用程序;
posted on 2020-11-09 11:13 Spring-Boot-Cloud 阅读(185) 评论(0) 收藏 举报
浙公网安备 33010602011771号