| package cc.mrbird.febs.pay.util; | 
|   | 
| import org.springframework.web.context.request.RequestAttributes; | 
| import org.springframework.web.context.request.RequestContextHolder; | 
| import org.springframework.web.context.request.ServletRequestAttributes; | 
|   | 
| import javax.servlet.http.HttpServletRequest; | 
| import java.io.*; | 
|   | 
| public class RequestBodyUtils { | 
|   | 
|     private static final int BUFFER_SIZE = 1024 * 8; | 
|   | 
|     /** | 
|      * read string. | 
|      * | 
|      * @param reader Reader instance. | 
|      * @return String. | 
|      * @throws IOException | 
|      */ | 
|     public static String read(Reader reader) throws IOException { | 
|         try (StringWriter writer = new StringWriter()) { | 
|             write(reader, writer); | 
|             return writer.getBuffer().toString(); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * write. | 
|      * | 
|      * @param reader Reader. | 
|      * @param writer Writer. | 
|      * @return count. | 
|      * @throws IOException | 
|      */ | 
|     public static long write(Reader reader, Writer writer) throws IOException { | 
|   | 
|         return write(reader, writer, BUFFER_SIZE); | 
|     } | 
|   | 
|     /** | 
|      * write. | 
|      * | 
|      * @param reader     Reader. | 
|      * @param writer     Writer. | 
|      * @param bufferSize buffer size. | 
|      * @return count. | 
|      * @throws IOException | 
|      */ | 
|     public static long write(Reader reader, Writer writer, int bufferSize) throws IOException { | 
|   | 
|         int read; | 
|         long total = 0; | 
|         char[] buf = new char[bufferSize]; | 
|         while ((read = reader.read(buf)) != -1) { | 
|   | 
|             writer.write(buf, 0, read); | 
|             total += read; | 
|         } | 
|         return total; | 
|     } | 
|   | 
|     /** | 
|      * 获取requestBody | 
|      */ | 
|     public static String getRequestBody() throws IOException { | 
|         RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); | 
|         ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attributes; | 
|         assert servletRequestAttributes != null; | 
|         HttpServletRequest request = servletRequestAttributes.getRequest(); | 
|         BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); | 
|         //读取输入流的内容转换为String类型IOUtils必须引入org.apache.dubbo.common.utils.IOUtils;包 | 
|         return RequestBodyUtils.read(reader); | 
|     } | 
|   | 
| } |