SpringBoot 压缩数据流如何解压

JAVA乐园

共 26618字,需浏览 54分钟

 ·

2021-07-19 16:26

0x01:HTTP压缩数据传输简介

通过请求和响应头中增加

Accept-Encoding: gzip
Content-Encodin: gzip

确定客户端或服务器端是否支持压缩

举例,客户端发送请求,服务端压缩响应数据返给客户端

  • 客户端请求中增加 Accept-Encoding: gzip 表示客户端支持gzip;

  • 服务端接收到请求后,将结果通过 gzip 压缩后返回给客户端并在响应头中增加 Content-Encoding: gzip 表示响应数据已被压缩

  • 客户端接收请求,响应头中有 Content-Encoding: gzip 表示数据需解压处理

客户端也可以发送压缩数据给服务端,通过代码将请求数据压缩即可,规范起见同样要在请求中加入 Content-Encoding: gzip


0x02:SpringBoot 案例

pom.xml 引入如下依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.iothw</groupId>
    <artifactId>gzip-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>gzip-demo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
        <mybatis.version>1.3.0</mybatis.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.3.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>aliyunmaven</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>
</project>

编写Gzip解压过滤器

package com.iothw.gzip.filter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ReadListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

/**
 * GZIP处理Filter
 */

@WebFilter(filterName = "gzipFilter", urlPatterns = "/")
public class GzipFilter implements Filter {

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException 
{
        chain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) request), response);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }
}

@Slf4j
class HttpServletRequestWrapper extends javax.servlet.http.HttpServletRequestWrapper {
    private HttpServletRequest request;

    public HttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
        this.request = request;
    }

    /**
     * 根据 request header 的 Content-Encoding 判断是否启用 gzip 解压数据流
     */

    @Override
    public ServletInputStream getInputStream() throws IOException {
        ServletInputStream stream = request.getInputStream();
        String contentEncoding = request.getHeader("Content-Encoding");
        if (null != contentEncoding && contentEncoding.indexOf("gzip") != -1) {
            GZIPInputStream gzipInputStream = new GZIPInputStream(stream);
            try {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                int len = -1;
                byte[] buffer = new byte[128];
                while((len = gzipInputStream.read(buffer))!=-1){
                    bout.write(buffer, 0, len);
                }
                ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
                ServletInputStream newStream = new ServletInputStream() {
                    @Override
                    public int read() throws IOException {
                        return bin.read();
                    }

                    @Override
                    public boolean isFinished() {
                        return false;
                    }

                    @Override
                    public boolean isReady() {
                        return false;
                    }

                    @Override
                    public void setReadListener(ReadListener readListener) {
                    }
                };
                return newStream;
            } catch (Exception e) {
                log.error("uncompress content fail", e);
            }finally{
                try {
                    gzipInputStream.close();
                } catch (Exception e) {
                }
            }
        }

        return stream;
    }
}

注册过滤器

package com.iothw.gzip.config;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.iothw.gzip.filter.GzipFilter;

@Configuration
public class FilterConfig {
    /**
     * 注册 GzipFilter
     *
     * @return
     */

    @Bean
    public FilterRegistrationBean<Filter> filterRegistrationBean() {
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<Filter>();
        registrationBean.setFilter(new GzipFilter());

        List<String> urlPatterns = new ArrayList<>();
        urlPatterns.add("/*");
        registrationBean.setUrlPatterns(urlPatterns);

        return registrationBean;
    }
}

编写SpringBoot引导类

package com.iothw.gzip;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {
    public static void main( String[] args ){
        SpringApplication.run(Application.class, args);
    }
}


0x03:测试验证

  • okhttp

引入依赖

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.4.1</version>
</dependency>

测试类

package com.test;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;

public class OkHttpGzipTest {

    public void processPost() {
        String url = "http://localhost:8080/gzip/gzipTest";
        String str = "我是要被压缩上传的数据,看好了我是压缩的数据";
        try {
            String response = post(url, str);
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String post(String url, String json) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder()
                // 通过GzipRequestInterceptor类拦截响应,自动处理gzip解压
                .addInterceptor(new GzipRequestInterceptor())
                .build();
        MediaType JSON = MediaType.get("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    class GzipRequestInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
                return chain.proceed(originalRequest);
            }

            Request compressedRequest = originalRequest.newBuilder()
                    .header("Content-Encoding""gzip")
                    .method(originalRequest.method(), gzip(originalRequest.body()))
                    .build();
            return chain.proceed(compressedRequest);
        }

        private RequestBody gzip(final RequestBody body) {
            return new RequestBody() {
                @Override
                public MediaType contentType() {
                    return body.contentType();
                }

                @Override
                public long contentLength() {
                    // We don't know the compressed length in advance!
                    return -1;
                }

                @Override
                public void writeTo(BufferedSink sink) throws IOException {
                    BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
                    body.writeTo(gzipSink);
                    gzipSink.close();
                }
            };
        }
    }

    public static void main(String[] args) {
        OkHttpGzipTest gt = new OkHttpGzipTest();
        gt.processPost();
    }
}
  • httpclient

引入依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.10</version>
</dependency>

测试类

package com.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.GZIPOutputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpClientGzipTest {

    /**
     * gzip 压缩发送
     */

    public void sendHttp(String url, String message) throws ClientProtocolException, IOException {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Encoding""gzip");
        try {
            ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
            originalContent.write(message.getBytes(Charset.forName("UTF-8")));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
            originalContent.writeTo(gzipOut);
            gzipOut.finish();
            httpPost.setEntity(new ByteArrayEntity(baos.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpResponse httpResponse = httpClient.execute(httpPost);
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            System.out.println(EntityUtils.toString(httpResponse.getEntity()));
        }
    }

    public static void main(String[] args) {
        HttpClientGzipTest ht = new HttpClientGzipTest();

        String url = "http://localhost:8080/gzip/gzipTest";
        String message = "我是要被压缩上传的数据,看好了我是压缩的数据";

        try {
            ht.sendHttp(url, message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

喜欢,在看



浏览 74
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报