GET 和 POST方法获取请求实体内容

java web获取请求体内容

黄威的世界 2016-05-10 13:53:21 浏览3263 评论0

摘要: Java Web中如何获取请求体内容呢? 我们知道请求方式分为两种:Get,Post Java代码   /***       * Compatible with GET and POST       *        * @param request       * @return : <…

Java Web中如何获取请求体内容呢?

我们知道请求方式分为两种:Get,Post

Java代码   收藏代码

  1. /*** 
  2.      * Compatible with GET and POST 
  3.      *  
  4.      * @param request 
  5.      * @return : <code>byte[]</code> 
  6.      * @throws IOException 
  7.      */  
  8.     public static byte[] getRequestQuery(HttpServletRequest request)  
  9.             throws IOException {  
  10.         String submitMehtod = request.getMethod();  
  11.         String queryString = null;  
  12.   
  13.         if (submitMehtod.equals(“GET”)) {// GET  
  14.             queryString = request.getQueryString();  
  15.             String charEncoding = request.getCharacterEncoding();// charset  
  16.             if (charEncoding == null) {  
  17.                 charEncoding = ”UTF-8”;  
  18.             }  
  19.             return queryString.getBytes(charEncoding);  
  20.         } else {// POST  
  21.             return getRequestPostBytes(request);  
  22.         }  
  23.     }  
  24.   
  25. /*** 
  26.      * Get request query string, form method : post 
  27.      *  
  28.      * @param request 
  29.      * @return byte[] 
  30.      * @throws IOException 
  31.      */  
  32.     public static byte[] getRequestPostBytes(HttpServletRequest request)  
  33.             throws IOException {  
  34.         int contentLength = request.getContentLength();  
  35.         if(contentLength<0){  
  36.             return null;  
  37.         }  
  38.         byte buffer[] = new byte[contentLength];  
  39.         for (int i = 0; i < contentLength;) {  
  40.   
  41.             int readlen = request.getInputStream().read(buffer, i,  
  42.                     contentLength - i);  
  43.             if (readlen == -1) {  
  44.                 break;  
  45.             }  
  46.             i += readlen;  
  47.         }  
  48.         return buffer;  
  49.     }  
  50. /*** 
  51.      * Get request query string, form method : post 
  52.      *  
  53.      * @param request 
  54.      * @return 
  55.      * @throws IOException 
  56.      */  
  57.     public static String getRequestPostStr(HttpServletRequest request)  
  58.             throws IOException {  
  59.         byte buffer[] = getRequestPostBytes(request);  
  60.         String charEncoding = request.getCharacterEncoding();  
  61.         if (charEncoding == null) {  
  62.             charEncoding = ”UTF-8”;  
  63.         }  
  64.         return new String(buffer, charEncoding);  
  65.     }  

 说明:当请求方式为“Get”时,直接使用request.getQueryString()获取String

当请求方式为“Post”时,读取InputStream(request.getInputStream())

    <!-- 登录查看 begin -->
            <!-- 登录查看 end -->
</div>

    <div class="copyright-outer-line yq-blog-sem-remove">
        <div class="yq-blog-sem-remove copyright-notice">
        版权声明:本文内容由互联网用户自发贡献,版权归作者所有,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:<a href="mailto:[email protected]">[email protected]</a> 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
    </div>
    </div>
<div class="yq-blog-sem-remove" style="text-align:center; margin-top:10px"><img src="//img.alicdn.com/tfs/TB1lKbLGb9YBuNjy0FgXXcxcXXa-512-512.png" style="width: 144px; height:144px"><p>用云栖社区APP,舒服~</p></div>
                    <div class="yq-blog-sem-remove" style="margin-top: 20px;font-size: 12px;line-height: 1.8;color:#373d41;font-family:PingFangSC-Regular;word-break: break-all;">
            【云栖快讯】Apache旗下顶级开源盛会 HBasecon Asia 2018将于8月17日在京举行,现场仅600席,免费赠票领取入口&nbsp;&nbsp;<a href="https://yq.aliyun.com/promotion/631" target="_blank" rel="nofollow">详情请点击</a>
        </div>

<div class="footer-detail yq-blog-sem-remove yq-blog-interaction clearfix">
    <a href="#comment" class="icon-pinglun comment_btn">评论  (<i>0</i>)</a>
    <span id="vote_btn" has_voted="" data-aid="40930" data-islogin="false" title="点赞" class="icon-zan opt-btn vote_btn ">点赞 (<span id="vote_num">0</span>)</span>
    <span id="mark_btn" has_marked="" data-aid="40930" data-islogin="false" title="收藏" class="icon-love opt-btn mark_btn ">收藏 (<span id="mark_num">0</span>)</span>
    <dl class="share-to">
        <dt>分享到:</dt>
        <dd>
            <a href="http://service.weibo.com/share/share.php?title=java+web%E8%8E%B7%E5%8F%96%E8%AF%B7%E6%B1%82%E4%BD%93%E5%86%85%E5%AE%B9+%0AJava+Web%E4%B8%AD%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E8%AF%B7%E6%B1%82%E4%BD%93%E5%86%85%E5%AE%B9%E5%91%A2%EF%BC%9F%0A%E6%88%91%E4%BB%AC%E7%9F%A5%E9%81%93%E8%AF%B7%E6%B1%82%E6%96%B9%E5%BC%8F%E5%88%86%E4%B8%BA%E4%B8%A4%E7%A7%8D%EF%BC%9AGet%EF%BC%8CPost%0A%0A%0AJava%E4%BB%A3%E7%A0%81%C2%A0%C2%A0%0A%0A%0A%2F%2A%2A%2A%C2%A0%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%2A%C2%A0Compatible%C2%A0with%C2%A0GET%C2%A0and%C2%A0POST%C2%A0%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%2A%C2%A0%C2%A0%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%2A%C2%A0%40param%C2%A0request%C2%A0%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%2A%C2%A0%40return%C2%A0%3A%C2%A0%3C...&amp;url=https%3A%2F%2Fyq.aliyun.com%2Farticles%2F40930" target="_blank" class="icon-weibo"></a>
            <div class="wechat">
                <i class="icon-weixin"></i>
                <img src="/api/qrcode?size=140&amp;key=dd965deb73295cbb7260315e00b8eac2179a36ad&amp;text=https%3A%2F%2Fyq.aliyun.com%2Farticles%2F40930" alt="">
            </div>
        </dd>
    </dl>
</div>
<div class="yq-blog-pre-next yq-blog-sem-remove">
    <ul class="about-c-list">
                        <li><a href="/articles/40927">上一篇:关于sessionid的一些问题</a></li>
                                    <li><a href="/articles/40932">下一篇:java 产生随机数</a></li>
                </ul>
</div>
        <div class="yq-blog-related-articles yq-blog-sem-remove">
        <h3 class="title-info">相关文章</h3>
        <ul class="about-c-list">
                                <li>
                    <a href="/articles/54663">
                                                        请求转发和请求包含
                                                </a>
                </li>
                                <li>
                    <a href="/articles/40650">
                                                        java  web http请求转发
                                                </a>
                </li>
                                <li>
                    <a href="/articles/394397">
                                                        Servlet 手记
                                                </a>
                </li>
                                <li>
                    <a href="/articles/104309">
                                                        JSP内置对象
                                                </a>
                </li>
                                <li>
                    <a href="/articles/611835">
                                                        JSP面试题都在这里
                                                </a>
                </li>
                                <li>
                    <a href="/articles/561905">
                                                        Axis2用法:客户端和服务端
                                                </a>
                </li>
                                <li>
                    <a href="/articles/231246">
                                                        应对安全漏洞:如何将LFI变为RFI
                                                </a>
                </li>
                                <li>
                    <a href="/articles/349394">
                                                        javaweb学习总结(二十七)——jsp简单标签开发案…
                                                </a>
                </li>
                                <li>
                    <a href="/articles/455209">
                                                        Servlet - 基础
                                                </a>
                </li>
                                <li>
                    <a href="/articles/37078">
                                                        JSP 三讲
                                                </a>
                </li>
                        </ul>
    </div>
    <div class="yq-blog-comment yq-blog-sem-remove">
    <h3 class="title-info">网友评论</h3>
                <section class="comments-box clearfix">
            <div class="media-list comments" id="comments">
            </div>

            <form accept-charset="UTF-8" action="/comments" method="POST" data-remote="true" data-target="#comments" class="js-comment-create js-active-on-valid css-unlogin">
                <input type="hidden" name="type" value="article">
                <input type="hidden" name="yunqi_csrf" value="8XQIN8DEK4">
                <input type="hidden" name="isCheck" value="1">
                <input type="hidden" name="pid" value="40930">

                <div class="form-group">
                    <div class="editor">
                                                        <div class="login_tips" id="comment">登录后可评论,请 <a id="fast_login" href="https://account.aliyun.com/login/login.htm?from_type=yqclub&amp;oauth_callback=https%3A%2F%2Fyq.aliyun.com%2Farticles%2F40930%3Fdo%3Dlogin" class="s4" hidefocus="true" rel="nofollow">登录</a> 或 <a href="https://account.aliyun.com/register/register.htm?from_type=yqclub&amp;oauth_callback=https%3A%2F%2Fyq.aliyun.com%2Farticles%2F40930%3Fdo%3Dlogin" target="_blank" class="s4" hidefocus="true" rel="nofollow">注册</a></div>
                                                </div><!-- /.editor  -->
                </div>


                <div class="form-group text-right">
                                                <a href="#modal-login" class="comment_button" data-toggle="modal">评论</a>
                                        </div>
            </form>
        </section>
        </div>

猜你喜欢

转载自blog.csdn.net/didi7696/article/details/81705290