From f02d7d38e8c7b0d88fb2f18f1224adc110a30b1f Mon Sep 17 00:00:00 2001 From: huoyo <1729913829@qq.com> Date: Wed, 28 Sep 2022 19:46:05 +0800 Subject: [PATCH] add hot update --- .../kotime/controller/KoTimeController.java | 32 +++++++--- src/main/resources/kotime-en.html | 59 +++++++++++++++++ src/main/resources/kotime.html | 63 +++++++++++++++++++ 3 files changed, 146 insertions(+), 8 deletions(-) diff --git a/src/main/java/cn/langpy/kotime/controller/KoTimeController.java b/src/main/java/cn/langpy/kotime/controller/KoTimeController.java index d11b0f8..a4ca199 100644 --- a/src/main/java/cn/langpy/kotime/controller/KoTimeController.java +++ b/src/main/java/cn/langpy/kotime/controller/KoTimeController.java @@ -246,13 +246,19 @@ public class KoTimeController { @PostMapping("/updateClass") @ResponseBody @Auth - public Map updateClass(@RequestParam("classFile") MultipartFile classFile,String className) throws ClassNotFoundException { + public Map updateClass(@RequestParam("classFile") MultipartFile classFile,String className) { Map map = new HashMap(); if (classFile==null || classFile.isEmpty()) { map.put("state", 0); map.put("message", "文件不能为空"); return map; } + if (!StringUtils.hasText(className)) { + map.put("state", 0); + map.put("message", "文类名不能为空"); + return map; + } + className = className.trim(); File file = null; try { String originalFilename = classFile.getOriginalFilename(); @@ -262,33 +268,43 @@ public class KoTimeController { return map; } String[] filename = originalFilename.split("\\."); + String substring = className.substring(className.lastIndexOf(".") + 1); + if (!substring.equals(filename[0])) { + map.put("state", 0); + map.put("message", "请确认类名是否正确"); + return map; + } file = uploadFile(classFile.getBytes(),filename[0]); } catch (IOException e) { - e.printStackTrace(); + log.severe("Error class file!"); + map.put("state", 0); + map.put("message", "无法解析文件"); + return map; } File jar = null; - if (StringUtils.hasText(agentPath)) { + if (!StringUtils.hasText(agentPath)) { jar = ClassUtil.createJar(); agentPath = jar.getAbsolutePath(); } - ClassUtil.updateClass(agentPath,className.trim(),file.getAbsolutePath()); - file.delete(); + ClassUtil.updateClass(agentPath,className,file.getAbsolutePath()); + file.deleteOnExit(); if (jar!=null) { - jar.delete(); + jar.deleteOnExit(); } map.put("state", 1); map.put("message", "更新成功"); return map; } - public static File uploadFile(byte[] file,String fileName) throws IOException { + + + private static File uploadFile(byte[] file,String fileName) throws IOException { FileOutputStream out = null; try { File targetFile = File.createTempFile(fileName, ".class", new File(System.getProperty("java.io.tmpdir"))); out = new FileOutputStream(targetFile.getAbsolutePath()); out.write(file); out.flush(); - out.close(); return targetFile; } catch (Exception e) { log.severe("" + e); diff --git a/src/main/resources/kotime-en.html b/src/main/resources/kotime-en.html index 0f0c504..bb3e6cc 100644 --- a/src/main/resources/kotime-en.html +++ b/src/main/resources/kotime-en.html @@ -16,6 +16,7 @@ var globalNeedLogin = globalNeedLoginValue; var globalToken = sessionStorage.getItem("kotimeToken") $(document).ready(function () { + document.querySelector("#classForm").action='contextPath/koTime/updateClass?token='+globalToken let globalIsLogin = false; $.ajaxSettings.async = false; $.get('contextPath/koTime/isLogin?token='+globalToken, function (data) { @@ -333,6 +334,47 @@ $('#searchText').val(''); } } + + function updateClass(){ + // document.querySelector("#classForm").submit(); + var formData = new FormData(); + var file = document.querySelector('#classFile').files[0]; + if (file==null || file==undefined) { + UIkit.notification("Null file",{}); + return; + } + var className = document.querySelector("#className").value + if (className==null || className==undefined || className.length<2) { + UIkit.notification("Null class name",{}); + return; + } + formData.append('classFile', file); + formData.append('className', className ); + + $.ajax({ + url: document.querySelector("#classForm").action, + type: 'POST', + cache: false, + data: formData, + processData: false, + contentType: false, + dataType: "json", + success: function (res) { + if (res['state']==1) { + UIkit.notification.closeAll(); + UIkit.notification("Success",{}); + }else { + UIkit.notification.closeAll(); + UIkit.notification(""+res['message']+"",{}); + } + }, + error: function (XmlHttpRequest, textStatus, errorThrown) { + UIkit.notification.closeAll(); + UIkit.notification("Fail",{}); + }, + complete: function () {} + }) + } @@ -350,6 +392,7 @@
  • Summary
  • Interfaces
  • Exceptions
  • +
  • Hot update
  • Configurations
  • Contact me
  • @@ -406,6 +449,22 @@
  • exception 1 1 closed
  • +
  • +
    +
    +
    + + +
    +
    +
    + +
    +
    + +
    +
    +
  • diff --git a/src/main/resources/kotime.html b/src/main/resources/kotime.html index a3e4baa..1359f0e 100644 --- a/src/main/resources/kotime.html +++ b/src/main/resources/kotime.html @@ -5,6 +5,10 @@ + + + + @@ -348,6 +394,7 @@
  • 总览
  • 接口列表
  • 异常列表
  • +
  • 热更新
  • 配置
  • 技术支持
  • @@ -404,6 +451,22 @@
  • exception 1 1 未开启
  • +
  • +
    +
    +
    + + +
    +
    +
    + +
    +
    + +
    +
    +