使用GZIP压缩接口的报文,达到节省流量的目的。
来源:互联网 发布:djvu for mac 编辑:程序博客网 时间:2024/06/01 18:12
GzipUtil 压缩和加压工具
/** * 压缩字符串 * @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);}}
/** * 解压缩字符串 * @param str * @param charest * @return * @throws IOException * @throws UnsupportedEncodingException * @throws FileNotFoundException */public static byte[] uncompress(InputStream in)throws IOException, UnsupportedEncodingException,FileNotFoundException {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压缩数据后 使用HttpServletResponse response 输出数据
byte[] _byte=GzipUtil.compress(json,"UTF-8");response.getOutputStream().write(_byte);response.getOutputStream().flush();response.getOutputStream().close();
0 0
- 使用GZIP压缩接口的报文,达到节省流量的目的。
- 如何达到SEO的目的!?? 。
- 如何使用泛型达到代码重用的目的
- 如何使用CSS达到段落开头空格的目的
- github + jenkins 如何不使用验证达到触发的目的
- 使用objcopy 弱化符号,以达到替换函数的目的
- gzip所使用压缩算法的基本原理
- ajax的jsonp使用gzip压缩
- 使用filter实现网站的gzip压缩
- android接口中json数据的传输中使用gzip压缩
- 删除登录信息,达到注销的目的
- tomcat的gzip压缩
- pythn的gzip压缩
- RestTemplate使用gzip压缩调用接口
- APP HTTP接口报文的加密与压缩
- APP HTTP接口报文的加密与压缩
- php开启gzip压缩节省带宽
- [笔记]经验分享:流量大的网站可以采用静态gzip压缩某些页面及文件
- 点滴感悟
- "黑马程序员" Eclipse的基本使用
- HDU 1276 士兵队列训练问题
- 为什么要在编译内核进行initrd RAMdisk这一步?
- SpringMVC生成JSON(三)
- 使用GZIP压缩接口的报文,达到节省流量的目的。
- __FUNCTION__, __LINE__ 有助于debug的宏定义
- You can Solve a Geometry Problem too 7.1.2
- IP地址分类以及C类IP地址的子网划分
- 线程异常:undefined reference to 'pthread_create' 处理
- 工程模式(一)简单工厂模式
- Android面试题
- test blog
- 汇编语言09——Call和Ret指令