HttpServletResponse设置响应体响应字符数据
PrintWriter getWriter():获取字符输出流,可以响应字符数据
例子: response.getWriter().write("<h1>HelloWorld!<h1>") ;
第一种形式:
esponse.setCharacterEncoding("UTF-8");//获取这个流对象之前设置一个编码方式 response.setHeader("content-type", "text/html;charset=UTF-8") ;// 响应头content-type response.getWriter().write("你好啊啊啊");
第二种形式:
response.setContentType("text/html;charset=utf-8") ; response.getWriter().write("你好") ;
HttpServletResponse设置响应体响应字节数据
ServletContext context = getServletConfig().getServletContext(); String path = context.getRealPath("/WEB-INF/classes/mv.jpg"); FileInputStream fis = new FileInputStream(path); ServletOutputStream stream = response.getOutputStream(); IOUtils.copy(fis, stream) ;//借助commons-io-2.0.jar和commons-fileupload-1.2.1.jar