支持打包
This commit is contained in:
@@ -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 一定被支持,忽略
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user