From 64d67eba963b6f43dd69b6f961c2857f9e51bf2d Mon Sep 17 00:00:00 2001 From: luzhengwang Date: Sat, 14 Mar 2026 14:33:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3Windows=E4=B8=8B=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E4=B8=AD=E6=96=87=E6=96=87=E4=BB=B6=EF=BC=8C=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=E5=8F=8A=E6=89=A7=E8=A1=8C=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E5=91=BD=E4=BB=A4bat=E7=9A=84=E4=B9=B1=E7=A0=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E9=98=B2=E6=AD=A2AI=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=96=87=E4=BB=B6=E5=8F=8A=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E7=9B=B8=E5=BA=94=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solonbot-parent/pom.xml | 2 ++ .../src/main/java/org/noear/solon/bot/core/CodeSkill.java | 4 ++-- .../java/org/noear/solon/bot/core/ProcessExecutor.java | 8 ++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/solonbot-parent/pom.xml b/solonbot-parent/pom.xml index b4ac447..fae6e58 100644 --- a/solonbot-parent/pom.xml +++ b/solonbot-parent/pom.xml @@ -18,6 +18,8 @@ 1.8 + UTF-8 + UTF-8 8.11.4 0.20.1 diff --git a/solonbot-sdk/src/main/java/org/noear/solon/bot/core/CodeSkill.java b/solonbot-sdk/src/main/java/org/noear/solon/bot/core/CodeSkill.java index 3471841..4f715d9 100644 --- a/solonbot-sdk/src/main/java/org/noear/solon/bot/core/CodeSkill.java +++ b/solonbot-sdk/src/main/java/org/noear/solon/bot/core/CodeSkill.java @@ -237,7 +237,7 @@ public class CodeSkill extends AbsSkill { try { Path gitignore = rootPath.resolve(".gitignore"); if (Files.exists(gitignore)) { - List lines = Files.readAllLines(gitignore); + List lines = Files.readAllLines(gitignore, StandardCharsets.UTF_8); // 精确匹配行,或者检查是否有以该文件名开头的有效行 boolean exists = lines.stream() .map(String::trim) @@ -245,7 +245,7 @@ public class CodeSkill extends AbsSkill { if (!exists) { String separator = (lines.isEmpty() || lines.get(lines.size()-1).isEmpty()) ? "" : "\n"; - Files.write(gitignore, (separator + fileName + "\n").getBytes(), StandardOpenOption.APPEND); + Files.write(gitignore, (separator + fileName + "\n").getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND); } } } catch (Exception ignored) { diff --git a/solonbot-sdk/src/main/java/org/noear/solon/bot/core/ProcessExecutor.java b/solonbot-sdk/src/main/java/org/noear/solon/bot/core/ProcessExecutor.java index 18de995..d62ee67 100644 --- a/solonbot-sdk/src/main/java/org/noear/solon/bot/core/ProcessExecutor.java +++ b/solonbot-sdk/src/main/java/org/noear/solon/bot/core/ProcessExecutor.java @@ -109,9 +109,13 @@ public class ProcessExecutor { public String executeCode(Path rootPath, String code, String cmd, String ext, Map envs, Integer timeoutMs, Consumer onOutput) { Path tempScript = null; try { - // 1. 持久化脚本 + // 1. 持久化脚本(Windows .bat 文件需前置 chcp 65001 以确保 UTF-8 输出) + String finalCode = code; + if (".bat".equals(ext)) { + finalCode = "@chcp 65001 > nul\r\n" + code; + } tempScript = Files.createTempFile(rootPath, "_script_", ext); - Files.write(tempScript, code.getBytes(scriptCharset)); + Files.write(tempScript, finalCode.getBytes(scriptCharset)); // 2. 构建完整命令(处理带空格的命令字符串) List fullCmd = new ArrayList<>(Arrays.asList(cmd.split("\\s+"))); -- Gitee