很简单的应用:客户端将XML报文用GZIP压缩后通过两个Parameter发送到服务器,用的POST方法,一个Parameter是“head”一个是“body”。我写了一个Filter来对request中的InputStream做解压动作。但是在Filter里面调用了request.getInputStream()方法后,接下来将request送给Servlet以后,request.getParameter("head")就取不到值了,但是request.getInputStream()能够取到解压后的InputStream。怎样才能让后续Servlet能够request.getParameter("head")取到值呢?
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
req = getServletRequest(req);
chain.doFilter(req, response);
}
private HttpServletRequest getServletRequest(
final HttpServletRequest pRequest) {
return new HttpServletRequestWrapper(pRequest) {
public int getContentLength() {
return -1;
}
public ServletInputStream getInputStream() throws IOException {
final InputStream in = new GZIPInputStream(pRequest
.getInputStream());
return new ServletInputStream() {
public int read() throws IOException {
return in.read();
}
public void close() throws IOException {
in.close();
}
};
}
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(
getInputStream(), "UTF-8"));
}
};
}