springboot對壓縮請求的處理方法
springboot對壓縮請求的處理
最近對接銀聯(lián)需求,為了節(jié)省帶寬,需要對報文進行壓縮處理。但是使用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:表示是否開啟壓縮,默認開啟,true:開啟,false:不開啟
server.compression.mime-types:壓縮的內(nèi)容的類型,有xml,json,html等格式
server.compression.compressionMinSize:開啟壓縮數(shù)據(jù)最小長度,單位為字節(jié),默認是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壓縮是對響應報文進行壓縮,當請求頭中存在accept-encoding時,如果Tomcat設(shè)置了壓縮,則會在響應時對數(shù)據(jù)進行壓縮。
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)-->當請求頭沒有這個字段是不進行壓縮
|| (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配置進行壓縮解壓。
需要單獨處理這種請求
@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("報文壓縮,需要進行解壓");
//業(yè)務處理
//返回報文也同樣需要進行壓縮處理
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ū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
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é)果的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java?spring?boot發(fā)送郵箱實現(xiàn)過程記錄
我們在?站上注冊賬號的時候?般需要獲取驗證碼,?這個驗證碼?般發(fā)送在你的?機號上還有的是發(fā)送在你的郵箱中,這篇文章主要給大家介紹了關(guān)于Java?spring?boot發(fā)送郵箱實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-01-01

