關於Spring boot中讀取屬性設定檔出現中文亂碼的問題的解決__spring

來源:互聯網
上載者:User

原文連結:http://www.zslin.com/web/article/detail/47


 問題描述

當在.properties的設定檔中有中文時,讀取出來的總是亂碼。比如我的application.properties設定檔的內容如下:

server.port=9090test.msg=你好:Springboot

當在程式中讀取test.msg值時總是會得到亂碼,預設是以ISO-8859-1的字元編碼讀取,嘗試的方法有:

1、添加配置

banner.charset=UTF-8server.tomcat.uri-encoding=UTF-8spring.http.encoding.charset=UTF-8spring.http.encoding.enabled=truespring.http.encoding.force=truespring.messages.encoding=UTF-8

2、設定檔案類型

將application.properites的檔案類型修改為UTF-8的編碼類別型。

通過以上方法測試擷取出來的值還是亂碼。 · 解決辦法

設定 File Encodings的Transparent native-to-ascii conversion為true,具體步驟如下:依次點擊

File -> Settings -> Editor -> File Encodings

將Properties Files (*.properties)下的Default encoding for properties files設定為UTF-8,將Transparent native-to-ascii conversion前的勾選上。


這是網上最多的一種解決方案,但是在eclipse下開發的話,會發現根本就找不到對應的Transparent native-to-ascii conversion設定選項。


那麼在eclipse下如何解決Spring boot讀取設定檔出現中文亂碼的問題呢。

     首先,分析勾選Transparent native-to-ascii conversion設定的作用,中文翻譯是透明native-to-ascii轉換。

     分析是eclipse中的屬性檔案都會轉化為ascii碼再去被解析。

     百度搜尋,eclipse中怎麼設定properties檔案的Transparent native-to-ascii conversion

     得到:   在eclipse中安裝properties外掛程式PropertiesEditor及設定(附圖),ASCII碼轉換成中文

    http://www.cnblogs.com/zdz8207/p/java-eclipse-properties.html

    推薦使用裡面的離線安裝。

    安裝完成PropertiesEditor 外掛程式後,使用該編輯器重新編輯屬性檔案中的中文,然後重新運行程式,

    發現讀取的中文設定檔不是亂碼了。






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.