PrinterConfig.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.mes.print.config;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.nio.charset.StandardCharsets;
  7. import java.nio.file.Files;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Properties;
  11. import java.util.regex.Pattern;
  12. import com.mes.print.PrinterException;
  13. /**
  14. * 打印配置加载器(UTF-8)。查找顺序:
  15. * 1. 系统属性 -Dprint.config=xxx 指定的路径
  16. * 2. 当前工作目录 ./config/print.properties
  17. * 3. classpath 上的 print.properties(打进 jar 里的默认值)
  18. * 都没有就全用默认值(标签机 TSC TTP-244 PRO)
  19. */
  20. public class PrinterConfig {
  21. private static final String CONFIG_FILE = "config/print.properties";
  22. /** 默认标签机名 */
  23. public static final String DEFAULT_LABEL_PRINTER = "TSC TTP-244 PRO";
  24. private static Properties props;
  25. public static synchronized Properties load() {
  26. if (props != null) return props;
  27. Properties p = new Properties();
  28. // 用 UTF-8 读:打印机名可能带中文(如「副本 1」),按 ISO-8859-1 读会乱码
  29. try (InputStream in = openStream()) {
  30. // 找不到配置文件就全用默认值,单 jar 放在任何目录都能跑
  31. if (in != null) p.load(new InputStreamReader(in, StandardCharsets.UTF_8));
  32. } catch (Exception e) {
  33. throw new PrinterException("加载打印配置失败: " + e.getMessage(), e);
  34. }
  35. props = p;
  36. return props;
  37. }
  38. private static InputStream openStream() throws Exception {
  39. String sysPath = System.getProperty("print.config");
  40. if (sysPath != null && new File(sysPath).exists()) {
  41. return new FileInputStream(sysPath);
  42. }
  43. File f = new File(CONFIG_FILE);
  44. System.out.println("[PrinterConfig] 查找配置文件: " + f.getAbsolutePath() + " (exists=" + f.exists() + ")");
  45. if (f.exists()) return new FileInputStream(f);
  46. InputStream in = PrinterConfig.class.getClassLoader().getResourceAsStream("print.properties");
  47. if (in != null) {
  48. System.out.println("[PrinterConfig] 从 classpath 加载配置");
  49. return in;
  50. }
  51. System.out.println("[PrinterConfig] 未找到配置文件,使用默认值");
  52. return null;
  53. }
  54. public static String get(String key) {
  55. return load().getProperty(key);
  56. }
  57. public static String get(String key, String defaultValue) {
  58. String v = load().getProperty(key);
  59. return v == null ? defaultValue : v;
  60. }
  61. /** 标签机 Windows 打印机名:label.printer.name,没配用默认 */
  62. public static String getLabelPrinterName() {
  63. String name = get("label.printer.name", "").trim();
  64. return name.isEmpty() ? DEFAULT_LABEL_PRINTER : name;
  65. }
  66. /** 重置缓存,下次 get 重新加载 */
  67. public static synchronized void reset() {
  68. props = null;
  69. }
  70. /**
  71. * 写单个配置项到 ./config/print.properties 并立即生效。
  72. * 按行替换,保留文件里的注释和其他键;键不存在就追加到末尾。
  73. */
  74. public static synchronized void set(String key, String value) {
  75. File f = new File(CONFIG_FILE).getAbsoluteFile();
  76. // properties 里反斜杠是转义符,网络打印机名 \\server\TSC 要写成 \\\\server\\TSC
  77. String entry = key + "=" + (value == null ? "" : value.trim().replace("\\", "\\\\"));
  78. Pattern keyLine = Pattern.compile("^\\s*" + Pattern.quote(key) + "\\s*[=:].*$");
  79. try {
  80. List<String> lines = f.exists()
  81. ? new ArrayList<>(Files.readAllLines(f.toPath(), StandardCharsets.UTF_8))
  82. : new ArrayList<>();
  83. boolean replaced = false;
  84. for (int i = 0; i < lines.size(); i++) {
  85. if (keyLine.matcher(lines.get(i)).matches()) {
  86. lines.set(i, entry);
  87. replaced = true;
  88. break;
  89. }
  90. }
  91. if (!replaced) lines.add(entry);
  92. File dir = f.getParentFile();
  93. if (dir != null && !dir.exists()) dir.mkdirs();
  94. Files.write(f.toPath(), lines, StandardCharsets.UTF_8);
  95. } catch (Exception e) {
  96. throw new PrinterException("保存打印配置失败: " + e.getMessage(), e);
  97. }
  98. reset();
  99. }
  100. }