请求
package com.jayspt.wrapper;
import org.springframework.util.StreamUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
/**
* @author scz
* @version v1.0
* @Description:
* @create 2024/7/16 11:25
*/
public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper {
private ByteArrayInputStream buffer;
private byte[] bytes;
private Map<String, String[]> params;// 定义参数集合
public MyHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
this.params = request.getParameterMap();
bytes = StreamUtils.copyToByteArray(request.getInputStream());
buffer = new ByteArrayInputStream(bytes);
}
public byte[] getBytes() {
return bytes;
}
@Override
public Map<String, String[]> getParameterMap() {
return params;
}
@Override
public String getParameter(String name) {
String[] vs = params.get(name);
if (vs == null || vs.length == 0) {
return null;
}
return vs[0];
}
@Override
public Enumeration<String> getParameterNames() {
return Collections.enumeration(params.keySet());
}
@Override
public String[] getParameterValues(String name) {
String[] vs = params.get(name);
if (vs == null || vs.length == 0) {
return null;
}
return vs;
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(buffer));
}
@Override
public ServletInputStream getInputStream() throws IOException {
return new MyServletInputStream(buffer);
}
private class MyServletInputStream extends ServletInputStream {
private ByteArrayInputStream bais;
public MyServletInputStream(ByteArrayInputStream bais) {
this.bais = bais;
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() throws IOException {
return bais.read();
}
}
}
响应
package com.jayspt.wrapper;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author scz
* @version v1.0
* @Description:
* @create 2024/7/16 11:16
*/
public class MyHttpServletResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream buffer;
private ServletOutputStream out;
public MyHttpServletResponseWrapper(HttpServletResponse response) {
super(response);
buffer = new ByteArrayOutputStream();
out = new MyServletOutputStream(buffer);
}
/**
* 将数据写回response的PrintWriter
*
* @return
* @throws IOException
*/
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(out);
}
/**
* 将数据写回response的ServletOutputStream
*
* @return
* @throws IOException
*/
@Override
public ServletOutputStream getOutputStream() throws IOException {
return out;
}
/**
* 返回响应数据
* @return
*/
public byte[] getBytes() {
return buffer.toByteArray();
}
/**
* 自定义 ServletOutputStream 将response中的数据写入到buffer中
*/
private class MyServletOutputStream extends ServletOutputStream {
private ByteArrayOutputStream bos;
public MyServletOutputStream(ByteArrayOutputStream bos) {
this.bos = bos;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
/**
* 将数据缓存到buffer中
*
* @param b the <code>byte</code>.
* @throws IOException
*/
@Override
public void write(int b) throws IOException {
bos.write(b);
}
}
}