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())

java web获取请求体内容的更多相关文章

  1. Java Web之请求和响应

    Servlet最主要作用就是处理客户端请求并作出回应,为此,针对每次请求,Web容器在调用service()之前都会创建两个对象,分别是HttpServletRequest和HttpServletRe ...

  2. Spring Cloud Gateway 之获取请求体(Request Body)的几种方式

    Spring Cloud Gateway 获取请求体 一.直接在全局拦截器中获取,伪代码如下 private String resolveBodyFromRequest(ServerHttpReque ...

  3. Java Web 获取客户端真实IP

    Java Web 获取客户端真实IP 发生的场景:服务器端接收客户端请求的时候,一般需要进行签名验证,客户端IP限定等情况,在进行客户端IP限定的时候,需要首先获取该真实的IP.一般分为两种情况: 方 ...

  4. SpringBoot web获取请求数据【转】

    SpringBoot web获取请求数据 一个网站最基本的功能就是匹配请求,获取请求数据,处理请求(业务处理),请求响应,我们今天来看SpringBoot中怎么获取请求数据. 文章包含的内容如下: 获 ...

  5. 使用Java发送Http请求的内容

    公司要将自己的产品封装一个WebService平台,所以最近开始学习使用Java发送Http请求的内容.这一块之前用PHP的时候写的也比较多,从用最基本的Socket和使用第三方插件都用过. 学习了J ...

  6. charles修改请求体内容

    charles修改请求体内容 问:什么是请求体?答:客户端向服务端发出的请求简称请求体,请求体中包含有许许多多的参数,每一个参数都有其特定的意义.多一个或者少一个则服务端给你返回的响应体就会不一样 一 ...

  7. 获取请求体数据 POST

    POST获取请求体 请求体中封装了 POST请求的请求参数 获取流对象 再从流对象中那数据 一种字节流 一种字符流 BufferedReader getReader()获取字符输入流 只能操作字符 S ...

  8. java web中请求和响应中包含中文出现乱码解析

    说明:在计算机中保存的一切文本信息是以一定的编码表(0,1,0,1)来保存我们所认识的字符(汉字或英文字符),由字符到计算机存储的二进制过程是编码,由读取二进制到文本的过程称为解码.而字符编码有多种不 ...

  9. 入门servlet:request获取请求体数据

    @WebServlet("/RequestDemo5") public class RequestDemo5 extends HttpServlet { protected voi ...

随机推荐

  1. 【转】C++标准转换运算符static_cast

    static_cast<new_type> (expression) 虽然const_cast是用来去除变量的const限定,但是static_cast却不是用来去除变量的static引用 ...

  2. codeforces 161D Distance in Tree 树上点分治

    链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...

  3. SpringMVC拦截器(慕课网)

    拦截器:通过统一拦截从浏览器发往服务器的请求来完成功能的增强 使用场景:解决请求的共性问题 如:乱码.权限验证 基本工作原理:拦截器和过滤器的工作原理相似 乱码问题:使用Spring过滤器(Filte ...

  4. node ,npm和nvm 版本的管理

    node npm :node 的包管理 nvm :node 的版本管理 node -v ---->查看node 的版本  (v---->version) npm -v ----->n ...

  5. vue-cli(vue脚手架)超详细教程

          都说Vue2简单上手容易,的确,看了官方文档确实觉得上手很快,除了ES6语法和webpack的配置让你感到陌生,重要的是思路的变换,以前用jq随便拿全局变量和修改dom的锤子不能用了,vu ...

  6. Linux命令之nohup和重定向

    用途:不挂断地运行命令.语法:nohup Command [ Arg ... ] [ & ]描述:nohup 命令运行由 Command 参数和任何相关的 Arg 参数指定的命令,忽略所有挂断 ...

  7. JS将图片转换成Base64码

    直接上代码 html页面代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  8. swift 实践- 06 -- UITextView

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  9. socket-WebSocket-HttpListener-TcpListener服务端客户端的具体使用案例

    /// <summary> /// 启动服务监听的ip和端口的主线程 /// </summary> /// <param name="tunnelPort&qu ...

  10. JS 实现的浏览器系统通知 iNotify.js

    注:本分非原创:信息来源 oschina 授权协议:MIT 开发语言:JavaScript 操作系统:跨平台 软件作者:同一种调调 iNotify.js 详细介绍 JS 实现浏览器的 title 闪烁 ...