public String getstr(String str)
{
try{
    String temp_p = str;
    byte[] temp_t = temp_p.getBytes("ISO8859-1"); 
String temp = new String(temp_t);
return temp;
   }catch(Exception e)
   {
   
   }
   return "null";
}

解决方案 »

  1.   

    在页面最上边添加<%@ page contentType='text/html; charset=GB2312' %>
    这样可以使从后台来的数据以GB2312显示
    如果还有问题,把取出来的数据利用下面的函数进行编码转换,这段代码写在<%!%>之间,放在<html>之前
    转码代码:
    private  String codeString(String s){
       String str=s;
       String temp="";    try{
          byte[] temp_t=str.getBytes("ISO-8859-1");
        temp=new String(temp_t,"GB2312");
        log.info("codeString = " + str);
    return temp;
      }
    catch(Exception e)
     {
      log.error("e codeString = " + str);
       return temp;
     }
       }
      

  2.   

    第一步:你确定数据库中的记录是乱码么?如果是的话,先解决数据库里面乱码的问题,具体的解决办法我已公布于:
    http://community.csdn.net/Expert/topic/3355/3355594.xml?temp=.2952692
    帖子的名称为 oracle816的字符集.
    第二步:如果数据库的内容正常的只是显示的时候出现乱码:
    ok: 出现乱码的问题有下面三种情况:
      first:  request引起的,如果是的话,加入filter处理,办法:
               --------------------------------------------------------------
             在web.xml中加入
      <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>web.SetCharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>gbk</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    对应的java程序:/**
     * <p>Title: test and study</p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: sdzs</p>
     * @author meconsea
     * @version 1.0
     */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;
    import javax.servlet.UnavailableException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class SetCharacterEncodingFilter implements Filter{  public SetCharacterEncodingFilter() {
      }  protected String encoding = null;
      protected FilterConfig filterConfig = null;
      protected boolean ignore = true;  public void destroy(){
        this.encoding = null;
        this.filterConfig = null;
      }  public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException{
        if(ignore || request.getCharacterEncoding() == null){
          encoding = selectEncoding(request);
          if(encoding != null){
            request.setCharacterEncoding(encoding);
          }
        }
        chain.doFilter(request,response);
      }  public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
        String value = filterConfig.getInitParameter("ignore");
        if(value == null){
          ignore = true;
        } else if(value.equalsIgnoreCase("true")){
           ignore = true;
        } else if(value.equalsIgnoreCase("yes")){
           ignore = true;
        } else{
           ignore = false;
        }
      }  protected String selectEncoding(ServletRequest request){
        return this.encoding;
      }}在jsp页面上加入:
    <%@ page contentType="text/html; charset=gbk" %>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gbk">
    ____________________________________________________________________________________
    second: 写入数据库的时候是乱码(chinese --->unicode ):解决办法:
    public static String gbToUn(String aNewStringIn) throws Exception {
        String stringOut = null;
        if(aNewStringIn == null || (aNewStringIn.trim()).equals("")) {
          return aNewStringIn ;
        }
        byte[] b = aNewStringIn.getBytes("GBK") ;
        stringOut = new String(b , "ISO8859-1") ;
        return stringOut ;
      }third 从数据库读取到页面unicode -- >chinese
     public static String unToGb(String str){
       String gbStr = null;
       if((str == null)||(str.trim().equals("")){
          return str;
       }
       byte[] b = str.getBytes("ISO-8859-1");
       gbStr = new String(b,"GBK");
       return gbStr;
     }一切ok!