HttpServletResponse
负责向客户端(浏览器)发送数据的相关方法
负责向客户端(浏览器)发送响应头的相关方法
负责向客户端(浏览器)发送响应状态码的相关方法
使用PrintWriter向客户端浏览器输出中文数据
response.setCharacterEncoding("UTF-8");设置将字符以"UTF-8"编码输出到客户端浏览器,然后再使用 response.getWriter();获取PrintWriter输出流,这两个步骤不能颠倒。
//通过设置响应头控制浏览器以UTF-8的编码显示数据,如果不加这句话,那么浏览器显示的将是乱码
response.setHeader("content-type", "text/html;charset=UTF-8");
//response.setContentType("text/html;charset=UTF-8");
//response.setContentType("text/html");response.setCharacterEncoding("UTF-8");
//response.setCharacterEncoding("UTF-8");response.getWriter().write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'/>");
response.getWriter().println("乱码?");
客户端浏览器接收到数据后,就按照响应头上设置的字符编码来解析数据,如下所示:
setCharacterEncoding
public void setCharacterEncoding(java.lang.String charset)
- Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by
setContentType(java.lang.String)orsetLocale(java.util.Locale), this method overrides it. CallingsetContentType(java.lang.String)with theStringoftext/htmland calling this method with theStringofUTF-8is equivalent with callingsetContentTypewith theStringoftext/html; charset=UTF-8.This method can be called repeatedly to change the character encoding. This method has no effect if it is called after
getWriterhas been called or after the response has been committed.Containers must communicate the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the character encoding is communicated as part of the
Content-Typeheader for text media types. Note that the character encoding cannot be communicated via HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the servlet response's writer. - Parameters:
charset- a String specifying only the character set defined by IANA Character Sets (http://www.iana.org/assignments/character-sets)- Since:
- 2.4
- See Also:
#setLocale
使用OutputStream向客户端浏览器输出中文数据
/**使用OutputStream输出中文注意问题: * 在服务器端,数据是以哪个码表输出的,那么就要控制客户端浏览器以相应的码表打开, * 比如:outputStream.write("中国".getBytes("UTF-8"));//使用OutputStream流向客户端浏览器输出中文,以UTF-8的编码进行输出 * 此时就要控制客户端浏览器以UTF-8的编码打开,否则显示的时候就会出现中文乱码,那么在服务器端如何控制客户端浏览器以以UTF-8的编码显示数据呢? * 可以通过设置响应头控制浏览器的行为,例如: * response.setHeader("content-type", "text/html;charset=UTF-8");//通过设置响应头控制浏览器以UTF-8的编码显示数据 */ String data = "中国"; OutputStream outputStream = response.getOutputStream();//获取OutputStream输出流 response.setHeader("content-type", "text/html;charset=UTF-8");//通过设置响应头控制浏览器以UTF-8的编码显示数据,如果不加这句话,那么浏览器显示的将是乱码 /** * data.getBytes()是一个将字符转换成字节数组的过程,这个过程中一定会去查码表, * 如果是中文的操作系统环境,默认就是查找查GB2312的码表, * 将字符转换成字节数组的过程就是将中文字符转换成GB2312的码表上对应的数字 * 比如: "中"在GB2312的码表上对应的数字是98 * "国"在GB2312的码表上对应的数字是99 */ /** * getBytes()方法如果不带参数,那么就会根据操作系统的语言环境来选择转换码表,如果是中文操作系统,那么就使用GB2312的码表 */ byte[] dataByteArr = data.getBytes("UTF-8");//将字符转换成字节数组,指定以UTF-8编码进行转换 outputStream.write(dataByteArr);//使用OutputStream流向客户端输出字节数组
文件下载
文件下载功能是web开发中经常使用到的功能,使用HttpServletResponse对象就可以实现文件的下载
文件下载功能的实现思路:
1.获取要下载的文件的绝对路径
2.获取要下载的文件名
3.设置content-disposition响应头控制浏览器以下载的形式打开文件
4.获取要下载的文件输入流
5.创建数据缓冲区
6.通过response对象获取OutputStream流
7.将FileInputStream流写入到buffer缓冲区
8.使用OutputStream将缓冲区的数据输出到客户端浏览器
String realPath = this.getServletContext().getRealPath("/download/张家界国家森林公园.JPG");//获取要下载的文件的绝对路径 String fileName = realPath.substring(realPath.lastIndexOf("\\")+1);//获取要下载的文件名 //设置content-disposition响应头控制浏览器以下载的形式打开文件,中文文件名要使用URLEncoder.encode方法进行编码 response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8")); FileReader in = new FileReader(realPath); int len = 0; char[] buffer = new char[1024]; PrintWriter out = response.getWriter(); while ((len = in.read(buffer)) > 0) { out.write(buffer,0,len);//将缓冲区的数据输出到客户端浏览器 } in.close();
浙公网安备 33010602011771号