httpClient对post内容gzip压缩和server端解压接收

client端代码:

 

public void sendHttp(String url, String message) {  

if (StringUtils.isBlank(message)) {  

        LOGGER.info("a blank message, return.");  

return;  

    }  

    PostMethod postMethod = new PostMethod(url);  

    postMethod.setContentChunked(true);  

    postMethod.addRequestHeader("Accept", "text/plain");  

    postMethod.setRequestHeader("Content-Encoding", "gzip");  

    postMethod.setRequestHeader("Transfer-Encoding", "chunked");  

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();  

        postMethod.setRequestEntity(new ByteArrayRequestEntity(baos  

                .toByteArray(), "text/plain; charset=utf-8"));  

    } catch (IOException e) {  

        LOGGER.error("write message fail.", e);  

return;  

    }  

int retry = 0;  

do {  

try {  

int status = httpClient.executeMethod(postMethod);  

if (HttpStatus.SC_OK == status) {  

if (LOGGER.isDebugEnabled()) {  

                    LOGGER.debug("send http success, url=" + url  

                            + ", content=" + message);  

                }  

return;  

            } else {  

                String rsp = postMethod.getResponseBodyAsString();  

                LOGGER.error("send http fail, status is: " + status  

                        + ", response is: " + rsp);  

            }  

        } catch (HttpException e) {  

            LOGGER.info("http exception when send http.", e);  

        } catch (IOException e) {  

            LOGGER.info("io exception when send http.", e);  

        } finally {  

            postMethod.releaseConnection();  

        }  

        LOGGER.info("this is "+ retry + " time, try next");  

    } while (retry++ < 3); 

/** 

 *  

 */  

package filter;  

  1. import java.io.IOException;  
  2. import javax.servlet.Filter;  
  3. import javax.servlet.FilterChain;  
  4. import javax.servlet.FilterConfig;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.ServletRequest;  
  7. import javax.servlet.ServletResponse;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. /** 
  10.  * 如果请求消息中包含gzip压缩数据,则进行解压 
  11.  *  
  12.  * @author chunxi.lcx 
  13.  *  
  14.  */  
  15. public class GzipFilter implements Filter {  
  16. @Override  
  17. public void init(FilterConfig filterConfig) throws ServletException {  
  18.     }  
  19. @Override  
  20. public void doFilter(ServletRequest request, ServletResponse response,  
  21.             FilterChain chain) throws IOException, ServletException {  
  22.         chain.doFilter(new GzipRequestWrapper((HttpServletRequest) request),  
  23.                 response);  
  24.     }  
  25. @Override  
  26. public void destroy() {  
  27.     }  
  28. }

 

/**
 *
 */
package filter;

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

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author chunxi.lcx
 *
 */
public class GzipRequestWrapper extends HttpServletRequestWrapper {
    public static final Logger LOGGER = LoggerFactory
            .getLogger(GzipRequestWrapper.class);
    private HttpServletRequest request;

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

    @Override
    public ServletInputStream getInputStream() throws IOException {
        ServletInputStream stream = request.getInputStream();
        String contentEncoding = request.getHeader("Content-Encoding");
        // 如果对内容进行了压缩,则解压
        if (null != contentEncoding && contentEncoding.indexOf("gzip") != -1) {
            try {
                final GZIPInputStream gzipInputStream = new GZIPInputStream(
                        stream);

                ServletInputStream newStream = new ServletInputStream() {

                    @Override
                    public int read() throws IOException {
                        return gzipInputStream.read();
                    }
                };
                return newStream;
            } catch (Exception e) {
                LOGGER.debug("ungzip content fail.", e);
            }
        }
        return stream;
    }

}

转载于:https://www.cnblogs.com/yangjingzhi/p/8416461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值