当前位置: 首页 > article >正文

java项目部署到linux读取properties中文乱码

在idea中由于idea可以设置编码,所以读取正常。

然而部署到linux后由于linux默认编码为ISO-8859-1,所以读取后中文会乱码。

下面写一个工具类专门读取properties文件,我的properties文件在linux上是放到config目录下的。

@Slf4j
public class PropertiesUtil { 
    protected static ResourceBundle erpResponse; 
    protected static final String PROPERTIES_FILE = "config/application-config";
    static {
        try {
            erpResponse = PropertyResourceBundle.getBundle(PROPERTIES_FILE);
        } catch (Exception e) {
            log.error(PROPERTIES_FILE + "配置文件加载失败。", e);
        }
    }
 
    public static String get(String key) {
        return erpResponse.getString(key);
    }


     public static String getDynamic(String key) {
         String osName = System.getProperty("os.name").toLowerCase();
         if (osName.contains("linux")) {
             // Linux 系统使用 getChinese 方法
             return getChinese(key);
         } else {
             // 其他系统(如 Windows)使用 get 方法
             return get(key);
         }
    }

    public static String getChinese(String key) {
        String string = null;
        try {
            string = new String(erpResponse.getString(key).getBytes("ISO-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage());
        }
        return string;
    }
}
protected static final String PROPERTIES_FILE = "config/application-config";只需要放properties相对jar包的相对路径,无需后缀就行

http://www.kler.cn/a/542666.html

相关文章:

  • Visual Studio 进行单元测试【入门】
  • 前端实现在PDF上添加标注(1)
  • 数据中台是什么?:架构演进、业务整合、方向演进
  • 从云原生到 AI 原生,谈谈我经历的网关发展历程和趋势
  • PDF Shaper:免费多功能 PDF 工具箱,一站式满足您的 PDF 需求!
  • 游戏引擎学习第96天
  • 超全前端面试(全!全!全!!!)
  • LeetCode 每日一题 2025/2/3-2025/2/9
  • 2024.1.2版本Android Studio gradle下载超时问题处理
  • python基础入门:6.2JSON与CSV数据处理
  • SkyWalking 10.1.0 实战:从零构建全链路监控,解锁微服务性能优化新境界
  • qt QCommandLineOption 详解
  • 蓝桥杯算法日记|2.11二分算法
  • 【目标检测xml2json】label从VOC格式xml文件转COCO格式json文件
  • PostgreSQL 数据库压力测试指南
  • 普通用户授权docker使用权限
  • docker和docker compose版本太低问题的解决方案
  • 16.React学习笔记.React更新机制
  • 大模型被偷家?CNN结合多模态!
  • 2025.2.11——一、[极客大挑战 2019]PHP wakeup绕过|备份文件|代码审计
  • 前端设计模式介绍及案例(单例模式、代理模式、工厂模式、装饰者模式、观察者模式)
  • SpringBoot 统一功能处理之拦截器、数据返回格式、异常处理
  • clone gerrit repos 到windows本地
  • 算法设计-归并排序(C++)
  • Elasticsearch:如何使用 Elastic 检测恶意浏览器扩展
  • 基于GA遗传优化的电动汽车光储充电站容量配置算法matlab仿真