The server uses Gzip to compress data and accelerate network transmission (Java example)

Source: Internet
Author: User

The next version of the Project will provide support for gzip in order to faster speed of data transmission on the network. In our project, gzip support is added to speed up data transmission over the network.

If you need to transfer data using gzip, you must be setting request header "Accept-Encoding" = "gzip ". thus, you will get a response, which include the response header name "Content-Encoding" and value "gzip", and need to ungzip the response data. besides,
The response header name "Content-Length" also will be returned. To use gzip, you must first set the request message header Accept-Encoding to gzip. In this way, you will get a response. you can know that the transmitted data is compressed by gzip according to the Content-Encoding of the message header. In addition, the Content-Length header will tell you the compressed data Length.

A example using gzip implemented by Java. Gzip implemented in Java

GetMethod method = new GetMethod (url); // generate a get method Instance. setQueryString (queryString); // sets the query string method. addRequestHeader ("Accept-Encoding", "gzip"); // you can specify gzip HttpClient client = new HttpClient () as the receiving response message (); // generate the client instance client.exe cuteMethod (method) that executes the get method; // execute the get method InputStream in = method. getResponseBodyAsStream (); // get the Response Message Body Header contentEncoding = method. getResponseHeader ("Content-Encoding"); // get the message header Conten T-Encoding determines whether the data stream is gzip compressed if (contentEncoding! = Null & contentEncoding. getValue (). equalsIgnoreCase ("gzip") {GZIPInputStream gzipIn = new GZIPInputStream (in); int len = Integer. parseInt (method. getResponseHeader ("Content-Length "). getValue (); byte [] B = new byte [len]; gzipIn. read (B); String json = new String (B); System. out. println (json );}

In addtional, give a example about Server compress the data using gzip. Example of using gzip to compress data on the server.

        byte[] result = data.getBytes("UTF-8");                if(response.getHeader("Accept-Encoding").equalsIgnoreCase("gzip"))        {            // System.out.println("Before compression, the data size is :"+ result.length);            // Using gzip compress the data            ByteArrayOutputStream out = new ByteArrayOutputStream();            GZIPOutputStream gout = new GZIPOutputStream(out);            gout.write(json.getBytes("UTF-8"));            gout.close();            result = out.toByteArray();            // System.out.println("After compression, the data size is "+gzipResult.length);            this.getResp().setHeader("Content-Encoding","gzip");            this.getResp().setHeader("Content-Length", result.length+"");        }                response.getOutputStream().write(result);

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.