背景:用javaFx开发的C/S程序,客户端须要更新,须要写个程序,若是有新版本须要提示用户更新。java
需求:若是有新版本,提示客户须要更新,客户根据提示,进入到更新页面,能够下载最新客户端(jnlp文件)。最新的客户端将下载到C:\\f1(硬性规定,不让客户选择)目录下,并生成一个批处理文件(bat)并在桌面建立这个批处理文件的快捷方式,客户直接执行这个批处理文件即进行更新.
服务器
解决办法:ui
有新版本提示客户更新比较好实现。每次发布版本都会生成一个版本号,客户每次登陆客户端,客户端往服务器端发送版本号,若是和服务器端存储的版本号一致,则没有要更新的版本,反之则提示客户须要更新。至于怎么推送消息,取决于各位。楼主用的是jms.this
java下载也是你们常常用的,不赘述。生成bat文件也下载也同样,就是IO流的操做。有些人可能脚本文件可能不太会写,其实很简单,网上一搜,不少都是现成的。并且就是和在dos命令同样。难点在于建立这个bat文件的快捷方式。借助于第三方jar包和一个dll文件。jshortcut.dll,jshortcut.jar。jshortcut.dll文件须要放到和src同一级目录上。spa
好了,很少说,下面贴代码,注释写的都比较清楚。
code
package com.platform.ui.update; import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import javax.swing.filechooser.FileSystemView; import net.jimmc.jshortcut.JShellLink; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; public class DownloadFileController extends AnchorPane { @FXML private Button download; @FXML void downloadFile() { // 获取资源路径 String tempResourcePath = this.getClass().getClassLoader() .getResource("").getPath(); String resourcePath = tempResourcePath.substring(1, tempResourcePath.indexOf("classes")) + "resource"; String targetPath = "C:\\f1"; File targetFile = new File(targetPath); if (!targetFile.exists()) { targetFile.mkdirs(); } File[] files = new File(resourcePath).listFiles(); for (File file : files) { // File resourceFile = new File(resourcePath); // 以流的形式下载文件。 InputStream fis; try { fis = new BufferedInputStream(new FileInputStream( file.getAbsolutePath())); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); FileOutputStream out = new FileOutputStream(targetFile + "\\" + file.getName()); out.write(buffer); out.flush(); out.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 建立写入的目标文件 String batPath = "C:\\f1\\run.bat"; File file = new File(batPath); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } // 写出流 BufferedWriter output; try { output = new BufferedWriter(new FileWriter(file)); output.write("cd C:\\f1"); output.write("\r\n"); output.write("javaws yk_platform_client.jnlp"); output.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // 在桌面建立run.bat快捷方式 FileSystemView fsv = FileSystemView.getFileSystemView(); String writeFolderPath = fsv.getHomeDirectory().toString() + "\\"; // 这即是读取桌面路径的方法了 String jarFileName = "C:\\f1\\run.bat";// 创建快捷方式后鼠标放到上面的时候现实的文件所存位置 // create lnk file JShellLink link = new JShellLink(); link.setFolder(writeFolderPath); // 建立的快捷方式所存在的位置,路径要真实路径,放到快速启动栏里面 link.setName("豪诺ERP更新文件"); // 快捷方式的名称 link.setIconLocation("C:\\f1\\erp.ico");// 图片位置 link.setPath(jarFileName); link.setArguments("");// 设置执行参数 link.save(); System.out.println("执行完毕!"); } }
有什么须要指正或者不明白的地方,欢迎交流。QQ:70747053
orm