responseoutputstream的输出问题

来源:互联网 发布:铁塔平台 登录域名 编辑:程序博客网 时间:2024/05/22 10:49
在客户端那边打印中文
package response;


import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;


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


public class ResponseDemo1 extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "中国";

response.setHeader("Content-type","text/html;charset = UTF-8");//发送一个相应头来控制客户端浏览器该用那种方式打开
OutputStream out = response.getOutputStream();
out.write(data.getBytes("UTF-8"));// 保险一点
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


doGet(request, response);
}


}




2.还有一种方法是通过html中的<meta>标签来控制浏览器的编码方式
private void test2(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
String data = "中国";
OutputStream out = response.getOutputStream();
out.write("<mate http-equiv='content-type' content = 'text/html;charset = UTF-8'>".getBytes());
out.write(data.getBytes("UTF-8"));
}


3.其实做下载挺简单的(写错了就下载)
private void test1(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
String data = "中国";


response.setHeader("Content-type", "text/html,charset = UTF-8");// 把分号写成了逗号
OutputStream out = response.getOutputStream();
out.write(data.getBytes("UTF-8"));// 保险一点
}




5.输出数字的问题
“1”和1 完全是两回事,我们想看到数字1我们需要写的是“1”(字符1)。

0 0