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);
}
}