response.setContentType("text/html;charset=utf-8")後依然亂碼的解決方案,

來源:互聯網
上載者:User

response.setContentType("text/html;charset=utf-8")後依然亂碼的解決方案,

從瀏覽器擷取資料到伺服器,伺服器將得到資料再顯示在瀏覽器上英文字母正常顯示,中文字元亂碼的問題,已經使用了

 

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);
}

成功解決

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.