Java gzip Data compression transfer to the foreground __ data compression

Source: Internet
Author: User

Features: Using Sevrlet bar data compression in the transfer to the foreground

Package cn.hncu.img;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
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 Gzipservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Respo
    NSE) throws Servletexception, IOException {doPost (request, response); /** * The DoPost method of the servlet.
     <br> * * This are called when a form has it tag value method equals to post. * @param request the request send by the ' client to the ' server * @param response the response send by the Serv
     ER to the client * @throws servletexception If a error occurred * @throws IOException if an error occurred */public void DopoSt (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {// Compresses data in byte array str into the BOS memory stream String str= "SSSDADASDASDASSSSDADASDASDA +" Ssssdadasdasdassssdadasdas
        Dasss "+" Sdadasdasdassssdadasdasdassssdadasdasdas ";
        Byte[] Buf=str.getbytes ();
        System.out.println ("Buf-length:" +buf.length);
        Bytearrayoutputstream bos=new Bytearrayoutputstream ();
        Gzipoutputstream gout=new Gzipoutputstream (BOS);
        Gout.write (BUF);
        Gout.close ();
        Take the compressed data out of the memory stream array byte Dest[]=bos.tobytearray ();
        System.out.println ("Des-length:" +dest.length);

        Response.setheader ("content-encoding", "gzip");/tell the browser that the content currently being sent is in gzip format response.setcontenttype ("text/html");
        OutputStream Out=response.getoutputstream ();
        Out.write (SRC);
        Out.write (dest);
        Out.flush ();

    Out.close ();
 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.