1.使用response处理中文乱码
package com.hbsi.response;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Response1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test5(response);
}
// 没有乱码,个体Bytes()默认是GB2312,浏览器默认也是GB2312
private void test1(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
String str = "河北";
OutputStream out = response.getOutputStream();
out.write(str.getBytes());
}
// 有乱码,因此要进行处理
private void test2(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
String str = "河北";
// 发送消息头,通知浏览器以UTF-8打开内容
// response.setHeader("Content-Type","text/html;charset=UTF-8");
response.setContentType("text/html;charset=UTF-8");
OutputStream out = response.getOutputStream();
out.write(str.getBytes("UTF-8"));
}
// 不发送http头,而是发送了一段html内容来控制浏览器以utf-8打开。
private void test3(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
String str = "河北";
// 发送消息头,通知浏览器以UTF-8打开内容
OutputStream out = response.getOutputStream();
out
.write("<meta http-equiv='content-type' content='text/html; charset=UTF-8'>"
.getBytes());
out.write(str.getBytes("UTF-8"));
}
// 采用字符输出流,response默认的编码是iso8859-1,
private void test4(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
String str = "河北";
// 把response的编码改为UTF-8;
// response.setCharacterEncoding("UTF-8");
// 告诉浏览器,以UTF-8显示
// response.setHeader("Content-Type","text/html;charset=UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(str);
}
//课后思考!!
private void test5(HttpServletResponse response) throws IOException,
UnsupportedEncodingException {
//response.getWriter().write(14);
response.getOutputStream().write(14);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2.使用response实现文件下载
package com.hbsi.response;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Response2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.获取要下载的资源
String path = this.getServletContext().getRealPath("/download/视频1.avi");
String filename = path.substring(path.lastIndexOf("\\")+1);
//System.out.println(filename);
//2.通知浏览器以下载方式打开发送过来的数据
//response.setHeader("content-disposition","attachment;filename="+filename);
//如果文件名是中文,要经过URL编码
response.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(filename,"UTF-8"));
//如果文件名是中文,要经过URL编码
//3.读取资源内容
FileInputStream fis = new FileInputStream(path);
byte[] buffer = new byte[1024];
int len = 0;
while((len=fis.read(buffer))>0){
response.getOutputStream().write(buffer,0,len);
}
fis.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}