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数据压缩传输内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue+springboot读取git的markdown文件并展示功能

    vue+springboot读取git的markdown文件并展示功能

    Markdown-it 是一个用于解析和渲染 Markdown 标记语言的 JavaScript 库,使用 Markdown-it,你可以将 Markdown 文本解析为 HTML 输出,并且可以根据需要添加功能、扩展语法或修改解析行为,本文介绍vue+springboot读取git的markdown文件并展示,感兴趣的朋友一起看看吧
    2024-01-01
  • idea中如何连接hive

    idea中如何连接hive

    这篇文章主要介绍了idea中如何连接hive问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • 浅析对Java关键字final和static的理解

    浅析对Java关键字final和static的理解

    本文主要给大家谈谈小编对java关键字final和static的理解,本文给大家介绍的较详细,需要的朋友参考参考下
    2017-04-04
  • springboot2.0整合dubbo的示例代码

    springboot2.0整合dubbo的示例代码

    这篇文章主要介绍了springboot2.0整合dubbo的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • Java8中接口的新特性测试

    Java8中接口的新特性测试

    今天小编就为大家分享一篇关于Java8中接口的新特性测试,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 解决无法解析javax.servlet的方法

    解决无法解析javax.servlet的方法

    最近在创建一个servlet时,自动生成的代码中出现servlet无法解析的提示,令我无法正常使用servlet里的方法,在对各个步骤进行查看后,发现了问题所在,需要的朋友可以参考下
    2021-05-05
  • MybatisX 快速开发插件过程详解

    MybatisX 快速开发插件过程详解

    MybatisX 是一款基于 IDEA 的快速开发插件,方便在使用mybatis以及mybatis-plus开始时简化繁琐的重复操作,提高开发速率。这篇文章主要介绍了MybatisX 快速开发插件,需要的朋友可以参考下
    2021-10-10
  • Java实例讲解文件上传与跨域问题

    Java实例讲解文件上传与跨域问题

    这篇文章主要介绍了Java文件上传与跨域问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • Spring的事件机制知识点详解及实例分析

    Spring的事件机制知识点详解及实例分析

    在本篇内容里小编给大家分享的是一篇关于Spring的事件机制知识点详解及实例分析,有需要的朋友么可以参考下。
    2021-12-12
  • 详解SpringBoot中添加@ResponseBody注解会发生什么

    详解SpringBoot中添加@ResponseBody注解会发生什么

    这篇文章主要介绍了详解SpringBoot中添加@ResponseBody注解会发生什么,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11

最新评论