diff --git a/solonbot-parent/pom.xml b/solonbot-parent/pom.xml index b4ac447ac9630f00241a778fd3e3eb7a020d7b29..fae6e58fb4dfe1fd8f54b63511d7af5a9af35f4d 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 3471841de3f1e44b148df1e45eac77e122dd082c..4f715d96b0c1b3803be5c6d8c1503f9c53d093fe 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 18de995ba2c9f697328e3167f506227ee9be9a5d..d62ee67fc14beed9d1980298c1a25b4678561b05 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+")));