Post 和 get 請求亂碼問題處理

2021-08-28 12:50:32 字數 1664 閱讀 3560

原始處理get post 請求亂碼

string newencoding = new string( params.getbytes("iso-8859-1") , "utf-8" );
原理分析

post 處理亂碼方式

req.setcharacterencoding("utf-8");

resp.setheader("content-type", "text/html;charset=utf-8");

動態**引數講解 

動態**的**都是固定的

public static object newproxyinstance(classloader loader,

class> inte***ces,

invocationhandler h)

引數1: 動態建立的不在虛擬機器中 需要使用類載入器 載入動態建立的類 當類.class.getclassloader()

引數2: 動態建立的類 裡面沒有方法 所以需要方法 方法在介面中 目標的介面 目標類例項.getclass().getinte***ce()

引數3: 執行處理類 對方法進行實現 每一次 呼叫目標類的方法 執行處理類的invoke方法都會被呼叫一次

invoke(object proxy, method method, object args)

引數1: **類的物件(沒用)

引數2: 執行的方法

引數3: 引數

get 處理亂碼方式

public void dofilter(servletrequest req, servletresponse resp, filterchain chain) throws ioexception, servletexception 

//增強 //獲得以前亂碼的引數

string parameter = request.getparameter((string) args[0]);

return new string(parameter.getbytes("iso-8859-1") , "utf-8");}}

return method.invoke(request, args);

}});

chain.dofilter(myrequest, response);

} catch (exception e)

}

spring mvc 的 web.xml檔案

characterencodingfilter  

org.springframework.web.filter.characterencodingfilter  

encoding  

utf-8  

forceencoding  

true  

/*

spring boot 的 pom.xml 檔案

utf-8

utf-8

1.8

這篇文章做了另外一番解釋

get和post 請求亂碼問題

post 在web.xml中加入 characterencodingfilter org.springframework.web.filter.characterencodingfilter encoding utf 8 get 以上可以解決post 請求亂碼問題。對於get 請求中文引數出現亂碼解...

post和get請求中文亂碼問題

post和get是提交表單時的兩種請求方式。當瀏覽器傳送post請求時,資料以utf 8的形式儲存到http的請求體中,不用通過url傳輸。當提交表單後,解析request的預設編碼是 iso 8859 1 這就是造成亂碼的原因。request.setcharacterencoding utf 8 ...

get請求方式和post請求方式亂碼問題

一 get請求方式亂碼 1 tomcat8版本伺服器已經自動配置好亂碼問題,所以get請求方式不會出現亂碼。2 tomcat7及以下版本get會出現亂碼問題,解決辦法是比post請求方式困難。主要有兩種方式解決 第一種 找到tomcat安裝包裡面conf底下的server.xml檔案 然後進入裡面找...