首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

中文乱码的过滤器

2013-09-26 
求一个中文乱码的过滤器求一个万能的中文乱码过滤器,用于java web工程。要求能适用于web请求,安卓请求和IOS

求一个中文乱码的过滤器
求一个万能的中文乱码过滤器,用于java web工程。
要求能适用于web请求,安卓请求和IOS请求。最好所有的编码转换都在服务端进行,客户端不关心乱码的问题。 java?web 编码 安卓 ios 乱码
[解决办法]
直接修改tomcat的配置好了,如下:
文件:tomcat根目录\apache-tomcat-6.0.29\conf\server.xml


<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443"  URIEncoding="UTF-8"/>

[解决办法]
还是统一编码比较好,
像这种‘%E7%BC%96%E7%A0%81’
utf-8下为‘编码’
gbk 下为‘缂栫爜’哪个对
[解决办法]
没有万能的,有的话大家早就用了。 

结贴散分吧~~
[解决办法]
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class EncodingFilter implements Filter {

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
MyRequest myRequest = new MyRequest(req);
chain.doFilter(myRequest, response);

}

public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}

public void destroy() {


// TODO Auto-generated method stub

}

}

class MyRequest extends HttpServletRequestWrapper {

private HttpServletRequest httpServletRequest;

public MyRequest(HttpServletRequest request) {
super(request);
this.httpServletRequest = request;
}

@Override
public String getParameter(String name) {
String value = httpServletRequest.getParameter(name);
try {
value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}

}


[解决办法]
谢谢。木有万能的!

你只能尽量统一编码,所有的都用Utf-8,这是个人经验问题。

反正我是连html页面编码格式,注意是页面编码格式都是utf-8!

但是这样偶尔大的项目也还是会碰到编码问题,但是只能说能最好的避免了。

如果不统一编码,就只能等屎了。。中文乱码的过滤器
[解决办法]
都用UTF-8的编码,将来会更省事。

热点排行
Bad Request.