在使用Environment讀取參數時,如果裏面有中文讀到的一般會是亂碼,網上一般都是推薦的修改idea的編碼等,但這樣做你可能會不生效。
你可以通過以下方法來讀取配置文件,手動設置爲指定編碼來讀取。
public static String readPropertiesFile(String key) throws FileNotFoundException, IOException {
try {
Resource fileRource = new ClassPathResource("application.properties");
Properties props = new Properties();
props.load(new InputStreamReader(fileRource.getInputStream(), "UTF-8"));
String value = props.getProperty(key);
System.out.println(key + "鍵的值是:" + value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}