最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot集成gzip和zip數(shù)據(jù)壓縮傳輸(適用大數(shù)據(jù)信息傳輸)

 更新時間:2023年09月27日 11:57:20   作者:隨風丶飄  
?在大數(shù)據(jù)量的傳輸中,壓縮數(shù)據(jù)后進行傳輸可以一定程度的解決速度問題,本文主要介紹了springboot集成gzip和zip數(shù)據(jù)壓縮傳輸,具有一定的參考價值,感興趣的可以了解一下

1、背景

在查詢數(shù)據(jù)庫信息的時候,由于數(shù)據(jù)庫信息返回數(shù)據(jù)條數(shù)較多,數(shù)據(jù)從服務器端傳至客戶端耗費大量時間,導致查詢數(shù)據(jù)變慢。

2、方案思路

1)、從查詢sql上入手,進行sql優(yōu)化;

2)、從業(yè)務層面優(yōu)化,復雜接口拆分成多個接口,避免大量數(shù)據(jù)堆積返回(視業(yè)務需求而定);

3)、對返回的大數(shù)據(jù)信息進行數(shù)據(jù)壓縮。(本文要點)

3、壓縮數(shù)據(jù)方案

1)、gzip壓縮

2)、zip壓縮

4、具體實現(xiàn)

(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 數(shù)據(jù)流
     * @return 壓縮數(shù)據(jù)流
     * @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 需要壓縮數(shù)據(jù)信息
     * @return 壓縮數(shù)據(jù)流
     * @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 欲解壓數(shù)據(jù)流
     * @return 原數(shù)據(jù)流
     * @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 欲解壓數(shù)據(jù)字符串
     * @return 原數(shù)據(jù)
     * @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);
    }
}

數(shù)據(jù)壓縮

    @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");
            // 調用數(shù)據(jù)庫獲取數(shù)據(jù)
            Map<String, Object> resMap = testMapper.findInfo(sqlStr);
            String dataStr = JSONObject.toJSONString(resMap);
            // 開始壓縮數(shù)據(jù)
            byte[] compress1 = gzipUtils.compress(dataStr);
            String FileBuf = Base64.getEncoder().encodeToString(compress1);
            return new JSONBeansResponse<>(FileBuf);
        }
        return new JSONBeansResponse<>(new ArrayList<>(0));
    }

數(shù)據(jù)解壓

    @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這樣的單字節(jié)編碼

(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  原數(shù)據(jù)流
     * @return 壓縮后的數(shù)據(jù)流
     * @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  原數(shù)據(jù)字符串
     * @return 壓縮后的數(shù)據(jù)流
     * @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  壓縮后的數(shù)據(jù)流
     * @return 原數(shù)據(jù)的數(shù)據(jù)流
     * @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 原數(shù)據(jù)字符串
     * @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);
        // 開始壓縮數(shù)據(jù)
        byte[] compress1 = zipUtils.compress(dataStr);
        String FileBuf = Base64.getEncoder().encodeToString(compress1);
        // 開始解壓數(shù)據(jù)
        String s = zipUtils.uncompress(FileBuf);
        List<Map> arrayLists = JSONObject.parseArray(s, Map.class);
        return new JSONBeansResponse<>(arrayLists);
    }

5、總結

在大數(shù)據(jù)量的傳輸中,壓縮數(shù)據(jù)后進行傳輸可以一定程度的解決速度問題。

zip和gzip的壓縮率測試過幾次大概在5-6倍大小左右。

到此這篇關于springboot集成gzip和zip數(shù)據(jù)壓縮傳輸(適用大數(shù)據(jù)信息傳輸)的文章就介紹到這了,更多相關springboot gzip和zip數(shù)據(jù)壓縮傳輸內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • java實現(xiàn)簡單銀行家算法

    java實現(xiàn)簡單銀行家算法

    這篇文章主要為大家詳細介紹了java實現(xiàn)簡單銀行家算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 在Eclipse中使用版本管理工具SVN的圖文教程

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

    下面小編就為大家分享一篇在Eclipse中使用版本管理工具SVN的圖文教程,具有很好的參考價值,一起跟隨小編過來看看吧
    2017-11-11
  • spring mvc靜態(tài)資源權限訪問的設置方式

    spring mvc靜態(tài)資源權限訪問的設置方式

    文章描述了在Spring MVC項目中,controller層和jsp頁面交互時,因未開放靜態(tài)資源訪問導致數(shù)據(jù)提交異常,通過在spring-mvc配置文件中開放靜態(tài)資源訪問,成功解決了問題,控制臺可正常接收到ajax提交的json數(shù)據(jù)
    2025-10-10
  • Java實現(xiàn)從字符串中找出數(shù)字字符串的方法小結

    Java實現(xiàn)從字符串中找出數(shù)字字符串的方法小結

    這篇文章主要介紹了Java實現(xiàn)從字符串中找出數(shù)字字符串的方法,結合實例形式總結分析了Java查找數(shù)字字符串的常用技巧,需要的朋友可以參考下
    2016-03-03
  • SpringMVC的注解@RequestMapping屬性及使用

    SpringMVC的注解@RequestMapping屬性及使用

    這篇文章主要為大家介紹了SpringMVC注解@RequestMapping屬性及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • JAVA 集成 PF4J 插件框架的應用場景分析

    JAVA 集成 PF4J 插件框架的應用場景分析

    PF4J是一個強大的Java插件框架,允許開發(fā)者將應用程序分解為可擴展的模塊,本文介紹了PF4J的基本概念和如何在Java項目中集成,本文給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2025-03-03
  • Spring Profiles使用方法詳解

    Spring Profiles使用方法詳解

    在你剛接觸SpringBoot的時候有沒有對它提供的Profile有些許不適應,經過摸索后才領悟到它的強大。今天我就對Profile進行一點歸納總結,留作互聯(lián)網記憶
    2022-12-12
  • SpringCloud 限流、熔斷、降級的區(qū)別及實現(xiàn)

    SpringCloud 限流、熔斷、降級的區(qū)別及實現(xiàn)

    本文主要介紹了SpringCloud 限流、熔斷、降級的區(qū)別及實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-03-03
  • kafka提交偏移量失敗導致重復消費的解決

    kafka提交偏移量失敗導致重復消費的解決

    文章主要討論了在使用Spring Kafka時遇到的`KafkaException`,特別是與消費者組和偏移量提交相關的問題,文章解釋了Kafka消費者的心跳機制和`max.poll.interval.ms`配置的作用,并提供了如何在`application.yml`或`application.properties`文件中配置這些參數(shù)的示例
    2026-01-01
  • Java獲取電腦真實IP地址的示例代碼

    Java獲取電腦真實IP地址的示例代碼

    這篇文章主要介紹了Java如何獲取電腦真實IP地址,忽略虛擬機等IP地址的干擾,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-09-09

最新評論

封丘县| 晋江市| 广河县| 西城区| 通渭县| 津南区| 都昌县| 乐业县| 清水县| 温宿县| 广平县| 张家口市| 靖州| 多伦县| 达拉特旗| 武夷山市| 平原县| 镇远县| 南郑县| 韩城市| 图木舒克市| 饶阳县| 连南| 思茅市| 申扎县| 双桥区| 云阳县| 会同县| 正蓝旗| 宣武区| 定安县| 龙泉市| 乌兰县| 荆州市| 资讯 | 襄城县| 山西省| 英德市| 阳泉市| 施甸县| 龙口市|