• 30天轻松学习javaweb_http头信息实例


    package com.wzh.test.http;

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.util.zip.GZIPOutputStream;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class ServletDemo1 extends HttpServlet {

    /**
    * Constructor of the object.
    */
    public ServletDemo1() {
    super();
    }

    /**
    * Destruction of the servlet. <br>
    */
    public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
    }

    /**
    * The doGet method of the servlet. <br>
    *
    * This method is called when a form has its tag value method equals to get.
    *
    * @param request
    * the request send by the client to the server
    * @param response
    * the response send by the server to the client
    * @throws ServletException
    * if an error occurred
    * @throws IOException
    * if an error occurred
    */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    this.doPost(request, response);
    }

    /**
    * The doPost method of the servlet. <br>
    *
    * This method is called when a form has its tag value method equals to
    * post.
    *
    * @param request
    * the request send by the client to the server
    * @param response
    * the response send by the server to the client
    * @throws ServletException
    * if an error occurred
    * @throws IOException
    * if an error occurred
    */
    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);
    }*/

    // test1(response);
    // test2(response);
    // test3(response);
    test4(response);
    }

    //定时刷新
    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);
    }

    //通过发送302状态码,让客户端跳转
    public void test1(HttpServletResponse response) {
    response.setStatus(302);
    response.addHeader("location", "http://www.baidu.com");
    }

    /**
    * Initialization of the servlet. <br>
    *
    * @throws ServletException
    * if an error occurs
    */
    public void init() throws ServletException {
    // Put your code here
    }

    }

  • 相关阅读:
    Java蓝桥杯--基础练习(7)特殊回文数
    第九周项目1-利用循环求和
    第八周项目6-本月有几天?
    第八周项目5-定期存款利息计算器
    第八周项目4-个人所得税计算器
    第八周项目3-多分段函数求值
    第八周项目2-计算小贺的周工资
    第八周项目1-求简单分段函数的值
    玩坏的Bad Apple之DOS
    玩坏的Bad Apple之Vim
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3737951.html
Copyright © 2020-2023  润新知