针对乱码,一般情况是前后台以及中间件编码格式不同导致.故需要保证编码格式相同即可.
1.前台页面格式指定,这里用的是jsp,故需要将pageEncoding指定为"UTF-8"
2.指定服务器编码格式,比如用的tomcat,默认是"ISO-8859-1",打开server.xml找到"
3.若服务器未指定编码格式,需要在接收的地方强制转码,new String("传递的中文参数".getBytes("ISO-8859-1"),"UTF-8"),若指定服务器编码,则不需要强制转换,否则转换两次则转换成其他字符.
4.Excel下载中,需要根据浏览器指定编码,具体如下:
String agent = request.getHeader("USER-AGENT").toLowerCase();
String codeFileName = java.net.URLEncoding.encode(fileName, "UTF-8");
if(agent.contains("firefox")) {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO-8859-1") + ".xls");
} else {
response.setHeader("Content-Disposition", "attachment;filename=" + codeFileName + ".xls");
}