• 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"); }
  • 相关阅读:
    nfs共享目录及sersync实时同步
    rsync备份
    MySQL基础操作
    源码包安装MySQL
    二进制安装MySQL
    Centos6防火墙-iptables版
    linux系统mongdb基础(1)
    linux系统ElK基础filebeat收集日志(4)
    linux系统ElK基础(3)
    linux系统ElK基础(2)
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3388792.html
Copyright © 2020-2023  润新知