Java EE------gzip Full station compression

Source: Internet
Author: User

Gzip Full Station compression

Send compressed text files to the browser to reduce traffic

conditions for gzip compression :

Set header File protocol:

Myresp.setheader ("content-encoding", "gzip"); Myresp.setcontentlength (src.length);
Two main classes: Bytearrayoutputstream,gzipoutputstream

Using packing mode to strengthen Respone


Main process:

There are two kinds of streams, byte stream outputstream and character stream PrintWriter

We need to strengthen these two streams within the Respone object (that is, to modify the write operations of both streams using the packaging design pattern)


Filter:

After the response is loaded, it is uploaded to the background to read the data, all in the bytearrayoutputstream we define , and then we take out the data to compress it. Use original send to front desk.

public void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {httpservletresponse resp= (httpservletresponse) response; Myresponse myresp =new myresponse (RESP);//Use the enhanced class to pass through, write directly in the inside Chain.dofilter (request, MYRESP);//compress on return.// First obtain the source data Bytearrayoutputstream bout=myresp.getbout ();//convert byte data----zip write requires byte[] Src=bout.tobytearray (); System.out.println ("Pre-compression" +src.length);//Statistic Quantity Bytearrayoutputstream baout =new bytearrayoutputstream (); Gzipoutputstream zip =new gzipoutputstream (baout);//The source data is compressed and written to baout in the memory stream Zip.write (SRC);//Close stream zip.close ();// Set HTTP protocol myresp.setheader ("Content-encoding", "gzip"); Myresp.setcontentlength (src.length); byte[] Desc= Baout.tobytearray (); System.out.println ("after Compression" +desc.length), OutputStream out =resp.getoutputstream ();//character stream compression must be written out.write (DESC) ;}

Strong class Myresponse (packing mode)

The Getoutputstream () and the Getwriter () of the character stream, respectively, of the byte flow function

This is an enhanced class that must consist of two character stream and byte streams class Myresponse extends httpservletresponsewrapper{private printwriter pw; Bytearrayoutputstream bout;public Myresponse (httpservletresponse response) {super (response);} Character Stream, byte write a class, write the write function yourself, write to bout public Servletoutputstream getoutputstream () throws IOException {Bout=new Bytearrayoutputstream ();//need a new container every time. return new Myout (bout);} Character stream processing, character stream and byte different, character stream can implement IO direct socket, cannot write public printwriter getwriter () throws IOException {Bout=new Bytearrayoutputstream ();//need a new container every time. pw= New PrintWriter (New OutputStreamWriter (bout, "Utf-8"), true);//directly through the stream socket write to the memory stream, than the byte stream simple return PW;} Finally, you need to return this container to public bytearrayoutputstream getbout () {if (pw!=null) {pw.close ();//The character stream must be refreshed, otherwise the data}return bout;}}

Byte streamWrite write function required from

Construct the bout, and pass it to this side for write operation.

/This is the write function of the byte stream,, because the printwriter inside can carry on the flow socket, therefore does not have to write one oneself writes the class Myout extends Servletoutputstream {bytearrayoutputstream bout ;p ublic myout (Bytearrayoutputstream bout) {this.bout=bout;} @Overridepublic void Write (int b) throws IOException {bout.write (b);//need to write to memory stream}}


In the Serlvet of hyperlinks we used a test case

public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { String str= "Deep pride is happy to die happy spoiled the happy button happy to get along with the card girl you said v saw but that is the card what is poor that's what the garbage is coming to ASD surrey See you see more than three times Oscar how Long";//casually input .... Response.getwriter (). write (str);/*outputstream out=response.getoutputstream (); byte[] b =str.getbytes ("Utf-8"); o Ut.write (b);//Byte character can be */printwriter pw =response.getwriter ();p w.write (str);}

When the boot server is, it will intercept the compression, index.jsp



accessing hyperlinks to the servlet also compresses

The greater the efficiency of compression, the higher the compression efficiency ...

Above is the filter needs to implement the function, can be through the byte, can also filter the characters, you can achieve the compression of the entire station function, specifically need to compress what, you can go to the Web. XML configuration. (I am here to intercept all)

Java EE------gzip Full station compression

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.