【java使用蓝奏云实现程序在线更新】

蓝奏云上传版本.dll与程序.exe

此处更新通过.dll后缀校验版本,下载.exe文件替换本地文件

请求分享的文件夹连接,校验版本号

try {
            String data = syncClient.getVersionUrl(Main.url);
            String ajax = data.substring(data.indexOf("提交中..."), data.indexOf("dataType : 'json'"));
            String pattern = "var\\s+\\w+\\s*=\\s*'([^']+)'";
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(data);
            List<String> matches = new ArrayList<>();
            while (m.find()) {
                String match = m.group(1); // 获取捕获组中的内容
                matches.add(match);
            }
            String t = matches.get(1);
            String k = matches.get(2);
            ajax = ajax.replaceAll("\n","");
            ajax = ajax.replaceAll("\t","");
            String[] strings = ajax.split(",");
            String fid = strings[3].replaceAll("'fid':","").replaceAll("'","");
            String uid =  strings[4].replaceAll("'uid':","").replaceAll("'","");
            JSONObject params = new JSONObject();
            params.put("lx", 2);
            params.put("fid", fid);
            params.put("uid", uid);
            params.put("pg", 1);
            params.put("rep", 0);
            params.put("t", t);
            params.put("k", k);
            params.put("up", 1);
            params.put("ls", 1);
            params.put("pwd", pwd);
            String version = syncClient.postVersionUrl(params);
            JSONObject res = JSON.parseObject(version, JSONObject.class);
            JSONArray array = (JSONArray) res.get("text");
            HashMap<String, String> map = new HashMap<>();
            for (Object o : array) {
                JSONObject  obj = (JSONObject) o;
                if(obj.get("icon").toString().equals("dll")){
                    map.put("version",obj.get("name_all").toString());
                }
                if(obj.get("icon").toString().equals("exe")){
                    map.put("exe",obj.get("id").toString());
                }
            }
            if(!Main.version.equals(map.getOrDefault("version",version).replace(".dll",""))){
                Image icon = new Image("/ico.png");
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("版本更新");
                alert.setHeaderText(null);
                alert.setContentText("\n您的版本过低,可能影响正常操作,是否进行更新?");
                alert.getButtonTypes().setAll(ButtonType.YES,ButtonType.NO);
                // 设置对话框的图标
                Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
                stage.getIcons().add(icon);
                // 显示对话框
                Optional<ButtonType> result = alert.showAndWait();
                // 处理用户点击按钮的结果
                result.ifPresent(buttonType -> {
                    if (buttonType == ButtonType.YES) {
                        //下载
                        this.update(map.get("exe"));
                    }
                });
            } else if (isUpdate) {
                textQueue.add("当前已是最新版本\n");
            }
        } catch (Exception e) {
            textQueue.add("检查更新失败:"+e.getMessage()+ "\n");
            logger.error("错误:",e);
        }
    }

获取直链,下载最新文件

 public void update(String id) {
        try {
            String jarPath = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
            jarPath = jarPath.replace("%20", " ");
            jarPath = jarPath.replace("%e5%90%8c%e6%ad%a5%e5%8a%a9%e6%89%8b", "同步助手");
            String jarDir = new File(jarPath).getParent();

            String data = syncClient.getDownloadedUrl("https://wwqh.lanzouw.com/tp/"+id);
            String pattern = "var\\s+\\w+\\s*=\\s*'([^']+)'";
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(data);
            List<String> matches = new ArrayList<>();
            while (m.find()) {
                String match = m.group(1); // 获取捕获组中的内容
                matches.add(match);
            }
            String u1 = matches.get(2);
            String u2 = matches.get(3);
            textQueue.add("更新提示:正在下载最新资源..."+ "\n");
            syncClient.getDownloadedFile(u1+u2,jarDir+"\\temp.exe");
            textQueue.add("更新提示:下载完成..."+ "\n");
            Thread.sleep(1000);
            // 更改为你本地 EXE 程序的路径
            String exePath = jarDir+"\\update.exe";
            //ProcessBuilder pb = new ProcessBuilder(exePath);
            ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", exePath);
            pb.directory(new File(jarDir)); // 设置程序运行的工作目录
            // 启动程序
            Process process = pb.start();
            this.handleClose();
            // 等待程序结束
            //process.waitFor();
        } catch (Exception e) {
            textQueue.add("更新失败:"+e.getMessage()+ "\n");
            logger.error("错误:",e);
        }
    }

请求代码

  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值