【工具】Spring中獲取properties引數&解決中文亂碼
使用java.util.Properties
工具類程式碼:
package com.guide.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.HashMap;
import java.util.Properties;
/**
* Desc:properties檔案獲取工具類
*
*/
public class PropertyUtil {
private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
private static Properties props;
synchronized static public HashMap<String, String> loadProps(String prop){
HashMap<String, String> map = new HashMap<>();
logger.info("開始載入properties檔案內容.......");
props = new Properties();
InputStream in = null;
try {
/*第一種,通過類載入器進行獲取properties檔案流*/
in = PropertyUtil.class.getClassLoader().getResourceAsStream(prop);
/*第二種,通過類進行獲取properties檔案流*/
//in = PropertyUtil.class.getResourceAsStream(prop);
props.load(in);
} catch (FileNotFoundException e) {
logger.error("properties檔案未找到" );
} catch (IOException e) {
logger.error("出現IOException");
} finally {
try {
if(null != in) {
in.close();
}
} catch (IOException e) {
logger.error("properties檔案流關閉出現異常");
}
}
logger.info("載入properties檔案內容完成...........");
logger.info("properties檔案內容:" + props);
for(Object i:props.keySet()){
map.put((String)i,props.getProperty((String)i));
}
return map;
}
}
java.util.Properties類中的load(InputStream inStream)方法如下:
/**
* Reads a property list (key and element pairs) from the input
* byte stream. The input stream is in a simple line-oriented
* format as specified in
* {@link #load(java.io.Reader) load(Reader)} and is assumed to use
* the ISO 8859-1 character encoding; that is each byte is one Latin1
* character. Characters not in Latin1, and certain special characters,
* are represented in keys and elements using Unicode escapes as defined in
* section 3.3 of
* <cite>The Java™ Language Specification</cite>.
* <p>
* The specified stream remains open after this method returns.
*
* @param inStream the input stream.
* @exception IOException if an error occurred when reading from the
* input stream.
* @throws IllegalArgumentException if the input stream contains a
* malformed Unicode escape sequence.
* @since 1.2
*/
public synchronized void load(InputStream inStream) throws IOException {
load0(new LineReader(inStream));
}
其中預設的編碼格式是iso-8859-1.
但是Properties中還有一個傳遞Reader引數的方法,我們可以使用Reader來包裝接收的InputStream
BufferedReader bf = new BufferedReader(new InputStreamReader(in, "UTF-8"));
props.load(in);
使用Spring框架中的org.springframework.core.io.support.PropertiesLoaderUtils
private static Properties springloadprop() {
EncodedResource encodedResource = new EncodedResource(new ClassPathResource("specialcity.properties"), "UTF-8");
try {
props = PropertiesLoaderUtils.loadProperties(encodedResource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return props;
}
OK
相關推薦
【工具】Spring中獲取properties引數&解決中文亂碼
使用java.util.Properties 工具類程式碼: package com.guide.util; import org.slf4j.Logger; import org.slf
【工具】mac下使用secure CRT時中文亂碼解決方法
mac下使用secure CRT時中文亂碼解決方法 需要兩步解決問題: 1、設定SecureCRT編碼為UTF-8 Options->Global Options->Default Session->Edit Default Settings...-&
【工具】Get Data-獲取論文圖片中資料的工具
在資料蒐集的過程中,可能會遇到論文中有資料,但是沒法獲取其具體值的情況,在資料搜尋的過程中找到兩種方法,一種是使用Get Data,另一種是使用Origin的外掛 還沒使用,先堆上找了幾個都有很多廣告,大家自己找軟體吧 小木蟲上說,誤差不會太大。結合各自的實際情況使用吧
【轉】python中獲取python版本號的方法
n) https href light nor body true print brush 原文 python3 #!/usr/bin/python # 第1種方法 import platform print(platform.python_version())
【記錄】spring中一個介面多個實現類
重構遇到個小問題,記錄下: 錯誤資訊: *************************** APPLICATION FAILED TO START *************************** Description: Field xxxService in com.ali
【轉】spring中對控制反轉和依賴注入的理解
由於最近沒有太多時間,不能深入的瞭解控制反轉和依賴注入,只好把別人的理解轉載過來,非常痛恨市面上各種教程對所謂的術語張口就來,等自己有了時間一定要寫出新手都能看懂的教程。 首先想說說IoC(Inversion of Control,控制反轉)。這是spring的核心,貫穿始終。所謂IoC
【ssh】struts2中action接收引數的方法
Struts2中Action接收引數的方法主要有以下三種: Struts2中Action接收引數的方法主要有以下三種: 1.使用Action的屬性接收引數: a.定義:在Action類中定義屬性,建立get和set方法; b.接收:通過屬性接收引數,如
【記錄】mybatis中獲取常量類中資料
部分轉載,已註明來源:1、mybatis中獲取常量類中資料<update id="refuseDebt"> UPDATE dt_debt a SET a.debt_status = ${@[email protected]_R
【轉】Spring 中配置sessionFactory及用法(JAVA後端)
編輯 刪除 Spring 中配置sessionFactory及用法 方法一: 1、在Spring的applicationContext.xml中配置bean <!-- 啟用註解注入 --> <context:annota
【JavaScript】 JS中獲取HTML元素值的三種方法
JavaScript中獲取HTML元素值的三種方法 JS獲取DOM元素的方法(8種): 通過ID獲取(getElementById) 通過name屬性(getElementsByName) 通過標籤名(getElementsByTagName) 通過類
HTML獲取連結中的引數(解決中文亂碼問題)
這是之前在網上找的一個HTML頁面獲取連結後面的引數的例子,本人覺得寫的很好,一直在用,下面貼上上js原始碼: var LocString=String(window.document.location.href); function GetQueryS
JS 獲取url引數以及中文亂碼問題
encodeURI() 函式可把字串作為 URI 進行編碼 var para=window.location.search;// 當前請求的url的引數部分 console.log(para) /
spring 中@ModelAttribute繫結變數中文亂碼的解決辦法
網上找了一些方法: 方法一 在專案的web.xml中配置spring的Character Encoding Filter <!-- Servlet Encoding Start --> <filter> <filter
docker中配置mysql並解決中文亂碼
在docker中配置並使用mysql映象步驟: 1、獲取mysql映象 docker pull mysql 2、載入並執行mysql映象 docker run --name mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=123456
【Spring 系列】一、如何從spring中獲取bean
導包 spring要管理的類資訊 package com.liuyong666.service.impl; import com.liuyong666.service.PersonService; public class PersonServiceBean implements Person
【spring Boot】Spring中@Controller和@RestController之間的區別
處理 public 不同 esp 舉例 rest control tro adding spring Boot入手的第一天,看到例子中的@RestController ............. 相同點:都是用來表示Spring某個類的是否可以接收HTTP請求 不同點:@C
【spring Boot】spring boot獲取資源文件的三種方式【兩種情況下】
不用 ash 12c ica pla bsp foreach set 使用 首先聲明一點,springboot獲取資源文件,需要看是 1》從spring boot默認的application.properties資源文件中獲取 2》還是從自定義的資源文件中獲取
【java】spring項目中 對entity進行本類間的克隆
tor mini cti false display des private rac 重寫 方法1: 【使用spring自帶BeanUtils實現克隆】 【要求:需要被克隆的類實現Cloneable接口並且重寫clone()方法】 》例子: 》》實體: package
【Kotlin】spring boot項目中,在Idea下啟動,報錯@Configuration class 'BugsnagClient' may not be final.
post open conf png spa temp 技術 dea and 報錯如下: Exception encountered during context initialization - cancelling refresh attempt: org.spr
【轉】Django中使用POST方法獲取POST數據
class 需要 request www ict .html bsp 請求 post 1.獲取POST中表單鍵值數據 如果要在django的POST方法中獲取表單數據,則在客戶端使用JavaScript發送POST數據前,定義post請求頭中的請求數據類型: