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=0; byte buffer[]=new byte[1024]; OutputStream out=response.getOutputStream(); while((len=in.read(buffer))>0){ out.write(buffer,0,len); } } //定时刷新 private void test4(HttpServletResponse response) throws IOException { response.setHeader("refresh", "2"); 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=0; byte buffer[]=new byte[1024]; OutputStream out=response.getOutputStream(); while((len=in.read(buffer))>0){ out.write(buffer,0,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(302); response.addHeader("location", "http://www.baidu.com"); }

 

你可能感兴趣的:(http)