package com.example.springboot.core; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.io.*; /** * @Auther: qint * @Date: 2023/03/01 16:57 * @Description: */ @Slf4j public class RequestWrapper extends HttpServletRequestWrapper { private String body; public RequestWrapper(HttpServletRequest request) throws IOException { super(request); body=getBodyStr(request); } public String getBodyStr(){ return body; } //方法1 // private String getBodyStr(HttpServletRequest request) throws IOException { // // BufferedReader reader=request.getReader(); // String line=null; // StringBuilder stringBuilder=new StringBuilder(); // while ((line=reader.readLine())!=null){ // // stringBuilder.append(line); // } // return stringBuilder.toString(); // // } //方法2 private String getBodyStr(HttpServletRequest request) throws IOException { InputStreamReader reader=new InputStreamReader(request.getInputStream()); OutputStream outputStream=new OutputStreamWriter(reader); int line=0; StringBuilder stringBuilder=new StringBuilder(); while ((line=reader.read())!=-1){ stringBuilder.append((char) line); log.info("par="+(char)line); } return stringBuilder.toString(); } }
标签:Body,SpringBoot,stringBuilder,request,reader,过滤器,import,line,String From: https://www.cnblogs.com/tiancai/p/17169182.html