您的位置:首页 > 编程语言 > PHP开发

responseoutputstream的输出问题

2016-08-19 19:37 197 查看
在客户端那边打印中文

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)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息