用javaFx开发的C/S程序,客户端需要更新,需要写个程序,如果有新版本需要提示用户更新。

背景:用javaFx开发的C/S程序,客户端需要更新,需要写个程序,如果有新版本需要提示用户更新。

需求:如果有新版本,提示客户需要更新,客户根据提示,进入到更新页面,可以下载最新客户端(jnlp文件)。最新的客户端将下载到C:\\f1(硬性规定,不让客户选择)目录下,并生成一个批处理文件(bat)并在桌面创建这个批处理文件的快捷方式,客户直接执行这个批处理文件即进行更新.

解决办法:

    有新版本提示客户更新比较好实现。每次发布版本都会生成一个版本号,客户每次登录客户端,客户端往服务器端发送版本号,如果和服务器端存储的版本号一致,则没有要更新的版本,反之则提示客户需要更新。至于怎么推送消息,取决于各位。楼主用的是jms.

    java下载也是大家经常用的,不赘述。生成bat文件也下载也一样,就是IO流的操作。有些人可能脚本文件可能不太会写,其实很简单,网上一搜,很多都是现成的。而且就是和在dos命令一样。难点在于创建这个bat文件的快捷方式。借助于第三方jar包和一个dll文件。jshortcut.dll,jshortcut.jar。jshortcut.dll文件需要放到和src同一级目录上。

    好了,不多说,下面贴代码,注释写的都比较清楚。

  1. package com.platform.ui.update;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedWriter;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.FileWriter;  
  9. import java.io.IOException;  
  10. import java.io.InputStream;  
  11.   
  12. import javax.swing.filechooser.FileSystemView;  
  13.   
  14. import net.jimmc.jshortcut.JShellLink;  
  15.   
  16. import javafx.fxml.FXML;  
  17. import javafx.scene.control.Button;  
  18. import javafx.scene.layout.AnchorPane;  
  19.   
  20. public class DownloadFileController extends AnchorPane {  
  21.     @FXML  
  22.     private Button download;  
  23.   
  24.     @FXML  
  25.     void downloadFile() {  
  26.   
  27.         // 获取资源路径  
  28.         String tempResourcePath = this.getClass().getClassLoader()  
  29.                 .getResource("").getPath();  
  30.         String resourcePath = tempResourcePath.substring(1,  
  31.                 tempResourcePath.indexOf("classes"))  
  32.                 + "resource";  
  33.   
  34.         String targetPath = "C:\\f1";  
  35.   
  36.         File targetFile = new File(targetPath);  
  37.         if (!targetFile.exists()) {  
  38.             targetFile.mkdirs();  
  39.         }  
  40.   
  41.         File[] files = new File(resourcePath).listFiles();  
  42.   
  43.         for (File file : files) {  
  44.             // File resourceFile = new File(resourcePath);  
  45.   
  46.             // 以流的形式下载文件。  
  47.             InputStream fis;  
  48.             try {  
  49.                 fis = new BufferedInputStream(new FileInputStream(  
  50.                         file.getAbsolutePath()));  
  51.                 byte[] buffer = new byte[fis.available()];  
  52.                 fis.read(buffer);  
  53.                 fis.close();  
  54.                 FileOutputStream out = new FileOutputStream(targetFile + "\\"  
  55.                         + file.getName());  
  56.                 out.write(buffer);  
  57.                 out.flush();  
  58.                 out.close();  
  59.             } catch (Exception e) {  
  60.                 // TODO Auto-generated catch block  
  61.                 e.printStackTrace();  
  62.             }  
  63.         }  
  64.         // 创建写入的目标文件  
  65.         String batPath = "C:\\f1\\run.bat";  
  66.         File file = new File(batPath);  
  67.         if (!file.exists()) {  
  68.             try {  
  69.                 file.createNewFile();  
  70.             } catch (IOException e) {  
  71.                 e.printStackTrace();  
  72.             }  
  73.         }  
  74.         // 写出流  
  75.         BufferedWriter output;  
  76.         try {  
  77.             output = new BufferedWriter(new FileWriter(file));  
  78.             output.write("cd C:\\f1");  
  79.             output.write("\r\n");  
  80.             output.write("javaws yk_platform_client.jnlp");  
  81.             output.close();  
  82.         } catch (IOException e1) {  
  83.             // TODO Auto-generated catch block  
  84.             e1.printStackTrace();  
  85.         }  
  86.   
  87.         // 在桌面创建run.bat快捷方式  
  88.         FileSystemView fsv = FileSystemView.getFileSystemView();  
  89.         String writeFolderPath = fsv.getHomeDirectory().toString() + "\\"// 这便是读取桌面路径的方法了  
  90.         String jarFileName = "C:\\f1\\run.bat";// 建立快捷方式后鼠标放到上面的时候现实的文件所存位置  
  91.         // create lnk file  
  92.         JShellLink link = new JShellLink();  
  93.         link.setFolder(writeFolderPath); // 创建的快捷方式所存在的位置,路径要真实路径,放到快速启动栏里面  
  94.         link.setName("豪诺ERP更新文件"); // 快捷方式的名称  
  95.         link.setIconLocation("C:\\f1\\erp.ico");// 图片位置  
  96.         link.setPath(jarFileName);  
  97.         link.setArguments("");// 设置执行参数  
  98.         link.save();  
  99.   
  100.         System.out.println("执行完毕!");  
  101.     }  
  102.   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值