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

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

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

1、背景

在查詢數(shù)據(jù)庫信息的時(shí)候,由于數(shù)據(jù)庫信息返回?cái)?shù)據(jù)條數(shù)較多,數(shù)據(jù)從服務(wù)器端傳至客戶端耗費(fèi)大量時(shí)間,導(dǎo)致查詢數(shù)據(jù)變慢。

2、方案思路

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

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

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

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

1)、gzip壓縮

2)、zip壓縮

4、具體實(shí)現(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");
            // 調(diào)用數(shù)據(jù)庫獲取數(shù)據(jù)
            Map<String, Object> resMap = testMapper.findInfo(sqlStr);
            String dataStr = JSONObject.toJSONString(resMap);
            // 開始?jí)嚎s數(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));
    }

遇到問題

解壓時(shí)候報(bào)錯(cuò):java.util.zip.ZipException: Not in GZIP format

解決方案:在轉(zhuǎn)換為字符串時(shí),一定要使用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);
        // 開始?jí)嚎s數(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、總結(jié)

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

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

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

    SpringMVC的注解@RequestMapping屬性及使用

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

    JAVA 集成 PF4J 插件框架的應(yīng)用場(chǎng)景分析

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

    Spring Profiles使用方法詳解

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

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

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

    kafka提交偏移量失敗導(dǎo)致重復(fù)消費(fèi)的解決

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

    Java獲取電腦真實(shí)IP地址的示例代碼

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

最新評(píng)論

台山市| 龙泉市| 湘阴县| 上林县| 福建省| 毕节市| 新营市| 武隆县| 沐川县| 湖南省| 巴林左旗| 金乡县| 隆化县| 牙克石市| 洛川县| 娱乐| 凤山市| 秦安县| 岳池县| 汾阳市| 长丰县| 社旗县| 汝阳县| 夏津县| 安化县| 资兴市| 深圳市| 旬阳县| 宁波市| 清丰县| 仙桃市| 友谊县| 大方县| 东安县| 彝良县| 台州市| 麻城市| 正镶白旗| 红原县| 抚顺市| 德阳市|