java javafx_基于java(javafx)的软件更新

本文介绍了如何使用JavaFX为C/S程序实现自动更新功能。当检测到新版本时,程序提示用户更新,并在后台下载最新客户端到指定目录C:\f1,生成批处理文件并创建桌面快捷方式供用户执行更新。
摘要由CSDN通过智能技术生成

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

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

解决办法:

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

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值