首页 > 技术文章 > response.setContentType("text/html;charset=utf-8")后依然乱码的解决方法

qingyundian 2017-09-07 11:06 原文

从浏览器获取数据到服务器,服务器将得到数据再显示在浏览器上英文字母正常显示,中文字符乱码的问题,已经使用了

 

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

成功解决

 

 

 

 

 

推荐阅读