码迷,mamicode.com
首页 > 其他好文 > 详细

servlet从服务器磁盘文件读出到浏览器显示,中文乱码问题,不要忘记在输入流和输出流都要设置编码格式,否则一个地方没设置不统一就会各种乱码

时间:2017-09-07 13:25:40      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:inpu   UI   etc   浏览器   https   imp   set   red   dog   

package com.swift;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/show")
public class fromJsonFile extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public fromJsonFile() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;utf-8");
        BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("d:/student.json"),"utf-8"));//这里不要忘记设置编码
        String str;
        StringBuffer sb=new StringBuffer();
        while((str=br.readLine())!=null) {
            sb.append(str);
            sb.append("\r\n");
        }
        br.close();
        response.getWriter().println(sb.toString());
        System.out.println(sb.toString());
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

 

servlet从服务器磁盘文件读出到浏览器显示,中文乱码问题,不要忘记在输入流和输出流都要设置编码格式,否则一个地方没设置不统一就会各种乱码

标签:inpu   UI   etc   浏览器   https   imp   set   red   dog   

原文地址:http://www.cnblogs.com/qingyundian/p/7488963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!