码迷,mamicode.com
首页 > 其他好文 > 详细

获取post请求的几种常见方式

时间:2019-06-18 10:36:54      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:red   public   tar   blank   buffer   line   lan   ram   stat   

通常从http post请求获取数据的方法如下:
1.request.getInputStream()
2.request.getReader()
3.request.getParameterMap()系列
4.通过spring框架中的RequestBody或RequestParam

public static String req2RawString(HttpServletRequest request) {
          StringBuilder sb = new StringBuilder();
          BufferedReader reader = null;
          try {
              reader = request.getReader();
              String line;
              while ((line = reader.readLine()) != null) {
                   sb.append(line).append(‘\n‘);
              }
              if (sb.length() > 1) {
                   sb.replace(sb.length() - 1, sb.length(), "");
              }
          }
          catch (IOException e) {
              logger.info("RequestUtil,IOException:" + e);
          }
          finally {
              if (reader != null) {
                   try {
                        reader.close();
                   }
                   catch (IOException e) {
                        logger.info("RequestUtil,IOException:" + e);
                   }
              }
          }
          String str = sb.toString();
          logger.info("Request Result:" + str);
          return str;

原文:https://blog.csdn.net/dengbixuan/article/details/73556970 

获取post请求的几种常见方式

标签:red   public   tar   blank   buffer   line   lan   ram   stat   

原文地址:https://www.cnblogs.com/4AMLJW/p/xdfg5654hjghg.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!