问题:页面链接后加中文参数,中文是乱码。
解决方案:修改Tomcat的配置文件。修改conf/server.xml 字符集
原先:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8445" />
修改后:
<Connector port="7001" protocol="HTTP/1.1"
connectionTimeout="20000" URIEncoding="UTF-8"
redirectPort="8445" />
--------------------------------------------------------------------------------
下载的文件名还中文
action中:
response.reset();
//设置响应类型
response.setContentType("application/octet-stream");
//设置响应的文件名称,并转换成中文编码
String fileName=response.encodeURL(new String(returnName.getBytes(),"ISO8859_1"));
response.addHeader("Content-Disposition", "attachment;filename="+fileName);
downfile.jsp 另外一种直接之间写在jsp中
response.setHeader("content-type","text/html;charset=gb2312");
response.setHeader("Content-disposition","attachment; filename="+URLEncoder.encode(sFile, "utf-8")); //注意此处必须用utf-8编码,否则乱码
--------------------------------------------------------------------------------
js 提交action
js文件中:
var paras;
paras = "cnname=" + encodeURI(encodeURI(cnname)); //提交中文时转码
paras += "&curl=" + encodeURI(encodeURI(curl));
action中:
String cnname = request.getParameter("cnname");
cnname = java.net.URLDecoder.decode(cnname, "utf-8"); //中文乱码解决
--------------------------------------------------------------------------------
js ajax 提交 action
jsp文件
var vuserName=($("#userName").val());
vuserName = encodeURI(vuserName); //中文乱码
if(vuserName!=""){
$("#ts").html("正在加载...");
$.post("ajaxAutoCompleteAction.do",{userName:vuserName},function(m){
$("#ts").html("<font color=#ff8800;><strong>提示:用鼠标或方向键选取</strong></font>"+unescape(m));
$("#ts>a").bind("click",vst);
$("#ts").css("display","block");
//初始化全局变量
a_i=-1;
});
}else{
$("#ts").css("display","none");
}
ajax action文件:
String name = request.getParameter("userName");
name = java.net.URLDecoder.decode(name, "utf-8"); //中文乱码
--------------------------------------------------------------------------------
jsp 提交jsp
<%
response.setHeader("content-type","text/html;charset=gb2312");
String swfFileName = UtilFuns.ConvertNull(request.getParameter("swfFileName"));
if(swfFileName.compareTo("")==0){
swfFileName = "doc/陕西省电力公司班组信息系统项目周报(20110104-20110107).swf";
}
swfFileName = java.net.URLDecoder.decode(swfFileName, "utf-8"); //中文乱码解决
%>
--------------------------------------------------------------------------------
submit提交到action, action重定向
urlPara = new String(urlPara.getBytes("gbk"), "ISO8859_1"); //必须转一次码,不然打开新窗口,url中文部分乱码
response.sendRedirect(reportUrl + "?" + urlPara);
--------------------------------------------------------------------------------
ajax提交到SaveAction
js文件中:
paras2 = "&rejectCS=" + encodeURI(rejectCS); //提交中文时转码
--------------------------------------------------------------------------------
ajax提交到action返回串, 在页面显示
js文件中:
paraString += "&" + list[i].name + "=" + encodeURI(encodeURI(list[i].value)); //提交中文时转码
action中:
response.setContentType("text/html;charset=gbk"); //设定字符集,不然返回字符乱码
response.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
//列出本部门所有人员的周总结
out.write(this.getDeptWeekSumUpList(cdBeginDate, request));