http示例代码

//下载文件
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.addHeader("content-disposition", "attachment;filename=3.jpg"); InputStream in=this.getServletContext().getResourceAsStream("/hadoop.jpg");
int len=;
byte buffer[]=new byte[];
OutputStream out=response.getOutputStream();
while((len=in.read(buffer))>){
out.write(buffer,,len);
}
} //定时刷新
private void test4(HttpServletResponse response) throws IOException {
response.setHeader("refresh", "");
String data="aaaa123rws";
response.getOutputStream().write(data.getBytes());
} //通过content-type头,通知浏览器以何种方式解析文件
public void test3(HttpServletResponse response) throws IOException {
response.addHeader("content-type", "image/jpeg");
InputStream in=this.getServletContext().getResourceAsStream("/hadoop.jpg");
int len=;
byte buffer[]=new byte[];
OutputStream out=response.getOutputStream();
while((len=in.read(buffer))>){
out.write(buffer,,len);
}
} //数据压缩
public void test2(HttpServletResponse response) throws IOException {
String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
System.out.println("原始数据大小:"+data.getBytes().length);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(bout);
gout.write(data.getBytes());
gout.close(); byte gzis[] = bout.toByteArray();// 得到压缩后的数据
System.out.println("压缩后大小:"+gzis.length);
response.setHeader("content-Encoding", "gzip");
response.setHeader("Content-Length", gzis.length+"");
response.getOutputStream().write(gzis);
} public void test1(HttpServletResponse response) {
response.setStatus();
response.addHeader("location", "http://www.baidu.com");
}
上一篇:MongoDB相关资料收集


下一篇:[No0000B9]C# 类型基础 值类型和引用类型 及其 对象复制 浅度复制vs深度复制 深入研究2