java读取prpperties文件中配置信息,获取value值中带中文时乱码文件解决方案

最近做了个需求,项目下载模板文件,文件全路径配置在properties中,文件名为中文:

user_list=/home/file/用户清单列表.xlsx

读取到java中后发生乱码,结果为:/home/file/ç¨æ·æ¸åå表.xlsx,造成无法解析文件,进而无法输出文件。
后面发现,properties虽然在idea中设置为了UTF-8,并且在linux,win中查看properties文件,编码为UTF-8,
但实际上仍然为ISO-8859-1,解决办法为将properties中获取到的字符串(ISO-8859-1)转化为UTF-8,即可解决问题,附代码如下:

@GetMapping("/getParam")
public void getMethodParam(HttpServletRequest request)throws Exception{
    //获取配置文件路径
    String propertiesFileName="D:\\A_WorkSpace\\decorate-furniture\\decorate_web\\src\\main\\resources\\a.properties";
    Properties props = new Properties();
    props.load(new FileInputStream(propertiesFileName));
   //获取配置文件中key为value_list的value的值
    String user_list =props.getProperty("user_list");
   //将ISO-8859-1转化为UTF-8
    String new_user_list=new String(user_list.getBytes("ISO-8859-1"), "utf-8");//这一句是重点
   //日志打印效果
    2019-07-18 00:33:32,101  INFO SessionConfiguration:43 - -------------------------user_list------------------------------/home/file/ç¨æ·æ¸åå表.xlsx
2019-07-18 00:33:32,103  INFO SessionConfiguration:44 - -------------------------new_user_list------------------------------/home/file/用户清单列表.xlsx

}

附图:

发布了20 篇原创文章 · 获赞 24 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_41267342/article/details/96393573