修改部分规则代码

This commit is contained in:
zouju
2026-02-09 11:15:26 +08:00
parent a78781782e
commit e07b11ef95
3 changed files with 74 additions and 3 deletions

View File

@@ -1,8 +1,14 @@
package com.solution.web.controller.algo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
@@ -37,6 +43,51 @@ public class AlgorithmController extends BaseController {
@Autowired
private IAlgorithmService algorithmService;
/**
* 导出规则列表
*/
@ApiOperation("运行python程序")
@PostMapping("/run")
public Object run(@RequestBody Algorithm algorithm) {
List<Algorithm> list = algorithmService.selectAlgorithmList(algorithm);
ObjectMapper objectMapper = new ObjectMapper();
for (Algorithm al : list) {
String codePath = al.getCodePath();
try {
String jsonInput = objectMapper.writeValueAsString(al);
String pythonExe = "E:\\Apps\\anaconda3\\python.exe";
ProcessBuilder pb = new ProcessBuilder(pythonExe, codePath);
Process process = pb.start();
try (OutputStream os = process.getOutputStream()) {
os.write(jsonInput.getBytes(StandardCharsets.UTF_8));
os.flush();
}
String result;
// 修改你 Java 代码中的这一段
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
result = reader.lines().collect(Collectors.joining("\n"));
}
int exitCode = process.waitFor();
if (exitCode != 0) {
return "Python执行失败错误码" + exitCode;
}
return objectMapper.readTree(result);
} catch (Exception e) {
e.printStackTrace();
return "执行异常: " + e.getMessage();
}
}
return "未找到算法";
}
/**
* 查询规则列表
*/
@@ -56,7 +107,7 @@ public class AlgorithmController extends BaseController {
@PreAuthorize("@ss.hasPermi('algo:algorithm:export')")
@Log(title = "规则", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, Algorithm algorithm) {
public void export(HttpServletResponse response, @RequestBody Algorithm algorithm) {
List<Algorithm> list = algorithmService.selectAlgorithmList(algorithm);
ExcelUtil<Algorithm> util = new ExcelUtil<Algorithm>(Algorithm.class);
util.exportExcel(response, list, "规则数据");