一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - PropertiesLoaderUtils 出現(xiàn)中文亂碼的解決方式

PropertiesLoaderUtils 出現(xiàn)中文亂碼的解決方式

2020-08-24 00:28編碼的三叔 Java教程

這篇文章主要介紹了PropertiesLoaderUtils 出現(xiàn)中文亂碼的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧~

?
1
2
3
4
5
6
7
8
9
10
11
try
 {
   EncodedResource encodedResource = new EncodedResource(new ClassPathResource(path), Charsets.UTF_8);
   Properties properties = PropertiesLoaderUtils.loadProperties(encodedResource);
 }
 catch (IOException e)
 {
 
   LOGGER.info("Champion:read properties failure",e);
 
 }

補充知識:使用Spring PropertyPlaceholderConfigurer 配置中文出現(xiàn)亂碼的解決方法

問題描述

在使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 讀取配置文件時,發(fā)現(xiàn)對于中文的處理會出現(xiàn)亂碼現(xiàn)象,比如有如下的配置項及其內(nèi)容:

content.shell=#!/bin/bash \necho "test,測試一下!!" \nsleep $1

采用如下的配置方式:

?
1
2
3
4
5
<bean id="propertyConifgurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
    <value>classpath:evn.properties</value>
  </property>
</bean>

通過Spring獲取到的配置項內(nèi)容,中文變成了亂碼。

解決方法

通過了解類org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的繼承關(guān)系,發(fā)現(xiàn)父類org.springframework.core.io.support.PropertiesLoaderSupport中有這樣的屬性fileEncoding,這一屬性的使用是在loadProperties方法中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * Load properties into the given instance.
 * @param props the Properties instance to load into
 * @throws IOException in case of I/O errors
 * @see #setLocations
 */
protected void loadProperties(Properties props) throws IOException {
  if (this.locations != null) {
    for (Resource location : this.locations) {
      if (logger.isInfoEnabled()) {
        logger.info("Loading properties file from " + location);
      }
      try {
        PropertiesLoaderUtils.fillProperties(
            props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister);
      }
      catch (IOException ex) {
        if (this.ignoreResourceNotFound) {
          if (logger.isWarnEnabled()) {
            logger.warn("Could not load properties from " + location + ": " + ex.getMessage());
          }
        }
        else {
          throw ex;
        }
      }
    }
  }
}

通過添加fileEncoding=utf-8屬性可以解決上述問題:

?
1
2
3
4
5
6
7
8
<bean id="propertyConifgurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
    <value>classpath:evn.properties</value>
  </property>
  <property name="fileEncoding">
    <value>utf-8</value>
  </property>
</bean>

以上這篇PropertiesLoaderUtils 出現(xiàn)中文亂碼的解決方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/ppwwp/article/details/106032712

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 特黄a大片免费视频 | 干操网 | 免费国产好深啊好涨好硬视频 | 日本sss在线高清观看 | 欧美日韩精品一区二区三区视频在线 | 亚洲一区 在线播放 | 美女做又爽又黄又猛 | 亚洲国产欧美目韩成人综合 | 成全动漫视频在线观看 | 欧美老人与小伙子性生交 | 亚洲 欧美 中文 日韩 另类 | 欧美人与禽交片在线播放 | 特黄aa级毛片免费视频播放 | 欧美大片一级片 | 天天色色色 | 日本人做受全过程视频 | 久久99精品国产自在自线 | 国产精品伊人 | 国内精品自产拍在线观看91 | 亚洲国产精品无码中文字满 | 天干夜天天夜天干天ww | 日本视频二区 | 国产一级精品高清一级毛片 | 日韩在线一区二区三区免费视频 | yy6080午夜国产免费福利 | 国产精品乱码高清在线观看 | 美国美女hd18| 欧美人曾交 | 亚洲天堂男人 | 手机能看的黄色网站 | 免费二区 | 欧美坐爱 | 欧美一区不卡二区不卡三区 | 农村妇女野外牲交一级毛片 | 热久久最新视频 | 夫妻性生活影院 | 精品国产自在现线拍国语 | 国产精品亚洲综合第一区 | 国产主播99 | 亚洲欧美久久婷婷爱综合一区天堂 | 美女尿口照片 |