Response输出字节流

文章目录

  • 代码
  • 补充

代码

  • 流程
  1. 设置编码resp.setContentType("text/html;charset=utf-8");
  2. 获取字节输出流ServletOutputStream outputStream = resp.getOutputStream();
  3. 输出数据outputStream.write("你好".getBytes("utf-8"));
@WebServlet("/responseDemo4")
public class ResponseDemo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //设置编码
        resp.setContentType("text/html;charset=utf-8");
        //获取字节输出流
        ServletOutputStream outputStream = resp.getOutputStream();
        //输出数据
        outputStream.write("你好".getBytes("utf-8"));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}

补充

  • 字节输出流一般输出图片,视频等文件,不输出文字。文字用字符输出流

你可能感兴趣的:(javaWeb基础,servlet,java,开发语言)