在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
首先看是从什么地方开始出现的乱码,只要统一编码,就不会出现乱码,下面以uft-8(个人认为最好)为例,详细说明: 2、如果乱码是在servlet中出现的,则有两种方法: 复制代码 代码如下: package com.sharep.filter;//包名 import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase("true")) this.ignore = true; else this.ignore = false; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) request.setCharacterEncoding(encoding); } chain.doFilter(request, response); } public void destroy() { this.encoding = null; this.filterConfig = null; } protected String selectEncoding(ServletRequest request) { return (this.encoding); } } 然后在web-inf的web.xml中加上如下代码: 复制代码 代码如下: <filter> <filter-name>SetCharacterEncoding</filter-name> <filter-class>com.young.filter.SetCharacterEncodingFilter</filter-class>//注意这里是类名,要有完整包名 <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 这样就搞定了 复制代码 代码如下: String url = “jdbc:mysql://localhost:3306/test2?autoReconnect=true&useUnicode=true&characterEncoding=gbk&mysqlEncoding=utf8″ ;
如果还是不行的话就是用 复制代码 代码如下: show variables like ‘collation_%'; 这个命令来查看默认字符集,如果不是utf-8的话在my.ini(windows)或者是my.cnf(linux)将相应的编码修改成utf8之后重启mysql服务器就ok了 |
请发表评论