在進行文件下載時liunx下出現中文文件名亂碼,windows下卻沒有.可能是不同操作系統的編碼方式不同?(含糊不清的說法),用
Properties initProp = new Properties(System.getProperties());
System.out.println(prop.getProperty("file.encoding"));能得到編碼方式都是UTF-8
后來發現用
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");不管linux還是windows 下用火狐都是顯示正常的,IE下卻顯示亂碼,所以問題出在瀏覽器,得在程序中加入判斷(分別處理firefox跟Ie):
//判斷是否是使用IE的方法
String userAgent = request.getHeader("User-Agent");
boolean isIE = false;
//userAgent.toLowerCase().indexOf("msie")
if(userAgent.indexOf("MSIE") > 0){
isIE = true;
}
if(isIE){
fileName = new String(fileName.getBytes("gb2312"), "iso-8859-1");
}else{
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
}