從瀏覽器獲取數據到服務器,服務器將得到數據再顯示在瀏覽器上英文字母正常顯示,中文字符亂碼的問題,已經使用了
response.setContentType("text/html;charset=utf-8");
將瀏覽器編碼設置為utf-8,但依然亂碼
源碼如下:
package com.swift; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson; @WebServlet("/add") public class ServletAdd extends HttpServlet { private static final long serialVersionUID = 1L; public ServletAdd() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); int id=Integer.parseInt(request.getParameter("id")); String name=request.getParameter("name"); int age=Integer.parseInt(request.getParameter("age")); Student st=new Student(id,name,age); Gson gson=new Gson(); String json=gson.toJson(st); response.setContentType("text/html;charset=utf-8");//這句使用無效,沒有解決亂碼問題 response.getWriter().append(json); writeToFile(new File("d:/student.json"),json); } private void writeToFile(File file,String json) { PrintWriter pw = null; try { try { pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file,true),"utf-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } pw.println(json); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { pw.close(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
解決過程
通過代碼查看當前電腦的編碼類型
System.out.println(System.getProperty("file.encoding"));
System.out.println(Charset.defaultCharset());
上面兩種方法哪個都可以。
得知編碼類型為GBK,所以將代碼改為response.setContentType("text/html;charset=GBK");
但還是亂碼,編碼改正還是沒有成功。
需要知道的注意事項有下面幾個:
(1)、如果服務端設置編碼格式為utf-8,使用的語句 response.setCharacterEncoding("utf-8");
而瀏覽器端我們查到的編碼是GBK,那么一定會亂碼,如下圖
(2)、方法一,這時在得知瀏覽器端為GBK的情況,我們只要設置服務器端編碼也為GBK,就可以了,使用語句如下:
response.setCharacterEncoding("utf-8");
但要注意這句代碼一定要放在盡可能的前邊,否則會和前邊一樣無效。
(3)、方法二,也可以通通都改成utf-8(就是瀏覽器端和服務器端同時設置成utf-8),代碼如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");//第一句,設置服務器端編碼
response.setContentType("text/html;charset=utf-8");//第二句,設置瀏覽器端解碼
response.getWriter().append("Served at: ").append(request.getContextPath());//這句沒用
int id=Integer.parseInt(request.getParameter("id"));
String name=request.getParameter("name");
int age=Integer.parseInt(request.getParameter("age"));
Student st=new Student(id,name,age);
Gson gson=new Gson();
String json=gson.toJson(st);
response.getWriter().append(json);
writeToFile(new File("d:/student.json"),json);
}
成功解決