| | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.nio.charset.StandardCharsets; |
| | | |
| | | |
| | | /** |
| | | * 解决服务器字符集问题 |
| | | * |
| | | * @author 文敏杰 |
| | | * @date 2026/06/25 |
| | | */ |
| | | public class CustomRequestWrapper extends HttpServletRequestWrapper { |
| | | private final String body; |
| | | |
| | | /** |
| | | * 构造函数,用于创建自定义请求包装器。 |
| | | * |
| | | * @param request 原始 HTTP 请求对象 |
| | | * @param body 新的请求体内容 |
| | | * @throws IOException 如果读取或处理请求体时发生错误 |
| | | */ |
| | | public CustomRequestWrapper(HttpServletRequest request, String body) throws IOException { |
| | | private final byte[] body; |
| | | |
| | | public CustomRequestWrapper(HttpServletRequest request, String body) { |
| | | super(request); |
| | | this.body = body; |
| | | this.body = body.getBytes(StandardCharsets.UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * 重写 getInputStream 方法,返回一个新的 ServletInputStream, |
| | | * 其中包含修改后的请求体内容。 |
| | | * |
| | | * @return 包含修改后请求体内容的 ServletInputStream |
| | | * @throws IOException 如果读取输入流时发生错误 |
| | | */ |
| | | @Override |
| | | public ServletInputStream getInputStream() throws IOException { |
| | | final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes()); |
| | | public ServletInputStream getInputStream() { |
| | | |
| | | ByteArrayInputStream bis = |
| | | new ByteArrayInputStream(body); |
| | | |
| | | return new ServletInputStream() { |
| | | |
| | | @Override |
| | | public int read() { |
| | | return bis.read(); |
| | | } |
| | | |
| | | @Override |
| | | public boolean isFinished() { |
| | | // 返回是否已经完成读取 |
| | | return false; |
| | | return bis.available() == 0; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isReady() { |
| | | // 返回是否准备好读取数据 |
| | | return false; |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public void setReadListener(ReadListener readListener) { |
| | | // 设置读监听器,这里不需要实现 |
| | | } |
| | | |
| | | @Override |
| | | public int read() throws IOException { |
| | | // 从字节数组输入流中读取下一个字节 |
| | | return byteArrayInputStream.read(); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 重写 getReader 方法,返回一个新的 BufferedReader, |
| | | * 其中包含修改后的请求体内容。 |
| | | * |
| | | * @return 包含修改后请求体内容的 BufferedReader |
| | | * @throws IOException 如果读取输入流时发生错误 |
| | | */ |
| | | @Override |
| | | public BufferedReader getReader() throws IOException { |
| | | return new BufferedReader(new InputStreamReader(this.getInputStream())); |
| | | public BufferedReader getReader() { |
| | | return new BufferedReader( |
| | | new InputStreamReader( |
| | | getInputStream(), |
| | | StandardCharsets.UTF_8 |
| | | ) |
| | | ); |
| | | } |
| | | |
| | | @Override |
| | | public String getCharacterEncoding() { |
| | | return StandardCharsets.UTF_8.name(); |
| | | } |
| | | |
| | | @Override |
| | | public int getContentLength() { |
| | | return body.length; |
| | | } |
| | | |
| | | @Override |
| | | public long getContentLengthLong() { |
| | | return body.length; |
| | | } |
| | | } |