java web获取请求体内容
摘要: Java Web中如何获取请求体内容呢? 我们知道请求方式分为两种:Get,Post Java代码 /*** * Compatible with GET and POST * * @param request * @return : <…
Java Web中如何获取请求体内容呢?
我们知道请求方式分为两种:Get,Post
- /***
- * Compatible with GET and POST
- *
- * @param request
- * @return : <code>byte[]</code>
- * @throws IOException
- */
- public static byte[] getRequestQuery(HttpServletRequest request)
- throws IOException {
- String submitMehtod = request.getMethod();
- String queryString = null;
- if (submitMehtod.equals(“GET”)) {// GET
- queryString = request.getQueryString();
- String charEncoding = request.getCharacterEncoding();// charset
- if (charEncoding == null) {
- charEncoding = ”UTF-8”;
- }
- return queryString.getBytes(charEncoding);
- } else {// POST
- return getRequestPostBytes(request);
- }
- }
- /***
- * Get request query string, form method : post
- *
- * @param request
- * @return byte[]
- * @throws IOException
- */
- public static byte[] getRequestPostBytes(HttpServletRequest request)
- throws IOException {
- int contentLength = request.getContentLength();
- if(contentLength<0){
- return null;
- }
- byte buffer[] = new byte[contentLength];
- for (int i = 0; i < contentLength;) {
- int readlen = request.getInputStream().read(buffer, i,
- contentLength - i);
- if (readlen == -1) {
- break;
- }
- i += readlen;
- }
- return buffer;
- }
- /***
- * Get request query string, form method : post
- *
- * @param request
- * @return
- * @throws IOException
- */
- public static String getRequestPostStr(HttpServletRequest request)
- throws IOException {
- byte buffer[] = getRequestPostBytes(request);
- String charEncoding = request.getCharacterEncoding();
- if (charEncoding == null) {
- charEncoding = ”UTF-8”;
- }
- return new String(buffer, charEncoding);
- }
说明:当请求方式为“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席,免费赠票领取入口 <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...&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&key=dd965deb73295cbb7260315e00b8eac2179a36ad&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&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&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>