server.compression.enabled=trueserver.compression.mime-types=application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plainserver.compression.compressionminsize=10
server.compression.enabled:表示是否开启压缩,默认开启,true:开启,false:不开启
server.compression.mime-types:压缩的内容的类型,有xml,json,html等格式
server.compression.compressionminsize:开启压缩数据最小长度,单位为字节,默认是2048字节
源码如下:
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设置压缩原理tomcat压缩是对响应报文进行压缩,当请求头中存在accept-encoding时,如果tomcat设置了压缩,则会在响应时对数据进行压缩。
tomcat压缩源码是在http11processor中设置的:
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; }}
二、银联报文压缩作为发卡机构,银联报文请求报文是压缩的,且报文头中不存在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("报文压缩,需要进行解压"); //业务处理 //返回报文也同样需要进行压缩处理 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:压缩/解压 字符串 */@slf4jpublic 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; }}
以上就是springboot对压缩请求的处理方法是什么的详细内容。
