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

访客 阅读:233 2020-02-24 00:48:31 评论:0

通常从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;

标签:java
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

扫一扫关注我们,了解最新精彩内容