亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot對壓縮請求的處理方法

 更新時間:2023年05月04日 08:56:29   作者:riant110  
這篇文章主要介紹了springboot對壓縮請求的處理,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

springboot對壓縮請求的處理

最近對接銀聯(lián)需求,為了節(jié)省帶寬,需要對報文進(jìn)行壓縮處理。但是使用springboot自帶的壓縮設(shè)置不起作用:

server.compression.enabled=true
server.compression.mime-types=application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plain
server.compression.compressionMinSize=10

server.compression.enabled:表示是否開啟壓縮,默認(rèn)開啟,true:開啟,false:不開啟
server.compression.mime-types:壓縮的內(nèi)容的類型,有xml,json,html等格式
server.compression.compressionMinSize:開啟壓縮數(shù)據(jù)最小長度,單位為字節(jié),默認(rèn)是2048字節(jié)
源碼如下:

public class Compression {
    private boolean enabled = false;
    private String[] mimeTypes = new String[]{"text/html", "text/xml", "text/plain", "text/css", "text/javascript", "application/javascript", "application/json", "application/xml"};
    private String[] excludedUserAgents = null;
    private int minResponseSize = 2048;
}

一、Tomcat設(shè)置壓縮原理

tomcat壓縮是對響應(yīng)報文進(jìn)行壓縮,當(dāng)請求頭中存在accept-encoding時,如果Tomcat設(shè)置了壓縮,則會在響應(yīng)時對數(shù)據(jù)進(jìn)行壓縮。
tomcat壓縮源碼是在Http11Processor中設(shè)置的:

public class Http11Processor extends AbstractProcessor {
  private boolean useCompression() {
        // Check if browser support gzip encoding
        MessageBytes acceptEncodingMB =
            request.getMimeHeaders().getValue("accept-encoding");
        if ((acceptEncodingMB == null)-->當(dāng)請求頭沒有這個字段是不進(jìn)行壓縮
            || (acceptEncodingMB.indexOf("gzip") == -1)) {
            return false;
        }
        // If force mode, always compress (test purposes only)
        if (compressionLevel == 2) {
            return true;
        }
        // Check for incompatible Browser
        if (noCompressionUserAgents != null) {
            MessageBytes userAgentValueMB =
                request.getMimeHeaders().getValue("user-agent");
            if(userAgentValueMB != null) {
                String userAgentValue = userAgentValueMB.toString();
                if (noCompressionUserAgents.matcher(userAgentValue).matches()) {
                    return false;
                }
            }
        }
        return true;
    }
}

二、銀聯(lián)報文壓縮

作為發(fā)卡機構(gòu),銀聯(lián)報文請求報文是壓縮的,且報文頭中不存在accept-encoding字段,無法直接使用tomcat配置進(jìn)行壓縮解壓。
需要單獨處理這種請求

@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    /**
     *
     * application/xml格式報文
     * */
    @PostMapping(path = "/test", produces = MediaType.APPLICATION_XML_VALUE, consumes = MediaType.APPLICATION_XML_VALUE)
    public void getUserInfoById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody;
        String resultBody="hello,wolrd";
        byte[] returnByte;
        if (StringUtils.isNoneEmpty(request.getHeader("Content-encoding"))) {
            logger.info("報文壓縮,需要進(jìn)行解壓");
            //業(yè)務(wù)處理
            //返回報文也同樣需要進(jìn)行壓縮處理
            assemleResponse(request,response,resultBody);
        }
    }
    public static void assemleResponse(HttpServletRequest request, HttpServletResponse response,String resultBody) throws IOException {
        response.setHeader("Content-Type","application/xml;charset=UTF-8");
        response.setHeader("Content-Encoding","gzip");
        byte[] returnByte=GzipUtil.compress(resultBody);
        OutputStream outputStream=response.getOutputStream();
        outputStream.write(returnByte);
    }
}
public class GzipUtil {
    public static String uncompress(byte[] bytes){
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        String requestBody=null;
        ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes);
        GZIPInputStream gzipInputStream = null;
        try {
            gzipInputStream = new GZIPInputStream(byteArrayInputStream);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int temp;
            while ((temp = gzipInputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, temp);
            }
            requestBody = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  requestBody;
    }
    public static String uncompress(HttpServletRequest request){
        String requestBody=null;
        int length = request.getContentLength();
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(request.getInputStream());
            GZIPInputStream gzipInputStream = new GZIPInputStream(bufferedInputStream);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int temp;
            while ((temp = gzipInputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, temp);
            }
            requestBody = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  requestBody;
    }
    public static byte[] compress(String src){
        if (src == null || src.length() == 0) {
            return null;
        }
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        try {
            GZIPOutputStream gzipOutputStream=new GZIPOutputStream(byteArrayOutputStream);
            gzipOutputStream.write(src.getBytes(StandardCharsets.UTF_8));
            gzipOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes=byteArrayOutputStream.toByteArray();
        return bytes;
    }
}

補充:java springbooot使用gzip壓縮字符串

import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
 * effect:壓縮/解壓 字符串
 */
@Slf4j
public class CompressUtils {
    /**
     * effect 使用gzip壓縮字符串
     * @param str 要壓縮的字符串
     * @return
     */
    public static String compress(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
        } catch (IOException e) {
            log.error("",e);
        } finally {
            if (gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    log.error("",e);
                }
            }
        }
        return new sun.misc.BASE64Encoder().encode(out.toByteArray());
//        return str;
    }
    /**
     * effect 使用gzip解壓縮
     *
     * @param str 壓縮字符串
     * @return
     */
    public static String uncompress(String str) {
        if (str == null) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        byte[] compressed = null;
        String decompressed = null;
        try {
            compressed = new sun.misc.BASE64Decoder().decodeBuffer(str);
            in = new ByteArrayInputStream(compressed);
            ginzip = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            log.error("",e);
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return decompressed;
    }
}

到此這篇關(guān)于springboot對壓縮請求的處理的文章就介紹到這了,更多相關(guān)springboot壓縮請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis開發(fā)要點-resultType和resultMap有什么區(qū)別詳解

    Mybatis開發(fā)要點-resultType和resultMap有什么區(qū)別詳解

    本文主要介紹了Mybatis開發(fā)要點-resultType和resultMap有什么區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用

    Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用

    今天小編就為大家分享一篇關(guān)于Junit 5中@ParameterizedTest與@EnumSource結(jié)合使用,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • druid執(zhí)行SQL出現(xiàn)錯誤但不影響返回結(jié)果的問題及解決

    druid執(zhí)行SQL出現(xiàn)錯誤但不影響返回結(jié)果的問題及解決

    這篇文章主要介紹了druid執(zhí)行SQL出現(xiàn)錯誤但不影響返回結(jié)果的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java?spring?boot發(fā)送郵箱實現(xiàn)過程記錄

    Java?spring?boot發(fā)送郵箱實現(xiàn)過程記錄

    我們在?站上注冊賬號的時候?般需要獲取驗證碼,?這個驗證碼?般發(fā)送在你的?機號上還有的是發(fā)送在你的郵箱中,這篇文章主要給大家介紹了關(guān)于Java?spring?boot發(fā)送郵箱實現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Spring配置文件的拆分和整合過程分析

    Spring配置文件的拆分和整合過程分析

    在實際應(yīng)用里,隨著應(yīng)用規(guī)模的增加,系統(tǒng)中 Bean 數(shù)量也大量增加,導(dǎo)致配置文件非常龐大。為了避免這種情況的產(chǎn)生,提高配置文件的可讀性與可維護(hù)性,可以將Spring 配置文件分解成多個配置文件,感興趣的朋友跟隨小編一起看看吧
    2022-10-10
  • 一文詳解Java項目中如何優(yōu)雅的使用枚舉類型

    一文詳解Java項目中如何優(yōu)雅的使用枚舉類型

    枚舉類型在開發(fā)中是很常見的,有非常多的應(yīng)用場景,這篇文章我們就來學(xué)習(xí)一下項目中如何優(yōu)雅的使用枚舉類型,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • Java8不可或缺小幫手之日期應(yīng)用

    Java8不可或缺小幫手之日期應(yīng)用

    jdk1.8后引入了新的日期時間處理API,相比傳統(tǒng)的date操作更加簡便,date中的SimpleDateFormat也是非線程安全的,廢話不多說,開干
    2023-05-05
  • springSecurity之如何添加自定義過濾器

    springSecurity之如何添加自定義過濾器

    這篇文章主要介紹了springSecurity之如何添加自定義過濾器的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring注解@Value及屬性加載配置文件方式

    Spring注解@Value及屬性加載配置文件方式

    這篇文章主要介紹了Spring注解@Value及屬性加載配置文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 23種設(shè)計模式(22)java狀態(tài)模式

    23種設(shè)計模式(22)java狀態(tài)模式

    這篇文章主要為大家詳細(xì)介紹了23種設(shè)計模式之java狀態(tài)模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評論