com.ms.network.factory.CachedBodyHttpServletRequest Maven / Gradle / Ivy
The newest version!
/*
* @MS 2022-12-13
* Copyright (c) 2001-2023 萌森
* 保留所有权利
* 本软件为萌森工作室所有及包含机密信息,须遵守其相关许可证条款进行使用。
* Copyright (c) 2001-2023 Meng Sen
* All rights reserved
* This software is owned by Mengsen Studio and contains confidential information, and must be used in accordance with its relevant license terms.
* Website:https://qyg2297248353.top
*/
package com.ms.network.factory;
import org.springframework.util.StreamUtils;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
public class CachedBodyHttpServletRequest extends HttpServletRequestWrapper {
/**
* 存储body数据的容器
*/
private final byte[] cachedBody;
public CachedBodyHttpServletRequest(HttpServletRequest request) throws IOException {
super(request);
// 获取请求流
InputStream requestInputStream = request.getInputStream();
// 以byte的方式,将其复制缓存起来
cachedBody = StreamUtils.copyToByteArray(requestInputStream);
}
@Override
public ServletInputStream getInputStream() {
return new CachedBodyServletInputStream(cachedBody);
}
@Override
public BufferedReader getReader() throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(cachedBody);
return new BufferedReader(new InputStreamReader(byteArrayInputStream));
}
}