在使用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;
}
}