实现http的request的inputStream重复读取

Posted 好大的月亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现http的request的inputStream重复读取相关的知识,希望对你有一定的参考价值。

包装类

package com.xx.xx.filter;

import java.io.*;
import java.nio.charset.Charset;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.apache.commons.lang3.exception.ExceptionUtils;

@Slf4j
public class RepeatedlyRequestWrapper extends HttpServletRequestWrapper

    private final byte[] body;

    public RepeatedlyRequestWrapper(HttpServletRequest request, ServletResponse response) throws IOException
    
        super(request);
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        body = getBodyString(request).getBytes("UTF-8");
    

    public String getBodyString(ServletRequest request)
    
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try (InputStream inputStream = request.getInputStream())
        
            reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            String line = "";
            while ((line = reader.readLine()) != null)
            
                sb.append(line);
            
        
        catch (IOException e)
        
            log.warn("获取输入流异常,e:", e.getMessage());
        
        finally
        
            if (reader != null)
            
                try
                
                    reader.close();
                
                catch (IOException e)
                
                    log.error(ExceptionUtils.getMessage(e));
                
            
        
        return sb.toString();
    

    @Override
    public BufferedReader getReader() throws IOException
    
        return new BufferedReader(new InputStreamReader(getInputStream()));
    

    @Override
    public ServletInputStream getInputStream() throws IOException
    
        final ByteArrayInputStream bais = new ByteArrayInputStream(body);
        return new ServletInputStream()
        
            @Override
            public int read() throws IOException
            
                return bais.read();
            

            @Override
            public int available() throws IOException
            
                return body.length;
            

            @Override
            public boolean isFinished()
            
                return false;
            

            @Override
            public boolean isReady()
            
                return false;
            

            @Override
            public void setReadListener(ReadListener readListener)
            

            
        ;
    

过滤器

package com.xxx.xxx.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.apache.commons.lang3.StringUtils;


public class RepeatableFilter implements Filter

    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    

    

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException
    
        ServletRequest requestWrapper = null;
        if (request instanceof HttpServletRequest
                && StringUtils.startsWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE))
        
            requestWrapper = new RepeatedlyRequestWrapper((HttpServletRequest) request, response);
        
        if (null == requestWrapper)
        
            chain.doFilter(request, response);
        
        else
        
            chain.doFilter(requestWrapper, response);
        
    

    @Override
    public void destroy()
    

    

读取流

RepeatedlyRequestWrapper repeatedlyRequest = (RepeatedlyRequestWrapper) request;
nowParams = getBodyString(repeatedlyRequest);

以上是关于实现http的request的inputStream重复读取的主要内容,如果未能解决你的问题,请参考以下文章

Requests库:python实现的简单易用的http库

实现http的request的inputStream重复读取

Android部分下载http

Node.js——request/get实现评论功能

python http requests 怎么实现模拟登录,提交表单

使用Requests库实现api接口测试(Python)