中文亂碼的解決方案:

1.以POST請求方式:

在獲取請求參數之前設置: request.setCharacterEncoding(“utf-8”);
設置輸出編碼:
或者<%@ page> contentType=”text/html;charset=utf-8” %>

2.以GET方式請求的

以GET方式請求的設置setCharacterEncoding是無效的。
我們需要在獲取參數後,對每一個參數進行單獨轉碼。
String name =request.getParameter(“name”);
name=new String(name.getBytes(“ISO-8859-1”),”utf-8”);

3.在數據庫中的亂碼:
設置數據庫的編碼爲utf-8

4.使用過濾器解決中文亂碼問題:

a.使用動態代理可以解決。本人親測,直接上代碼;

package cn.itcast.filter;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

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.HttpServletResponse;

public class EncodeFilter implements Filter {

    public void destroy() {

    }

    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        System.out.println("-----------進入了過濾器------------");
        // 轉換
        final HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        // 設置post編碼解決
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        final String method1 = request.getMethod();
        System.err.println("訪問方式:"+method1);

        // 動態代理
        HttpServletRequest proxy = (HttpServletRequest) Proxy.newProxyInstance(
                request.getClass().getClassLoader(),
                new Class[] { HttpServletRequest.class },
                new InvocationHandler() {

                    public Object invoke(Object proxy, Method method,
                            Object[] args) throws Throwable {

                        Object invoke =null;

                        if(method1.equals("GET") && "getParameter".equals(method.getName())){
                            //如果是get方式然後就
                            invoke = (String)method.invoke(request, args);
                            invoke = new String(((String) invoke).getBytes("ISO-8859-1"),"utf-8");
                            System.out.println("轉換後的"+invoke);

                        }else{
                            invoke = method.invoke(request, args);
                        }
                        return invoke;
                    }
                });

        // 放行
        chain.doFilter(proxy, response);

    }

    public void init(FilterConfig arg0) throws ServletException {

    }

}

b.還有一種就是網上很多的重寫getParameter方法,有空過來補上;

5.上傳與下載的亂碼或中文不顯示問題解決:

JSP或Servlet上傳文件中文名亂碼解決:

String fileN=item.getName();
//String fileName=new String(fileN.getBytes("ISO-8859-1"),"utf-8");
String fileName=new String(fileN.getBytes(),"utf-8");//對提取出來的文件名進行UTF-8編碼,

以上可以解決上傳文件名亂碼和瀏覽器顯示以及存入數據庫都顯示正常。
解決下載文件時中文不顯示的問題:

response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename="+
new String(fileName.getBytes("utf-8"),"ISO-8859-1"));

原因分析,由於HTTP頭部的默認編碼爲ISO-8859-1而我們上傳文件於下載文件過程中,提取到的文件名都要通過HTTP頭部。
所以我們需要在上傳的時候對提取到的文件名進行轉碼爲UTF-8,然後在下載時我們也要進行反向轉碼爲ISO-8859-1.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章