日期:2014-05-20  浏览次数:20838 次

gbk 转 utf8 问题
String b = getParameter("name"); 
b = new String(dish_id.getBytes("GBK"), "UTF-8"); 
使用上述方法,当遇到奇数位中文时出现乱码。。。 
还有其他方法转换吗?

------解决方案--------------------
Filter
------解决方案--------------------
恩,上述说写个filter是正确的,网上有很多这样的源码!
------解决方案--------------------
帮顶 楼上~~
------解决方案--------------------
可以试试URLEncoder试试。
------解决方案--------------------
用java.nio.charset来解决这种问题,先查一下getbytes("GBK")后的是不是一个汉字三个字节
------解决方案--------------------
10楼的兄弟那个是spring集成的CharacterEncodingFilter,如果不用spring的话就不行了

我这倒是有一个characterEncodingFilter的类,看看成不成

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Encoding Character to UTF-8
=================
 */

 

public class CharacterEncodingFilter
implements Filter {

private FilterConfig config = null;
// default to UTF-8
private String targetEncoding = "UTF-8";

public void init(FilterConfig config) throws ServletException {
this.config = config;
this.targetEncoding = config.getInitParameter("encoding");
}

public void destroy() {
config = null;
targetEncoding = null;
}

public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) srequest;
request.setCharacterEncoding(targetEncoding);

chain.doFilter(srequest, sresponse);
}



------解决方案--------------------
你可以在传递之前就先讲字符转换成utf8的形式啊,然后在serlvet里面再转回来,就保证不回出错了。
转换的方式跟b = new String(dish_id.getBytes("GBK"), "UTF-8"); 
差不多,只是转为byte型,就可以了
------解决方案--------------------
或者在过滤器里重写一个方法,好像是接收请求的方法。。。然后再操作,相当于是讲请求拦截了下来,
处理完成后,再继续执行下面的
------解决方案--------------------
1.加了过滤器
2.你的MySQL里面的字符编码设置正确

定义SetCharacterEncodingFilter类,实现Filter接口,代码如下:
Java code
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;

//destroy方法
public void destroy() {

   this.encoding = null;
   this.filterConfig = null;

}

//选择设置使用的字符编码
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {

   // Conditionally select and set the character encoding to be used
   if (ignore || (request.getCharacterEncoding() == null)) {
    String encoding = selectEncoding(request);
    if (encoding != null)
     request.setCharacterEncoding(encoding);
   }

   // Pass control on to the next filter
   chain.doFilter(request, response);

}

//将这个filter放置在服务器中
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 if (value.equalsIgnoreCase("yes"))
    this.ignore = true;
   else
    this.ignore = false;

}


//选择适当的字符编码
protected String selectEncoding(ServletRequest request) {
 
   return (this.encoding);

}

}


配置这个Filter,只需在web.xml文件中添加如下代码:

<filter>
   <filter-name>Set Character Encoding</filter-name>
   <filter-class>com.wanglei.SetCharacterEncodingFilter</filter-class>
   <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
   </init-param>
</filter>
   <filter-mapping>
   <filter-name>Set Character Encoding</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>