java web程序部署后,读取配置文件乱码

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值