论坛首页 Java企业应用论坛

如何在Filter中对ServletInputStream处理后(例如解Gzip压缩)让后续Servlet能够getParameter得到参数?

浏览 3117 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2009-10-26   最后修改:2009-10-26
很简单的应用:客户端将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"));
			}
		};
	}
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics