From 1fea3eea8bf676ac6cf35c6b98e42efdbc5b897e Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期日, 29 九月 2024 11:50:08 +0800
Subject: [PATCH] 模型生成代码后备份dll和jar,用于测试运行模型

---
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java |   60 +++++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java
index f9bfa10..0c77b58 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java
@@ -1,32 +1,25 @@
 package com.iailab.module.model.mpk.controller.admin;
 
-import com.alibaba.excel.EasyExcel;
-import com.alibaba.excel.converters.longconverter.LongStringConverter;
-import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
-import com.iailab.framework.common.enums.CommonStatusEnum;
 import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants;
 import com.iailab.framework.common.pojo.CommonResult;
-import com.iailab.framework.excel.core.handler.SelectSheetWriteHandler;
-import com.iailab.framework.excel.core.util.ExcelUtils;
+import com.iailab.module.model.mpk.common.MdkConstant;
 import com.iailab.module.model.mpk.common.utils.Readtxt;
 import com.iailab.module.model.mpk.dto.MdkDTO;
-import com.iailab.module.system.enums.common.SexEnum;
 import io.swagger.v3.oas.annotations.Operation;
-import io.swagger.v3.oas.annotations.Parameter;
-import io.swagger.v3.oas.annotations.Parameters;
-import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
+import java.io.File;
 import java.lang.reflect.Method;
-import java.net.URLEncoder;
-import java.nio.charset.StandardCharsets;
-import java.util.*;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
 
 import static com.iailab.framework.common.pojo.CommonResult.error;
 import static com.iailab.framework.common.pojo.CommonResult.success;
@@ -39,8 +32,30 @@
 @RestController
 @RequestMapping("/model/mpk/api")
 public class MdkController {
+    @Value("${mpk.bak-file-path}")
+    private String mpkBakFilePath;
+
+
     @PostMapping("run")
     public CommonResult<String> run(@RequestBody MdkDTO dto) {
+
+        try {
+            File jarFile = new File(mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + dto.getPyName() + ".jar");
+            if (!jarFile.exists()) {
+                throw new RuntimeException("jar包不存在,请先生成代码。jarPath:" + jarFile.getAbsolutePath());
+            }
+            File dllFile = new File(mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + dto.getPyName() + ".dll");
+            if (!dllFile.exists()) {
+                throw new RuntimeException("dllw文件不存在,请先生成代码。dllPath:" + dllFile.getAbsolutePath());
+            }
+            // 加载jar包
+            loadJar(jarFile.getAbsolutePath());
+            // 加载dll
+            System.load(dllFile.getAbsolutePath());
+        } catch (Exception e) {
+            throw new RuntimeException("加载运行环境失败。");
+        }
+
         System.out.println("runTime=" + System.currentTimeMillis());
         try {
             List<String> datas = dto.getDatas();
@@ -103,4 +118,19 @@
         }
         return success(result);
     }
+
+    private void loadJar(String jarPath) throws Exception {
+        File jarFile = new File(jarPath);
+        if (!jarFile.exists()) {
+            throw new Exception("jar沒有找到!");
+        } else {
+            Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
+            boolean accessible = method.isAccessible();
+            method.setAccessible(true);
+            URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
+            URL url = jarFile.toURI().toURL();
+            method.invoke(classLoader, url);
+            method.setAccessible(accessible);
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3