java web程序部署后,读取配置文件乱码
java web 程序直接运行时,读取配置文件不乱码,部署后发现读取的配置文件发生了乱码现象,刚开始试了好多版本,修改了tomcate中的server.xml,在节点中增加了useBodyEncodingForURI=“true”,重启tomcate后,仍然乱码,最后在读取配置文件处加了编码格式,重启tomcate后乱码问题解决了。
之前读配置文件的代码如下:
//根据文件路径获取接口文件内容
public static String fileContent(String strPath) {
String result = “”;
File file = new File(strPath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
String s = null;
while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
result = result + “\n” + s;
}
br.close();
} catch (Exception e) {
result = null;
e.printStackTrace();
}
return result;
}
读取配置文件处加上了编码格式,乱码问题解决了,代码如下:
public static String fileContent(String strPath) {
String result = “”;
File file = new File(strPath);
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), “UTF-8”);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
result = result + “\n” + s;
}
br.close();
} catch (Exception e) {
result = null;
e.printStackTrace();
}
return result;
}
java web程序部署后,读取配置文件乱码
最新推荐文章于 2023-07-04 16:23:18 发布