/** * 获取requestBody */ public static String getRequestBody() throws IOException
{ RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)
attributes; HttpServletRequest request = servletRequestAttributes.getRequest();
BufferedReader reader = new BufferedReader(new
InputStreamReader(request.getInputStream()));
//读取输入流的内容转换为String类型IOUtils必须引入org.apache.dubbo.common.utils.IOUtils;包 String
body = RequestBodyUtils.read(reader); return body; }
工具类:
import java.io.IOException; import java.io.Reader; import
java.io.StringWriter; import java.io.Writer; /** * @author Created by
pangkunkun on 2018/4/20. * 这段代码来自dubbo * */ 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 { StringWriter writer =
new StringWriter(); try { write(reader, writer); return
writer.getBuffer().toString(); } finally{ writer.close(); } } /** * 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[BUFFER_SIZE]; while( ( read = reader.read(buf)
) != -1 ) { writer.write(buf, 0, read); total += read; } return total; } }

技术
今日推荐
阅读数 169928
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信