溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何解決PropertiesLoaderUtils 中文亂碼的問(wèn)題

發(fā)布時(shí)間:2020-11-07 16:45:23 來(lái)源:億速云 閱讀:415 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)如何解決PropertiesLoaderUtils 中文亂碼的問(wèn)題,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

   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);
 
    }

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

問(wèn)題描述

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

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

采用如下的配置方式:

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

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

解決方法

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

  /**
   * 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;
          }
        }
      }
    }
  }

通過(guò)添加fileEncoding=utf-8屬性可以解決上述問(wèn)題:

<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>

看完上述內(nèi)容,你們對(duì)如何解決PropertiesLoaderUtils 中文亂碼的問(wèn)題有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI