springboot集成gzip和zip数据压缩传输(适用大数据信息传输)

 更新时间:2023年09月27日 11:57:20   作者:随风丶飘  
 在大数据量的传输中,压缩数据后进行传输可以一定程度的解决速度问题,本文主要介绍了springboot集成gzip和zip数据压缩传输,具有一定的参考价值,感兴趣的可以了解一下

1、背景

在查询数据库信息的时候,由于数据库信息返回数据条数较多,数据从服务器端传至客户端耗费大量时间,导致查询数据变慢。

2、方案思路

1)、从查询sql上入手,进行sql优化;

2)、从业务层面优化,复杂接口拆分成多个接口,避免大量数据堆积返回(视业务需求而定);

3)、对返回的大数据信息进行数据压缩。(本文要点)

3、压缩数据方案

1)、gzip压缩

2)、zip压缩

4、具体实现

(1)、gzip压缩方案

 GzipUtils工具类

package com.自己的包.util;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
 * @program: tool_java
 * @description:
 * @author: sfp
 * @create: 2021-11-30 14:33
 **/
@Component
public class GzipUtils {
    /**
     * 压缩
     *
     * @param data 数据流
     * @return 压缩数据流
     * @throws IOException 异常
     */
    public byte[] compress(byte[] data) throws IOException {
        if (data == null || data.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(data);
        gzip.close();
        return out.toByteArray();
    }
    /**
     * 压缩
     *
     * @param str 需要压缩数据信息
     * @return 压缩数据流
     * @throws IOException 异常
     */
    public byte[] compress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return null;
        }
        return compress(str.getBytes(StandardCharsets.UTF_8));
    }
    /**
     * 解压
     *
     * @param data 欲解压数据流
     * @return 原数据流
     * @throws IOException 异常
     */
    public byte[] uncompress(byte[] data) throws IOException {
        if (data == null || data.length == 0) {
            return data;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[1024];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        gunzip.close();
        in.close();
        return out.toByteArray();
    }
    /**
     * 解压
     *
     * @param str 欲解压数据字符串
     * @return 原数据
     * @throws IOException 异常
     */
    public String uncompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        byte[] data = uncompress(str.getBytes(StandardCharsets.ISO_8859_1));
        return new String(data);
    }
}

数据压缩

    @Autowired
    private GzipUtils gzipUtils;
    @RequestMapping(value = "testGzip", method = RequestMethod.POST)
    public JSONBeansResponse testGzip(@RequestBody Map<String, String> map) throws IOException {
        if (null != map) {
            String sqlStr = map.get("paramStr");
            // 调用数据库获取数据
            Map<String, Object> resMap = testMapper.findInfo(sqlStr);
            String dataStr = JSONObject.toJSONString(resMap);
            // 开始压缩数据
            byte[] compress1 = gzipUtils.compress(dataStr);
            String FileBuf = Base64.getEncoder().encodeToString(compress1);
            return new JSONBeansResponse<>(FileBuf);
        }
        return new JSONBeansResponse<>(new ArrayList<>(0));
    }

数据解压

    @RequestMapping(value = "testUnGzip", method = RequestMethod.POST)
    public JSONBeansResponse testUnGzip(@RequestBody Map<String, String> map) throws IOException {
        if (null != map) {
            String dataStream = map.get("dataStream ");
            byte[] decode = Base64.getDecoder().decode(dataStream);
            byte[] compress1 = gzipUtils.uncompress(decode);
            String dataStr = new String(compress1);
            Map<String, Object> res = JSONObject.parseObject(dataStr, Map.class);
            return new JSONBeansResponse<>(res);
        }
        return new JSONBeansResponse<>(new ArrayList<>(0));
    }

遇到问题

解压时候报错:java.util.zip.ZipException: Not in GZIP format

解决方案:在转换为字符串时,一定要使用ISO-8859-1这样的单字节编码

(2)、zip压缩方案

ZipUtils工具类

package com.自己的包.util;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
 * @program: tool_java
 * @description: zip压缩工具
 * @author: sfp
 * @create: 2021-12-01 14:11
 **/
@Component
public class ZipUtils {
/** 压缩
     * @param data  原数据流
     * @return 压缩后的数据流
     * @throws IOException 异常
     */
    public byte[] compress(byte[] data) throws IOException {
        if (data == null || data.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ZipOutputStream gzip = new ZipOutputStream(out);
        gzip.putNextEntry(new ZipEntry("json"));
        gzip.write(data);
        gzip.close();
        return out.toByteArray();
    }
    /** 压缩
     * @param str  原数据字符串
     * @return 压缩后的数据流
     * @throws IOException 异常
     */
    public byte[] compress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return null;
        }
        return compress(str.getBytes(StandardCharsets.UTF_8));
    }
    /** 解压缩
     * @param data  压缩后的数据流
     * @return 原数据的数据流
     * @throws IOException 异常
     */
    public byte[] uncompress(byte[] data) throws IOException {
        if (data == null || data.length == 0) {
            return data;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        ZipInputStream gunzip = new ZipInputStream(in);
        ZipEntry nextEntry = gunzip.getNextEntry();
        while (nextEntry != null) {
            final String fileName = nextEntry.getName();
            if (nextEntry.isDirectory()) {
                nextEntry = gunzip.getNextEntry();
            } else if (fileName.equals("json")) {
                byte[] buffer = new byte[1024];
                int n;
                while ((n = gunzip.read(buffer)) >= 0) {
                    out.write(buffer, 0, n);
                }
                gunzip.close();
                in.close();
                return out.toByteArray();
            }
        }
        return out.toByteArray();
    }
    /** 解压
     * @param str  压缩后的base64流
     * @return 原数据字符串
     * @throws IOException 异常
     */
    public String uncompress(String str) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        byte[] data = uncompress(Base64.getDecoder().decode(str));
        return new String(data);
    }
}

zip使用

    @Autowired
    private ZipUtils zipUtils;
    @RequestMapping(value = "testzip", method = RequestMethod.POST)
    public JSONBeansResponse testzip(@RequestBody Map<String, String> map) throws IOException {
        String sqlStr = map.get("paramStr");
        List<Map<String, Object>> resMap = testMapper.findInfo(sqlStr);;
        String dataStr = JSONObject.toJSONString(resMap);
        // 开始压缩数据
        byte[] compress1 = zipUtils.compress(dataStr);
        String FileBuf = Base64.getEncoder().encodeToString(compress1);
        // 开始解压数据
        String s = zipUtils.uncompress(FileBuf);
        List<Map> arrayLists = JSONObject.parseArray(s, Map.class);
        return new JSONBeansResponse<>(arrayLists);
    }

5、总结

在大数据量的传输中,压缩数据后进行传输可以一定程度的解决速度问题。

zip和gzip的压缩率测试过几次大概在5-6倍大小左右。

到此这篇关于springboot集成gzip和zip数据压缩传输(适用大数据信息传输)的文章就介绍到这了,更多相关springboot gzip和zip数据压缩传输内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java实现简单银行家算法

    java实现简单银行家算法

    这篇文章主要为大家详细介绍了java实现简单银行家算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • 在Eclipse中使用版本管理工具SVN的图文教程

    在Eclipse中使用版本管理工具SVN的图文教程

    下面小编就为大家分享一篇在Eclipse中使用版本管理工具SVN的图文教程,具有很好的参考价值,一起跟随小编过来看看吧
    2017-11-11
  • spring mvc静态资源权限访问的设置方式

    spring mvc静态资源权限访问的设置方式

    文章描述了在Spring MVC项目中,controller层和jsp页面交互时,因未开放静态资源访问导致数据提交异常,通过在spring-mvc配置文件中开放静态资源访问,成功解决了问题,控制台可正常接收到ajax提交的json数据
    2025-10-10
  • Java实现从字符串中找出数字字符串的方法小结

    Java实现从字符串中找出数字字符串的方法小结

    这篇文章主要介绍了Java实现从字符串中找出数字字符串的方法,结合实例形式总结分析了Java查找数字字符串的常用技巧,需要的朋友可以参考下
    2016-03-03
  • SpringMVC的注解@RequestMapping属性及使用

    SpringMVC的注解@RequestMapping属性及使用

    这篇文章主要为大家介绍了SpringMVC注解@RequestMapping属性及使用,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • JAVA 集成 PF4J 插件框架的应用场景分析

    JAVA 集成 PF4J 插件框架的应用场景分析

    PF4J是一个强大的Java插件框架,允许开发者将应用程序分解为可扩展的模块,本文介绍了PF4J的基本概念和如何在Java项目中集成,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2025-03-03
  • Spring Profiles使用方法详解

    Spring Profiles使用方法详解

    在你刚接触SpringBoot的时候有没有对它提供的Profile有些许不适应,经过摸索后才领悟到它的强大。今天我就对Profile进行一点归纳总结,留作互联网记忆
    2022-12-12
  • SpringCloud 限流、熔断、降级的区别及实现

    SpringCloud 限流、熔断、降级的区别及实现

    本文主要介绍了SpringCloud 限流、熔断、降级的区别及实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-03-03
  • kafka提交偏移量失败导致重复消费的解决

    kafka提交偏移量失败导致重复消费的解决

    文章主要讨论了在使用Spring Kafka时遇到的`KafkaException`,特别是与消费者组和偏移量提交相关的问题,文章解释了Kafka消费者的心跳机制和`max.poll.interval.ms`配置的作用,并提供了如何在`application.yml`或`application.properties`文件中配置这些参数的示例
    2026-01-01
  • Java获取电脑真实IP地址的示例代码

    Java获取电脑真实IP地址的示例代码

    这篇文章主要介绍了Java如何获取电脑真实IP地址,忽略虚拟机等IP地址的干扰,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
    2020-09-09

最新评论