Java中解決Servlet獲取請求(GET/POST)資料時資料亂碼問題
阿新 • • 發佈:2019-02-05
一:Servlet獲取請求資料時亂碼問題解決方法
<1> get方式資料亂碼只能使用下面的方法解決
String username = request.getParameter("username");
username = new String(username.getBytes("ISO-8859-1") , "utf-8")
<2.1> post方式既可以使用
String username = request.getParameter("username");
username = new String(username.getBytes("ISO-8859-1") , "utf-8");
<2.2> 也可以使用
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
<3>但對於request.getReader()來說
BufferedReader reader = new BufferedReader(request.getReader());
String line = reader.readLine();
line = URLDecoder.decode(line, "utf-8");