Public void dofilter (servletrequest req, servletresponse resp,
Filterchain chain) throws ioexception, servletexception {
Httpservletrequest request = (httpservletrequest) req;
Httpservletresponse response = (httpservletresponse) resp;
Gziphttpservletresponse gresponse = new gziphttpservletresponse (response );
Chain. dofilter (request, gresponse); // allow
// Write the compressed code here
// Find a buffer byte stream in the memory
Bytearrayoutputstream baos = new bytearrayoutputstream ();
// Compress data into a buffer byte stream
Gzipoutputstream gout = new gzipoutputstream (baos );
// Retrieve data: Compressed
Byte B [] = gresponse. getoldbytes (); // original bytes
System. Out. println ("original data size:" + B. Length );
Gout. Write (B );
Gout. Close (); // ensure that all data enters the memory cache stream
// Extract the compressed data
B = baos. tobytearray ();
System. Out. println ("compressed data size:" + B. Length );
// Inform the client of the compression method before output
Response. setheader ("content-encoding", "gzip ");
Response. setcontentlength (B. Length); // tell the client body size
// Output with the response object of the server
Servletoutputstream out = response. getoutputstream ();
Out. Write (B );
}
Public void Init (filterconfig) throws servletexception {
}
}
Class gziphttpservletresponse extends httpservletresponsewrapper {
Private bytearrayoutputstream baos = new bytearrayoutputstream ();
Private printwriter PW;
Public gziphttpservletresponse (httpservletresponse response ){
Super (response );
}
// Encapsulate the raw data into a buffer stream
@ Override
Public servletoutputstream getoutputstream () throws ioexception {
Return new myservletoutputstream (baos );
}
// Volume stream: encapsulate raw data into a buffer stream
@ Override
Public printwriter getwriter () throws ioexception {
PW = new printwriter (New outputstreamwriter (baos, super. getcharacterencoding (); // bytes streams converted into byte stream encoding will be lost
Return PW;
}
// Return the cache data in baos: original
Public byte [] getoldbytes (){
Try {
If (PW! = NULL ){
PW. Close ();
}
Baos. Flush ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Return baos. tobytearray ();
}
}
Class myservletoutputstream extends servletoutputstream {
Private bytearrayoutputstream baos;
Public myservletoutputstream (bytearrayoutputstream baos ){
This. baos = baos;
}
@ Override
Public void write (int B) throws ioexception {
Baos. Write (B );
}
}
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