支持打包

This commit is contained in:
张家训
2026-06-11 14:10:40 +08:00
parent feeabf9f59
commit 075bf46ef2
8 changed files with 267 additions and 0 deletions
@@ -2,6 +2,7 @@ package com.example.workutils;
import com.example.workutils.system.plugin.Plugin;
import com.example.workutils.system.plugin.PluginManager;
import com.example.workutils.system.support.WinConsole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
@@ -23,6 +24,7 @@ public class WorkUtilsApplication implements CommandLineRunner {
}
public static void main(String[] args) {
WinConsole.ensureUtf8();
SpringApplication.run(WorkUtilsApplication.class, args);
}
@@ -0,0 +1,43 @@
package com.example.workutils.system.support;
import com.sun.jna.platform.win32.Kernel32;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
/**
* Windows 控制台工具:将控制台代码页切换为 UTF-8,
* 确保 System.out / System.err 输出与 logback UTF-8 编码一致,
* 解决中文乱码问题。
*/
public final class WinConsole {
private WinConsole() {}
/**
* 如果运行在 Windows 下,将控制台输出代码页设置为 UTF-8 (65001),
* 并重定向 System.out / System.err 为 UTF-8 编码。
*/
public static void ensureUtf8() {
if (!System.getProperty("os.name", "").toLowerCase().contains("win")) {
return;
}
try {
Kernel32 kernel = Kernel32.INSTANCE;
// 设置控制台输出/输入代码页为 UTF-8
kernel.SetConsoleOutputCP(65001);
kernel.SetConsoleCP(65001);
// 重定向 PrintStream,使 logback 和 System.out 都输出 UTF-8 字节
System.setOut(new PrintStream(
new FileOutputStream(FileDescriptor.out), true, StandardCharsets.UTF_8.name()));
System.setErr(new PrintStream(
new FileOutputStream(FileDescriptor.err), true, StandardCharsets.UTF_8.name()));
} catch (UnsupportedEncodingException e) {
// UTF-8 一定被支持,忽略
}
}
}
+2
View File
@@ -10,6 +10,8 @@ workutils:
damaged-repos-file: "damaged_repos.cache"
logging:
charset:
console: UTF-8
level:
com.example.workutils: INFO
org.springframework: WARN
+1
View File
@@ -4,6 +4,7 @@
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<charset>UTF-8</charset>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>