HttpServletRequest 请求中的 body 内容仅能调用 request.getInputStream(), request.getReader()和request.getParameter(“key”) 方法读取一次,重复读取会报 java.io.IOException: Stream closed 异常。 需要包装HttpServletRequest对象,缓存body数据,再次读取的时候将缓存的值写出
一、MyRequestWrapper
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.apache.commons.io.IOUtils;
import org.springframework.util.StringUtils;
public class MyRequestWrapper extends HttpServletRequestWrapper {
private final String body;
public MyRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
this.body = this.getBodyString(request);
}
public String getBody() {
return this.body;
}
public String getBodyString(HttpServletRequest request) throws IOException {
String contentType = request.getContentType();
String bodyString = "";
StringBuilder sb = new StringBuilder();
if (StringUtils.isEmpty(contentType) || !contentType.contains("multipart/form-data") && !contentType.contains("x-www-form-urlencoded")) {
return IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
} else {
Map<String, String[]> parameterMap = request.getParameterMap();
Entry next;
String value;
for(Iterator var6 = parameterMap.entrySet().iterator(); var6.hasNext(); sb.append((String)next.getKey()).append("=").append(value).append("&")) {
next = (Entry)var6.next();
String[] values = (String[])next.getValue();
value = null;
if (values != null && values.length == 1) {
value = values[0];
} else {
value = Arrays.toString(values);
}
}
if (sb.length() > 0) {
bodyString = sb.toString().substring(0, sb.toString().length() - 1);
}
return bodyString;
}
}
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(this.body.getBytes());
return new ServletInputStream() {
public boolean isFinished() {
return false;
}
public boolean isReady() {
return false;
}
public void setReadListener(ReadListener readListener) {
}
public int read() {
return bais.read();
}
};
}
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
}
二、编写Filter重新包装request为MyRequestWrapper
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
@Component
public class HttpServletRequestReplacedFilter implements Filter {
public HttpServletRequestReplacedFilter() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
if (request instanceof HttpServletRequest) {
requestWrapper = new MyRequestWrapper((HttpServletRequest)request);
}
if (requestWrapper == null) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}
}
三、编写工具类HttpHelper
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpHelper {
private static final Logger log = LoggerFactory.getLogger(HttpHelper.class);
private HttpHelper() {
}
public static String getBodyString(HttpServletRequest request) throws IOException {
StringBuilder sb = new StringBuilder();
try {
InputStream inputStream = request.getInputStream();
Throwable var3 = null;
try {
InputStreamReader isr = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
Throwable var5 = null;
try {
BufferedReader reader = new BufferedReader(isr);
Throwable var7 = null;
try {
String line = "";
while((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (Throwable var54) {
var7 = var54;
throw var54;
} finally {
if (reader != null) {
if (var7 != null) {
try {
reader.close();
} catch (Throwable var53) {
var7.addSuppressed(var53);
}
} else {
reader.close();
}
}
}
} catch (Throwable var56) {
var5 = var56;
throw var56;
} finally {
if (isr != null) {
if (var5 != null) {
try {
isr.close();
} catch (Throwable var52) {
var5.addSuppressed(var52);
}
} else {
isr.close();
}
}
}
} catch (Throwable var58) {
var3 = var58;
throw var58;
} finally {
if (inputStream != null) {
if (var3 != null) {
try {
inputStream.close();
} catch (Throwable var51) {
var3.addSuppressed(var51);
}
} else {
inputStream.close();
}
}
}
} catch (IOException var60) {
log.error(var60.getMessage(), var60);
}
return sb.toString();
}
}
四、获取请求体
String str = HttpHelper.getBodyString(request); 即可获取到请求体
|