随笔-17  评论-6  文章-1  trackbacks-0


    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;

        int length = req.getContentLength();
        if (length > 0) {
            BufferedRequestWrapper bufferedRequest = new BufferedRequestWrapper(req,length);

            InputStream is = bufferedRequest.getInputStream();
            byte[] content = new byte[length];
           
            int pad = 0;
            while(pad < length){
                pad += is.read(content, pad, length);
            }

            request = bufferedRequest;
        }
        chain.doFilter(request, response);      
    }


BufferedRequestWrapper .java

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

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

public class BufferedRequestWrapper extends HttpServletRequestWrapper {

    ByteArrayInputStream bais;

    BufferedServletInputStream bsis;

    byte[] buffer;

    public BufferedRequestWrapper(HttpServletRequest req,int length) throws IOException {
        super(req);
        // Read InputStream and store its content in a buffer.
        InputStream is = req.getInputStream();
        buffer = new byte[length];

        int pad = 0;
        while(pad < length){
            pad += is.read(buffer, pad, length);
        }
    }

    public ServletInputStream getInputStream() {
        try {
            // Generate a new InputStream by stored buffer
            bais = new ByteArrayInputStream(buffer);
            // Istantiate a subclass of ServletInputStream
            // (Only ServletInputStream or subclasses of it are accepted by the
            // servlet engine!)
            bsis = new BufferedServletInputStream(bais);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
        }
        return bsis;
    }

}




BufferedServletInputStream .java

import java.io.*;
import javax.servlet.ServletInputStream;

/*
 Subclass of ServletInputStream needed by the servlet engine.
 All inputStream methods are wrapped and are delegated to
 the ByteArrayInputStream (obtained as constructor parameter)!
 */
public class BufferedServletInputStream extends ServletInputStream {

    ByteArrayInputStream bais;

    public BufferedServletInputStream(ByteArrayInputStream bais) {
        this.bais = bais;
    }

    public int available() {
        return bais.available();
    }

    public int read() {
        return bais.read();
    }

    public int read(byte[] buf, int off, int len) {
        return bais.read(buf, off, len);
    }

}

posted on 2006-10-25 17:01 小铁匠 阅读(2729) 评论(1)  编辑  收藏 所属分类: java

评论:
# re: filter得到request的body/content[未登录] 2015-04-13 17:55 |
额  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: