有四个方法,记录一下,方便以后自己查找,也方便大家。
首先:
< %@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
并且
request.setCharacterEncoding("gb2312");
第二:
配置 server.xml。
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>
第三:
配置 web.xml
<filter>
<filter-name>setEncoding</filter-name>
<filter-class>com.dinner.filter.CharsetFilters</filter-class>
<init-param>
<param-name>coding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第四:
工程编码
开发中发现Windows版Eclipse默认Java和Web工程的默认编码方式不那么遂人愿,修改方法如下:
1. Web工程文件编码修改方式
Window -> Preference -> Content types
推荐将Web相关文件的编码都设置为UTF-8
2. Java源文件编码修改方式
Window -> Preference -> Workspace -> Text file encoding
Windows平台默认为GBK,Linux平台默认为UTF-8
经过这四个步骤,肯定能解决的。
连接来源:http://blog.sina.com.cn/s/blog_62aab2760100l1hx.html
现在项目处理乱码的方法:
后台:this.setName(URLDecoder.decode(this.getAjaxInData(),"utf-8"))
前台:encodeURI($("#diaodumingcheng").val())
server.xml:<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>
strtuts2图片下载
@Action(value="fileDownload",
results={@Result(name="success", type="stream", params={
"contentType", "application/octet-stream;charset=ISO8859-1",
"inputName", "inputStream",
"contentDisposition", "attachment;filename=${upPicFileFileName2}",
"bufferSize", "4096"
})})
public String fileDownload() throws FileNotFoundException {
//upPicFileFileName是服务器存放的文件名
//upPicFileFileName2是用户上传的名称
try {
serv.fileDownload(this);
String str = new String(this.upPicFileFileName2.getBytes("GBK"),"ISO8859-1");
this.upPicFileFileName2 = str;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}