Java中常遇的中文乱码问题解决方案

说明:将编码设置为“utf-8”和“gb2312”,均能解决乱码问题,需要注意的是,一定要保证所有的编码一致,这里我采用“utf-8”

1.解决request请求中的中文乱码问题

  • get请求方式解决方案

String param = request.getParameter("param");
param = new String(param.getBytes("ISO-8859-1"), "utf-8");

  • post请求方式解决方案

reqeust.setCharacterEncoding("utf-8")

2.解决response响应中的中文乱码问题

response.setCharacterEncoding("utf-8");

3.解决HTML页面中的中文乱码问题

<META http-equiv=Content-Type content="text/html;charset=utf-8">

4.解决JSP页面中的中文乱码问题

<%@ page contentType="text/html;charset=utf-8" language="Java"%>

5.解决页面数据传输的中文乱码问题

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class> cn.nd.filter.Filter
        </filter-class> 
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Filter代码:

package cn.nd.filter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

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;
import javax.servlet.http.HttpServletResponse;

public class Filter implements javax.servlet.Filter {

    public void destroy() {
        // TODO Auto-generated method stub

    }

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) req;
        HttpServletResponse response=(HttpServletResponse) res;

        HttpServletRequest myrequest=new MyRequest(request);
        response.setContentType("text/html;charset=utf-8");

        chain.doFilter(myrequest, response);

    }

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

    }

}
class MyRequest extends HttpServletRequestWrapper {

    private HttpServletRequest request;

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

    // 重写关于获取请求参数的方法.
    @Override
    public String getParameter(String name) {
        Map<String, String[]> map = getParameterMap();
        if(name==null){
            return null;
        }
        String[] values=map.get(name);
        if(values==null||values.length==0){
            return null;
        }
        return values[0];


    }

    @Override
    public String[] getParameterValues(String name) {
        Map<String, String[]> map = getParameterMap();
        if(name==null){
            return null;
        }
        String[] values=map.get(name);
        return values;
    }

    private boolean flag = true;

    @Override
    public Map getParameterMap() {
        // 1.得到所有请求参数的Map集合
        Map<String, String[]> map = request.getParameterMap();                                         // 有编码问题.
        if(flag){
            for(String s:map.keySet()){
                String[] values=map.get(s);
                for(int i=0;i<values.length;i++){
                    try {
                        values[i]=new String(values[i].getBytes("iso8859-1"),"utf-8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            flag=false;
        }
        return map;
    }

}

6.解决MySQL数据库的中文乱码问题

jdbc:MYSQL://localhost/test?user=root&password=123456
&useUnicode=true&characterEncoding=utf-8

展开阅读全文

没有更多推荐了,返回首页