• Http请求和响应应用


     1 //以下载方式打开资源
     2     public void test4(HttpServletResponse response) throws IOException {
     3         response.setHeader("content-disposition", "attachment;filename=1.bmp");
     4         InputStream in = this.getServletContext().getResourceAsStream("/1.bmp");
     5         int len = 0;
     6         byte buffer[] = new byte[1024];
     7         OutputStream out = response.getOutputStream();
     8         while((len=in.read(buffer))>0){
     9             out.write(buffer, 0, len);
    10         }
    11     }
    12     //设置响应头,每隔3秒刷新一次
    13     public void test3(HttpServletResponse response) throws IOException {
    14         response.setHeader("refresh", "3");//可以用在股票里
    15         //response.setHeader("refresh", "3:url='http://www.sina.com'");3秒之后刷新到新浪上面去
    16         //在论坛上,会说3秒后去哪,就是用到这个技术。
    17         String data = "aaaaaaaaaaaaa";//服务器从浏览器得到的数据
    18         response.getOutputStream().write(data.getBytes());
    19     }
    20     //通过content-type字段控制浏览器以哪种方式打开数据
    21     public void test2(HttpServletResponse response) throws IOException {
    22         response.setHeader("content-type", "image/bmp");
    23         InputStream in = this.getServletContext().getResourceAsStream("/1.bmp");
    24         int len = 0;
    25         byte buffer[] = new byte[1024];
    26         OutputStream out = response.getOutputStream();
    27         while((len=in.read(buffer))>0){
    28             out.write(buffer, 0, len);
    29         }
    30     }
    31     
    32     //压缩数据输出
    33     public void test1(HttpServletResponse response) throws IOException {
    34         String data="aaaaaaaaa";
    35         ByteArrayOutputStream bout = new ByteArrayOutputStream();
    36         GZIPOutputStream gout = new GZIPOutputStream(bout);
    37         //ByteArrayOutputStream叫底层流,GZIPOutputStream包装流,包装流会有一个缓冲的,调用包装流写数据的时候,
    38         //如果数据量没有把包装流的缓冲写满,他是不会往底层流里面写的,为了确保一定能够取到数据,添加gout.close();
    39         gout.write(data.getBytes());
    40         gout.close();
    41         byte gzip[] = bout.toByteArray();//得到压缩后的数据
    42         //给浏览器之后浏览器不知道是压缩数据,一定要通过设置响应头,通知浏览器数据采用的压缩格式,不然会出现错误
    43         response.setHeader("Content-Encoding", "gzip");
    44         response.setHeader("Content-Length", gzip.length+"");
    45         response.getOutputStream().write(gzip);
    46     }
    47 //实现断点下载,请求里面包含range头
    48     public static void test6() throws MalformedURLException, IOException,
    49             FileNotFoundException {
    50         //已接受aaaaa  源文件aaaaaaaaaaaaaaaaaaaaa
    51         URL url = new URL("http://localhost:8080/day05/a.txt");
    52         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    53         conn.setRequestProperty("Range", "bytes=5-");
    54         
    55         InputStream in = conn.getInputStream();
    56         
    57         int len=0;
    58         byte buffer[] = new byte[1024];
    59         FileOutputStream out = new FileOutputStream("c:\a.txt",true);//追加
    60         while((len=in.read(buffer))>0){
    61             out.write(buffer, 0, len);
    62         }
    63         in.close();
    64         out.close();
    65     }
  • 相关阅读:
    git项目管理-合并请求
    记录一次git stash找回删除的存储
    chrome 下 position:fixed失效(react)
    css3 var变量
    rc-select下拉选择控件库推荐
    (转载)vue路径后面去除#号
    本地配置独立域名环境
    javascript判断pc还是手机端
    javascript复制到粘贴板的方案
    javascript轮播插件的使用(TouchSlide)
  • 原文地址:https://www.cnblogs.com/liun1994/p/3843594.html
Copyright © 2020-2023  润新知