获取HttpServletResponse出入参
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取HttpServletResponse出入参相关的知识,希望对你有一定的参考价值。
参考技术A package com.himo.Filter;import com.alibaba.fastjson.JSON;
import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
/**
* 作者:修罗大人
* 时间:2018-11-15 16:46
* 描述:日志过滤器
*/
@Component
@WebFilter(filterName ="logFilter", urlPatterns ="/*")
public class LogFilterimplements Filter
private Loggerlogger = LoggerFactory.getLogger(this.getClass());
private static final StringignoreUrlRegex =".*((pay/)|(/index)|(/index/.*)|([.]((html)|(jsp)|(css)|(js)|(gif)|(png))))$";
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throws IOException, ServletException
ThreadContext.put("TId", UUID.randomUUID().toString());
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
// 请求html页面、js不打印日志
if (httpServletRequest.getRequestURI().matches(ignoreUrlRegex))
ThreadContext.clearAll();
filterChain.doFilter(servletRequest,servletResponse);
return;
HttpServletRequest request = (HttpServletRequest) servletRequest;
logger.info("url: " + request.getRequestURL());//url
logger.info("ip: " + request.getRemoteHost());//ip
logger.info("method: "+request.getMethod());//post or get? or ?
Map parameterMap = request.getParameterMap();
logger.warn("请求url参数:" + JSON.toJSONString(parameterMap));
try
ServletInputStream inputStream = request.getInputStream();
int len = request.getContentLength();
if (len >0)
byte[] bytes =new byte[len];
int i =0;
ByteArrayOutputStream bo =new ByteArrayOutputStream();
inputStream.read(bytes,i,len);
bo.write(bytes,i,len);
bo.flush();
bo.close();
String result = bo.toString();
logger.warn("请求体参数" + result);
catch (IOException e)
e.printStackTrace();
HttpServletResponse resp = (HttpServletResponse) servletResponse;
ResponseWrapper mResp =new ResponseWrapper(resp);// 包装响应对象 resp 并缓存响应数据
filterChain.doFilter(request, mResp);
byte[] bytes = mResp.getBytes();// 获取缓存的响应数据
if (bytes.length >0)
String responseStr =new String(bytes);
logger.warn("返回值:" + responseStr);
// System.out.println("压缩前大小:" + bytes.length);
// System.out.println("压缩前数据:" + new String(bytes,"utf-8"));
// ByteArrayOutputStream bout = new ByteArrayOutputStream();
// GZIPOutputStream gzipOut = new GZIPOutputStream(bout); // 创建 GZIPOutputStream 对象
// gzipOut.write(bytes); // 将响应的数据写到 Gzip 压缩流中
// gzipOut.flush();
// gzipOut.close(); // 将数据刷新到 bout 字节流数组
// byte[] bts = bout.toByteArray();
// System.out.println("压缩后大小:" + bts.length);
// resp.setHeader("Content-Encoding", "gzip"); // 设置响应头信息
// resp.getOutputStream().write(bts); // 将压缩数据响应给客户端
@Override
public void init(FilterConfig filterConfig)throws ServletException
@Override
public void destroy()
package com.himo.Filter;
/**
* 作者:修罗大人
* 时间:2018-11-15 17:13
* 描述:ResponseWrapper
*/
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.*;
public class ResponseWrapperextends HttpServletResponseWrapper
private ByteArrayOutputStreambytes =new ByteArrayOutputStream();
private HttpServletResponseresponse;
private PrintWriterpwrite;
public ResponseWrapper(HttpServletResponse response)
super(response);
this.response = response;
@Override
public ServletOutputStream getOutputStream()throws IOException
return new MyServletOutputStream(bytes);// 将数据写到 byte 中
/**
* 重写父类的 getWriter () 方法,将响应数据缓存在 PrintWriter 中
*/
@Override
public PrintWriter getWriter()throws IOException
try
pwrite =new PrintWriter(new OutputStreamWriter(bytes,"utf-8"));
catch (UnsupportedEncodingException e)
e.printStackTrace();
return pwrite;
/**
* 获取缓存在 PrintWriter 中的响应数据
*
* @return
*/
public byte[] getBytes()
if (null !=pwrite)
pwrite.close();
return bytes.toByteArray();
if (null !=bytes)
try
bytes.flush();
catch (IOException e)
e.printStackTrace();
return bytes.toByteArray();
class MyServletOutputStreamextends ServletOutputStream
private ByteArrayOutputStreamostream;
public MyServletOutputStream(ByteArrayOutputStream ostream)
this.ostream = ostream;
@Override
public void write(int b)throws IOException
ostream.write(b);// 将数据写到 stream 中
@Override
public boolean isReady()
return false;
@Override
public void setWriteListener(WriteListener writeListener)
如何从 HttpServletResponse 获取 HTML 内容?
【中文标题】如何从 HttpServletResponse 获取 HTML 内容?【英文标题】:How to get the HTML content from HttpServletResponse? 【发布时间】:2014-08-21 17:05:19 【问题描述】:我已经设法在 tomcat 上为 servlet 设置了一个过滤器,但是 HttpServletResponse 仅具有获取响应标头的功能,而不是实际的 HTML 响应本身。一些阅读告诉我,我必须制作一个包装类,但我不知道该怎么做。经过一番苦恼后,我决定将其发布在 *** 上。
如何从 HttpServletResponse 中检索 HTML 内容?我需要将其保存为文本文件。
【问题讨论】:
【参考方案1】:您需要实现HttpServletRequestWrapper 接口,尤其是getWriter 和getOutputStream 方法,然后将该类提供给doFilter 方法。最后,您会将缓冲区的内容转储到文件中。
在这里检查一点类似的问题response wrapper to convert all output to uppercase
【讨论】:
以上是关于获取HttpServletResponse出入参的主要内容,如果未能解决你的问题,请参考以下文章
ArthasArthas使用watch命令(生产上查看方法出入参)