IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Gzip压缩大json对象 SpringBoot+Filter -> 正文阅读

[Java知识库]Gzip压缩大json对象 SpringBoot+Filter

Gzip压缩大json对象 SpringBoot+Filter

参考:https://mp.weixin.qq.com/s/D_q17ZAsPW1eOEhUA0dYlw

简单场景

一个保存广告接口,一个很大的json对象进行传输。

存在的问题:

  1. 占用网络带宽,而有些云产品就是按照带宽来计费的,间接浪费了钱
  2. 传输数据大导致网络传输耗时

解决:

请求广告保存接口时先将Json对象字符串进行GZIP压缩,那请求时传入的就是压缩后的数据,而GZIP的压缩效率是很高的,因此可以大大减小传输数据,而当数据到达广告保存接口前再将传来的数据进行解压缩,还原成JSON对象就完成了整个GZIP压缩数据的请求以及处理流程。

确实可以解决问题,这样做法后缺点:

  • 请求变复杂了

    • 接口调用方那边需要对数据进行压缩
    • 接口执行方那边需要对拿到的数据进行解压
  • 需要额外占用更多的CPU计算资源

  • 可能会影响到原有的其他接口

解决处理

  • 对与需要占用而外的CPU计算资源来说,公司的内部系统属于IO密集型应用,因此用一些CPU资源来换取更快的网络传输其实是很划算的
  • 使用过滤器在请求数据到达Controller之前对数据进行解压缩处理后重新写回到Body中,避免影响Controller的逻辑,代码零侵入
  • 而对于改造接口的同时是否会影响到原来的接口这一点可以通过 HttpHeader 的Content-Encoding=gzip属性来区分是否需要对请求数据进行解压缩

解决具体思路

流程图:

在这里插入图片描述

核心代码:

广告参数对象实体类

@Data
public class Advertising {
    private String adName;
    private String adTag;
}

请求controller接口

@RestController
@RequestMapping("/advertising")
@Slf4j
public class AdvertisingController {

    @PostMapping("/save")
    public Advertising saveProject(@RequestBody Advertising advertising) {
        log.info("获取内容" + advertising);
        return advertising;
    }
}

过滤器

@Slf4j
@Component
public class GZIPFilter implements Filter {

    private static final String CONTENT_ENCODING = "Content-Encoding";
    private static final String CONTENT_ENCODING_TYPE = "gzip";

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long start = System.currentTimeMillis();
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;

        //获取指定的请求头 Content-Encoding
        String encodeType = httpServletRequest.getHeader(CONTENT_ENCODING);
        if (CONTENT_ENCODING_TYPE.equals(encodeType)) {
            log.info("请求:{} 需要解压", httpServletRequest.getRequestURI());
            UnZIPRequestWrapper unZIPRequestWrapper = new UnZIPRequestWrapper(httpServletRequest);
            chain.doFilter(unZIPRequestWrapper, response);
        } else {
            log.info("请求:{} 无需解压", httpServletRequest.getRequestURI());
            chain.doFilter(request, response);
        }
        log.info("耗时:{}ms", System.currentTimeMillis() - start);
    }
}

配置类

@Configuration
public class FilterRegistrationConfig {

    @Resource
    private GZIPFilter gzipFilter;

    @Bean
    public FilterRegistrationBean<GZIPFilter> gzipFilterRegistrationBean() {
        FilterRegistrationBean<GZIPFilter> registration = new FilterRegistrationBean<>();
        //Filter可以new,也可以使用依赖注入Bean
        registration.setFilter(gzipFilter);
        //过滤器名称
        registration.setName("gzipFilter");
        //拦截路径
        registration.addUrlPatterns("/*");
        //设置顺序
        registration.setOrder(1);
        return registration;
    }
}

自定义一个HttpServletRequestWrapper处理解压缩

JsonString经过压缩后保存为二进制文件 -> 解压缩后还原成JsonString转换成byte[] 写回body中
/**
 * JsonString经过压缩后保存为二进制文件 -> 解压缩后还原成JsonString转换成byte[] 写回body中
 */
@Slf4j
public class UnZIPRequestWrapper extends HttpServletRequestWrapper {

    private final byte[] bytes;

    public UnZIPRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        try (BufferedInputStream bis = new BufferedInputStream(request.getInputStream());
             ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            final byte[] body;
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) > 0) {
                baos.write(buffer, 0, len);
            }
            body = baos.toByteArray();
            if (body.length == 0) {
                log.info("Body无内容,无需解压");
                bytes = body;
                return;
            }
            this.bytes = GZIPUtils.uncompressToByteArray(body);
        } catch (IOException ex) {
            log.info("解压缩步骤发生异常!");
            ex.printStackTrace();
            throw ex;
        }
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        return new ServletInputStream() {

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }

            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }

}

GZIP工具类

package com.ung.realtime.utils;


import org.apache.commons.codec.binary.StringUtils;

import java.io.*;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPUtils {

    public static final String GZIP_ENCODE_UTF_8 = "UTF-8";

    /**
     * 字符串压缩为GZIP字节数组
     * @param str
     * @return
     */
    public static byte[] compress(String str) {
        return compress(str, GZIP_ENCODE_UTF_8);
    }

    /**
     * 字符串压缩为GZIP字节数组
     * @param str
     * @param encoding
     * @return
     */
    public static byte[] compress(String str, String encoding) {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes(encoding));
            gzip.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    /**
     * GZIP解压缩
     * @param bytes
     * @return
     */
    public static byte[] uncompress(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    /**
     * 解压并返回String
     * @param bytes
     * @return
     */
    public static String uncompressToString(byte[] bytes) throws IOException {
        return uncompressToString(bytes, GZIP_ENCODE_UTF_8);
    }

    /**
     *
     * @param bytes
     * @return
     */
    public static byte[] uncompressToByteArray(byte[] bytes) throws IOException {
        return uncompressToByteArray(bytes, GZIP_ENCODE_UTF_8);
    }

    /**
     * 解压成字符串
     * @param bytes 压缩后的字节数组
     * @param encoding 编码方式
     * @return 解压后的字符串
     */
    public static String uncompressToString(byte[] bytes, String encoding) throws IOException {
        byte[] result = uncompressToByteArray(bytes, encoding);
        return new String(result);
    }

    /**
     * 解压成字节数组
     * @param bytes
     * @param encoding
     * @return
     */
    public static byte[] uncompressToByteArray(byte[] bytes, String encoding) throws IOException {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream ungzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            return out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw new IOException("解压缩失败!");
        }
    }

    /**
     * 将字节流转换成文件
     * @param filename
     * @param data
     * @throws Exception
     */
    public static void saveFile(String filename,byte [] data)throws Exception{
        if(data != null){
            String filepath ="D:\\" + filename;
            File file  = new File(filepath);
            if(file.exists()){
                file.delete();
            }
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(data,0,data.length);
            fos.flush();
            fos.close();
            System.out.println(file);
        }
    }


    public static void main(String[] args) throws Exception {
        String str = "{\"adName\":\"ad1\",\"adTag\":\"<img src='https://ssb-use1.smartadserver.com:443/api/imp?callerid=40&amp;rtb=1&amp;rtbnid=3817&amp;rtbbid=922501523137776153&amp;rtbh=f0486d91c43d678d29edeea5b5e7b826b47b3d58&amp;rtblt=637839192022385241&amp;rtbet=2&amp;rtbptnid=101&amp;cftgid=bcfd9477a6a4' border='0' width='0' height='0' style='display:none'/><img src='https://ssb-use1.smartadserver.com:443/api/event?callerid=40&amp;actionid=10016&amp;rtb=1&amp;rtbnid=3817&amp;rtbbid=922501523137776153&amp;rtbh=f0486d91c43d678d29edeea5b5e7b826b47b3d58&amp;rtblt=637839192022385241&amp;rtbet=2&amp;rtbptnid=101' border='0' width='0' height='0' style='display:none'/><DIV STYLE=\\\\\\\"position: absolute; left: 0px; top: 0px; visibility: hidden;\\\\\\\"><IMG SRC=\\\\\\\"https://pagead2.googlesyndication.com/pagead/gen_204?id=xbid&dbm_b=AKAmf-BYOvUFzqHszVQzB8qfvmFA0KUKwEmzXHSMr8KUmpYxuVYLsSPkr5In0delLK4rykXCBFS4yQKrPsWrNam9150ZmaBRW1yFki70rB3w-x-zv5Wr0r4\\\\\\\" BORDER=0 WIDTH=1 HEIGHT=1 ALT=\\\\\\\"\\\\\\\" STYLE=\\\\\\\"display:none\\\\\\\"></DIV><iframe title=\\\\\\\"Blank\\\\\\\" src=\\\\\\\"https://googleads.g.doubleclick.net/xbbe/pixel?d=CNfM9gIQv_S38AIY0ua-xAEwAQ&v=APEucNW-zSyy3v1TPAmXsoISK1pk8YGMtgB9XFUJpb3z1nOO6Fy7RiKGaeOO87KPHmjvqC7U9QuksFqKOt21zm0Q9VgXylDjv64tF5w8sE2vqfi5By8kQK4\\\\\\\" style=\\\\\\\"display:none\\\\\\\" aria-hidden=\\\\\\\"true\\\\\\\"></iframe><div><div style=\\\\\\\"position:relative;\\\\\\\"><script data-jc=\\\\\\\"75\\\\\\\" data-jc-version=\\\\\\\"r20220323\\\\\\\" data-jc-flags=\\\\\\\"[&quot;x%278442&#39;921378y&quot;]\\\\\\\">(function(){/*  Copyright The Closure Library Authors. SPDX-License-Identifier: Apache-2.0 */ var k=\\\\\\\"function\\\\\\\"==typeof Object.create?Object.create:function(a){function b(){}b.prototype=a;return new b},l;if(\\\\\\\"function\\\\\\\"==typeof Object.setPrototypeOf)l=Object.setPrototypeOf;else{var m;a:{var n={a:!0},p={};try{p.__proto__=n;m=p.a;break a}catch(a){}m=!1}l=m?function(a,b){a.__proto__=b;if(a.__proto__!==b)throw new TypeError(a+\\\\\\\" is not extensible\\\\\\\");return a}:null}var q=l,r=this||self;var t=new function(a,b){this.key=a;this.defaultValue=void 0===b?0:b;this.valueType=\\\\\\\"number\\\\\\\"}(\\\\\\\"100004\\\\\\\",16E3);var u=Array.prototype.forEach?function(a,b){Array.prototype.forEach.call(a,b,void 0)}:function(a,b){for(var d=a.length,c=\\\\\\\"string\\\\\\\"===typeof a?a.split(\\\\\\\"\\\\\\\"):a,e=0;e<d;e++)e in c&&b.call(void 0,c[e],e,a)},v=Array.prototype.map?function(a,b){return Array.prototype.map.call(a,b,void 0)}:function(a,b){for(var d=a.length,c=Array(d),e=\\\\\\\"string\\\\\\\"===typeof a?a.split(\\\\\\\"\\\\\\\"):a,f=0;f<d;f++)f in e&&(c[f]=b.call(void 0,e[f],f,a));return c},w=Array.prototype.reduce?function(a,b,d){return Array.prototype.reduce.call(a,b, d)}:function(a,b,d){var c=d;u(a,function(e,f){c=b.call(void 0,c,e,f,a)});return c};function x(a){for(var b=[],d=0;d<a;d++)b[d]=\\\\\\\"\\\\\\\";return b};/*  SPDX-License-Identifier: Apache-2.0 */ function y(a){y[\\\\\\\" \\\\\\\"](a);return a}y[\\\\\\\" \\\\\\\"]=function(){};function A(a,b){if(a)for(var d in a)Object.prototype.hasOwnProperty.call(a,d)&&b(a[d],d,a)}var B=/https?:\\\\\\\\/\\\\\\\\/[^\\\\\\\\/]+/;function C(a){return(a=B.exec(a))&&a[0]||\\\\\\\"\\\\\\\"};var D=0;function E(a){var b=document.currentScript;return(b=void 0===b?null:b)&&b.getAttribute(\\\\\\\"data-jc\\\\\\\")===String(a)?b:document.querySelector('[data-jc=\\\\\\\"'+a+'\\\\\\\"]')};function F(){G||(G=new H);var a=G.g[t.key];if(\\\\\\\"proto\\\\\\\"===t.valueType){try{var b=JSON.parse(a);if(Array.isArray(b))return b}catch(d){}return t.defaultValue}return typeof a===typeof t.defaultValue?a:t.defaultValue};function H(){this.g={};var a=E(D);a=a&&a.getAttribute(\\\\\\\"data-jc-flags\\\\\\\")||\\\\\\\"\\\\\\\";try{var b=JSON.parse(a)[0];a=\\\\\\\"\\\\\\\";for(var d=0;d<b.length;d++)a+=String.fromCharCode(b.charCodeAt(d)^\\\\\\\"\\\\\\\\u0003\\\\\\\\u0007\\\\\\\\u0003\\\\\\\\u0007\\\\\\\\b\\\\\\\\u0004\\\\\\\\u0004\\\\\\\\u0006\\\\\\\\u0005\\\\\\\\u0003\\\\\\\".charCodeAt(d%10));this.g=JSON.parse(a)}catch(c){}}var G,I=H;function J(){this.g={}}I.prototype=k(J.prototype);I.prototype.constructor=I; if(q)q(I,J);else for(var K in J)if(\\\\\\\"prototype\\\\\\\"!=K)if(Object.defineProperties){var L=Object.getOwnPropertyDescriptor(J,K);L&&Object.defineProperty(I,K,L)}else I[K]=J[K];I.l=J.prototype;var M=RegExp(\\\\\\\"^https?://(\\\\\\\\\\\\\\\\w|-)+\\\\\\\\\\\\\\\\.cdn\\\\\\\\\\\\\\\\.ampproject\\\\\\\\\\\\\\\\.(net|org)(\\\\\\\\\\\\\\\\?|/|$)\\\\\\\");function N(a){a=a||O();for(var b=new P(r.location.href,!1),d=null,c=a.length-1,e=c;0<=e;--e){var f=a[e];!d&&M.test(f.url)&&(d=f);if(f.url&&!f.h){b=f;break}}e=null;f=a.length&&a[c].url;0!=b.depth&&f&&(e=a[c]);return new Q(b,e,d)} function O(){var a=r,b=[],d=null;do{var c=a;try{var e;if(e=!!c&&null!=c.location.href)b:{try{y(c.foo);e=!0;break b}catch(h){}e=!1}var f=e}catch(h){f=!1}if(f){var g=c.location.href;d=c.document&&c.document.referrer||null}else g=d,d=null;b.push(new P(g||\\\\\\\"\\\\\\\"));try{a=c.parent}catch(h){a=null}}while(a&&c!=a);c=0;for(a=b.length-1;c<=a;++c)b[c].depth=a-c;c=r;if(c.location&&c.location.ancestorOrigins&&c.location.ancestorOrigins.length==b.length-1)for(a=1;a<b.length;++a)g=b[a],g.url||(g.url=c.location.ancestorOrigins[a- 1]||\\\\\\\"\\\\\\\",g.h=!0);return b}function Q(a,b,d){this.i=a;this.j=b;this.g=d}function P(a,b){this.url=a;this.h=!!b;this.depth=null};function R(a,b,d,c,e){var f=[];A(a,function(g,h){(g=S(g,b,d,c,e))&&f.push(h+\\\\\\\"=\\\\\\\"+g)});return f.join(b)}function S(a,b,d,c,e){if(null==a)return\\\\\\\"\\\\\\\";b=b||\\\\\\\"&\\\\\\\";d=d||\\\\\\\",$\\\\\\\";\\\\\\\"string\\\\\\\"==typeof d&&(d=d.split(\\\\\\\"\\\\\\\"));if(a instanceof Array){if(c=c||0,c<d.length){for(var f=[],g=0;g<a.length;g++)f.push(S(a[g],b,d,c+1,e));return f.join(d[c])}}else if(\\\\\\\"object\\\\\\\"==typeof a)return e=e||0,2>e?encodeURIComponent(R(a,b,d,c,e+1)):\\\\\\\"...\\\\\\\";return encodeURIComponent(String(a))};function T(a,b){this.g=a;this.depth=b}function U(){function a(h,z){return null==h?z:h}var b=O(),d=Math.max(b.length-1,0),c=N(b);b=c.i;var e=c.j,f=c.g,g=[];f&&g.push(new T([f.url,f.h?2:0],a(f.depth,1)));e&&e!=f&&g.push(new T([e.url,2],0));b.url&&b!=f&&g.push(new T([b.url,0],a(b.depth,d)));c=v(g,function(h,z){return g.slice(0,g.length-z)});!b.url||(f||e)&&b!=f||(e=C(b.url))&&c.push([new T([e,1],a(b.depth,d))]);c.push([]);return v(c,function(h){return V(d,h)})} function V(a,b){var d=w(b,function(e,f){return Math.max(e,f.depth)},-1),c=x(d+2);c[0]=a;u(b,function(e){return c[e.depth+1]=e.g});return c}function W(){var a=U();return v(a,function(b){return S(b)})};D=75; window.rfl=function(a){try{var b=Number(0===(F()||0)?2083:F()),d=W();d.pop();var c=b-a.length-5;for(b=0;b<d.length;b++){var e=encodeURIComponent(d[b]);if(e.length<=c)return setTimeout(function(){var f=void 0===f?.01:f;if(!(Math.random()>f)){var g=E(75);g=\\\\\\\"https://\\\\\\\"+(g&&\\\\\\\"true\\\\\\\"===g.getAttribute(\\\\\\\"data-jc-rcd\\\\\\\")?\\\\\\\"pagead2.googlesyndication-cn.com\\\\\\\":\\\\\\\"pagead2.googlesyndication.com\\\\\\\")+\\\\\\\"/pagead/gen_204?id=jca&jc=75&version=\\\\\\\";var h=(h=E(75))&&h.getAttribute(\\\\\\\"data-jc-version\\\\\\\")||\\\\\\\"unknown\\\\\\\";f=g+h+\\\\\\\"&sample=\\\\\\\"+f;g=window; if(h=g.navigator)h=g.navigator.userAgent,h=/Chrome/.test(h)&&!/Edge/.test(h)?!0:!1;h&&g.navigator.sendBeacon?g.navigator.sendBeacon(f):(g.google_image_requests||(g.google_image_requests=[]),h=g.document,h=void 0===h?document:h,h=h.createElement(\\\\\\\"img\\\\\\\"),h.src=f,g.google_image_requests.push(h))}},0),a+\\\\\\\"&rfl=\\\\\\\"+e}return a}catch(f){}return a};}).call(this);</script><script>var url = 'https://googleads.g.doubleclick.net/dbm/ad?dbm_c=AKAmf-CPXqvszy8TWIndn8EhRTKVxux6KHN_Gd1mmp_IeI_tLmNxPEseD4ZBd7bpefZYWO-HyxoIf8XnmVknlDuzbNpTbwNGGVDraSAUSrx4iS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DvbUacCZxBlGfc8tH5ZipqsC42zhhwO8TJvYOSco-uavmnb9XmDXJYEEvAKLIMeNe2gqxG_pn5jkBBh8ZTxZ22N74AFFVbhOG0-4yvoE_s1eDR6qEZVfJgqHGnG64MbHBYH4XjPdyUohjr-hFslybgDItj0i7QmkgWlK2NKinlW65gcydlgoVIB4V1zklfNZQ3hpXawc5g4YRKEy4ZFVFsKbUtvJC4BDu3i_exC47BQMGwVwPo3EwgZZTTfhRiv8sMlEXiW6910PZQSNS1B3IeH5EFdyh28RZxDZbuJV10N-Jnfo-zBZeOy3Xet_zTc8QbN6okMNpN_amSgfNMfISDtxTEVRDQ1VY1SvfGKDkLEKNZYoPZ_vYWn9gTvc2014OuNSi0Z35ZnEDuLVncnoZnHD7pW9WfNwwpEA19o60TkiEHbT3r1J4jjxnir9BkCRJ3VwfFKP8isToZQN4vBydfrCeiuP8Sn9QDvqaAEeHBNwqsrs0ogEdaGU48rr4C52fyJAi1skr1PTjEK2I66fPlaXWR8CPTsaXwryxtZM_4ExCg1wsWrNceCupvJdsXLAAtYgHMgq58x7wrbPxiWVBMeU_Hz6ain4wkFtubTfwEtBHQ0yXHXJyGRlJR35bROGVScjzj_2nC_hRoWezcdHV57VEms975C-96VS8PilAJDn_P_kagkD1vx69QGO0-3huGMLJjvlt1sIvz3xPMzhKx832QZAOi-WfsN0XtbrLKMkicw&cid=CAASfeRor9wquZxeNG7XwJJO2R9n87ypJw6mbe4YCmeOj5A1K2YwumKVWNt4hKXIQiy4IhpZI-PCZbIAu07SqMeChBAwGVL6VNnALXcDAdl4aeV-WFiInak7fzWnPILKLWnS_4-izMSTRBjE17sgfdTOV60X-gSODI8tpioua8il';document.write('<script src=\\\\\\\"' + (window.rfl ? window.rfl(url) : url) + '\\\\\\\"></s' + 'cript>');</script></div></div><img src=\\\\\\\"https://vektor-us-east-1-eks.axonix.com/supply/pxl/9393766c-24ce-4d10-bb6e-80aca669404b/70f5e2f2a68a45b29dce47798e213ca3?index=0&auctionPrice=1.595429457\\\\\\\" alt=\\\\\\\" \\\\\\\" style=\\\\\\\"display:none\\\\\\\"/><img src=\\\\\\\"https://vektor-us-east-1-eks.axonix.com/supply/trk/9393766c-24ce-4d10-bb6e-80aca669404b/70f5e2f2a68a45b29dce47798e213ca3\\\\\\\" width=\\\\\\\"1\\\\\\\" height=\\\\\\\"1\\\\\\\" alt=\\\\\\\"\\\\\\\" style=\\\\\\\"position: absolute; left: 0px; top: 0px; visibility: hidden;\\\\\\\" /><img src=\\\\\\\"https://vektor-us-east-1-eks.axonix.com/supply/imp/9393766c-24ce-4d10-bb6e-80aca669404b/70f5e2f2a68a45b29dce47798e213ca3\\\\\\\" width=\\\\\\\"1\\\\\\\" height=\\\\\\\"1\\\\\\\" alt=\\\\\\\"\\\\\\\" style=\\\\\\\"position: absolute; left: 0px; top: 0px; visibility: hidden;\\\\\\\" /><script> window.SASAdContainerTypeTrackerConfig = {mraidTrackingUrl: 'https://ssb-use1.smartadserver.com:443/api/event?callerid=40&actionid=10012&rtb=1&rtbnid=3817&rtbbid=922501523137776153&rtbh=f0486d91c43d678d29edeea5b5e7b826b47b3d58&rtblt=637839192022385241&rtbet=2&rtbptnid=101',omidTrackingUrl: 'https://ssb-use1.smartadserver.com:443/api/event?callerid=40&actionid=10013&rtb=1&rtbnid=3817&rtbbid=922501523137776153&rtbh=f0486d91c43d678d29edeea5b5e7b826b47b3d58&rtblt=637839192022385241&rtbet=2&rtbptnid=101'} </script><script src=\\\\\\\"https://apps.sascdn.com/onepx/OnepxImpressionScript.js\\\\\\\"></script>\"}\n";
        saveFile("binaryAd", compress(str));
        System.out.println("原长度:" + str.length());
        System.out.println("压缩后字符串:" + Arrays.toString(GZIPUtils.compress(str)).length());
        System.out.println("解压缩后字符串:" + StringUtils.newStringUtf8(GZIPUtils.uncompress(GZIPUtils.compress(str))));
        System.out.println("解压缩后字符串:" + GZIPUtils.uncompressToString(GZIPUtils.compress(str)));
        System.out.println("解压前后是否相等:"+str.equals(GZIPUtils.uncompressToString(GZIPUtils.compress(str))));
    }
}

测试效果

注意:

不要直接将压缩后的byte[]当作字符串进行传输,否则你会发现压缩后的请求数据竟然比没压缩后的要大得多!一般有两种传输压缩后的byte[]的方式:

  • 将压缩后的byet[]进行base64编码再传输字符串,这种方式会损失掉一部分GZIP的压缩效果,适用于压缩结果要存储在Redis中的情况
  • 将压缩后的byte[]以二进制的形式写入到文件中,请求时直接在body中带上文件即可,用这种方式可以不损失压缩效果

1、启动服务

2、使用postman测试

添加请求头信息

Content-Encoding gzip

Content-Type application/json

在这里插入图片描述

3、请求参数设置

这里使用第二种测试,在工具类中 修改 saveFile() 方法来指点保存文件的路径,

执行main方法后,直接用文件作为参数

在这里插入图片描述

最后请求看结果

请求前的大小和请求后的大小,成功处理压缩后的数据

在这里插入图片描述

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-10-31 11:40:47  更:2022-10-31 11:43:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 4:17:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码