Gziputil compression and pressurization tools
/*** Compressed string ** @ Param Str * @ Param Charest * @ return * @ throws ioexception * @ throws unsupportedencodingexception */public static byte [] compress (string STR, string charset) throws ioexception, unsupportedencodingexception {assert. notnull (STR, "null compress error"); bytearrayoutputstream out = new bytearrayoutputstream (); gzipoutputstream gzip = new gzipoutputstream (out); try {gzip. write (Str. getbytes (charset); gzip. close (); Return out. tobytearray ();} catch (exception e) {Throw new ioexception (E);} finally {fileutil. close (gzip); fileutil. close (out );}}
/*** Extract the string * @ Param Str * @ Param Charest * @ return * @ throws ioexception * @ throws unsupportedencodingexception * @ throws filenotfoundexception */public static byte [] uncompress (inputstream in) throws ioexception, timeout, bytes {bytearrayoutputstream out = new bytearrayoutputstream (); gzipinputstream gunzip = new gzipinputstream (in); try {byte [] buffer = new byte [256]; int N; while (n = gunzip. read (buffer)> = 0) {out. write (buffer, 0, n);} return out. tobytearray ();} catch (exception e) {Throw new ioexception (E);} finally {fileutil. close (out); fileutil. close (gunzip); fileutil. close (in );}}Controller compresses data and uses httpservletresponse response to output data
byte[] _byte=GzipUtil.compress(json,"UTF-8");response.getOutputStream().write(_byte);response.getOutputStream().flush();response.getOutputStream().close();