/*===================================压缩数据=======================================*/
*1、使用GZIPOutputStream流来压缩数据
*2、设置响应头Content-Encoding来告诉浏览器,服务器发送回来的数据压缩后的格式
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "aaaaaaaaaaaaaaaaaaaaaaaaa" +
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +
"cccccccccccccccccccccccccccccccccccccc" +
"ddddddddddddddddddddddddddddddddd" +
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" +
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" +
"ggggggggggggggggggggggggggggggggg" +
"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh";
System.out.println("原始数据的大小为:" + data.getBytes().length);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(bout); //buffer
gout.write(data.getBytes());
gout.close();
//得到压缩后的数据
byte g[] = bout.toByteArray();
response.setHeader("Content-Encoding", "gzip");
response.setHeader("Content-Length",g.length +"");
response.getOutputStream().write(g);
}
/*===================================将图片输出到页面====================================*/
/*===================================将图片输出到页面====================================*/
/**
* 浏览器能接收(Accept)的数据类型有:
* application/x-ms-application,
* image/jpeg,
* application/xaml+xml,
* image/gif,
* image/pjpeg,
* application/x-ms-xbap,
* application/vnd.ms-excel,
* application/vnd.ms-powerpoint,
* application/msword,
*///使用content-type响应头指定发送给浏览器的数据类型为"image/jpeg"
response.setHeader("content-type", "image/jpeg");//使用content-type响应头指定发送给浏览器的数据类型为"image/jpeg"
//读取位于项目根目录下的img文件夹里面的WP_20131005_002.jpg这张图片,返回一个输入流
InputStream in = this.getServletContext().getResourceAsStream("/img/WP_20131005_002.jpg");
byte buffer[] = new byte[1024];
int len = 0;
OutputStream out = response.getOutputStream();//得到输出流
while ((len = in.read(buffer)) > 0) {//读取输入流(in)里面的内容存储到缓冲区(buffer)
out.write(buffer, 0, len);//将缓冲区里面的内容输出到浏览器
}
}
/*===================================页面定时跳转=======================================*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
/*===================================让浏览器下载文件====================================*/
/*===================================页面定时跳转=======================================*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setHeader("refresh", "3");//设置refresh响应头,让浏览器每隔3秒定时刷新
response.setHeader("refresh", "3;url='http://www.baidu.com'"); //设置refresh响应头,让浏览器3秒后跳转到http://www.baidu.com
response.getWriter().write("gacl");
}
/*===================================让浏览器下载文件====================================*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 设置content-disposition响应头,让浏览器下载文件
*/
response.setHeader("content-disposition", "attachment;filename=xxx.jpg");
InputStream in = this.getServletContext().getResourceAsStream("/img/1.jpg");
byte buffer[] = new byte[1024];
int len = 0;
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}