/**
* 获得文件编码
* @param fileName
* @return
* @throws Exception
*/
public static String codeString(String fileName) {
String code = "UTF-8";
try {
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
bin.close();
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
}catch (Exception e){
e.printStackTrace();
}
return code;
}
在windows本地测试项目因为代码中写死“utf-8”导致读取出来的日志中文乱码
new InputStreamReader(new FileInputStream(logFile), "utf-8")
根据上面方法写到FileUtil工具类中调用
new InputStreamReader(new FileInputStream(logFile), FileUtil.codeString(logFileName))
自动获取文件中编码格式然后web应用返回日志数据不乱码了。