1、设置响应行
设置响应行的状态码
setStatus(int sc)
2、设置响应头
addHeader(String name, String value)
addIntHeader(String name, int value)
addDateHeader(String name, long date)
setHeader(String name, String value)
setDateHeader(String name, long date)
setIntHeader(String name, int value)
其中,add表示添加,而set表示设置
(1)重定向: 状态码:302 响应头:location 代表重定向地址
(2)定时刷新: 响应头:refresh
response.setHeader(“refresh”,”5;http://www.baidu.com”);
表示5秒后页面跳转到百度
3、设置响应体
中文乱码:
(1)Reponse缓冲区编码默认为iso8859-1,修改编码:
Response.setCharacterEncoding(“utf-8”);
(2)设置浏览器解析页面的编码,浏览器默认与Windows系统一样的编码(gbk):
response.setHeader("Content-type","text/html;charset=UTF-8");
此句模拟上面操作,修改响应头<meta charset="UTF-8">
(3)当写了第(2)可以省略(1),有了(2)Tomcat自动添加(1)功能
(4)getWriter()和getOutputStream()不能同时调用
4、文件下载
(1)用a标签直接指向服务器资源
A)、什么情况下会文件下载?
浏览器不能解析的文件
(2)解决乱码方法如下
if (agent.contains("MSIE")) {
// IE浏览器
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// 火狐浏览器
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?"
+ base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {
// 其它浏览器
filename = URLEncoder.encode(filename, "utf-8");
}
其中agent就是请求头User-Agent的值
例:
package cn.yuchao.download;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.misc.BASE64Encoder;
@WebServlet("/FileDownload")
public class FileDownload extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获得下载文件的名称---解决get提交乱码
String filename = request.getParameter("filename");
/*System.out.println(filename);
filename = new String(filename.getBytes("iso-8859-1"), "utf-8");
System.out.println(filename);*/
// 解决客户端下载乱码
String agent = request.getHeader("User-Agent");
String filenameEncoder;
if (agent.contains("MSIE")) {
// IE浏览器
filenameEncoder = URLEncoder.encode(filename, "utf-8");
filenameEncoder = filenameEncoder.replace("+", " ");
} else if (agent.contains("Firefox")) {
// 火狐浏览器
BASE64Encoder base64Encoder = new BASE64Encoder();
filenameEncoder = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {
// 其它浏览器
filenameEncoder = URLEncoder.encode(filename, "utf-8");
}
// 获得文件绝对路径
String path = this.getServletContext().getRealPath("download/" + filename);
// 设置文件类型
response.setContentType(this.getServletContext().getMimeType(filename));
// 设置客户端下载,而不是直接解析
response.setHeader("Content-Disposition", "attachment;filename=" + filenameEncoder);
// 将文件读入到response缓冲区
InputStream in = new FileInputStream(path);
ServletOutputStream out = response.getOutputStream();
int len;
byte[] by = new byte[1024];
while ((len = in.read(by)) > 0) {
out.write(by, 0, len);
}
in.close();
out.close(); // 可以不写,Tomcat会自动关闭
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}