获取HttpServletRequest请求Body中的内容
在实际开发过程中,经常需要从 HttpServletRequest 中读取HTTP请求的body内容,俗话说的好”好记性不如烂笔头“,特在此将其读取方法记录一下。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream; import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest; public class HttpServletRequestReader
{ // 字符串读取
// 方法一
public static String ReadAsChars(HttpServletRequest request)
{ BufferedReader br = null;
StringBuilder sb = new StringBuilder("");
try
{
br = request.getReader();
String str;
while ((str = br.readLine()) != null)
{
sb.append(str);
}
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (null != br)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return sb.toString();
} // 方法二
public static void ReadAsChars2(HttpServletRequest request)
{
InputStream is = null;
try
{
is = request.getInputStream();
StringBuilder sb = new StringBuilder();
byte[] b = new byte[4096];
for (int n; (n = is.read(b)) != -1;)
{
sb.append(new String(b, 0, n));
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (null != is)
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
} } // 二进制读取
public static byte[] readAsBytes(HttpServletRequest request)
{ int len = request.getContentLength();
byte[] buffer = new byte[len];
ServletInputStream in = null; try
{
in = request.getInputStream();
in.read(buffer, 0, len);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (null != in)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return buffer;
} }
注意:HttpServletRequest 请求中的 body 内容仅能调用 request.getInputStream(), request.getReader()和request.getParameter("key") 方法读取一次,重复读取会报 java.io.IOException: Stream closed 异常。
转自:https://blog.csdn.net/pengjunlee/article/details/79416687
获取HttpServletRequest请求Body中的内容的更多相关文章
- 035.[转] 获取HttpServletRequest请求Body中的内容
注意: HttpServletRequest 请求中的 body 内容仅能调用 request.getInputStream(), request.getReader()和request.getPar ...
- javascript怎么获取指定url网页中的内容
javascript怎么获取指定url网页中的内容 一.总结 一句话总结:推荐jquery中ajax,简单方便. 1.js能跨域操作么? javascript出于安全机制不允许跨域操作的. 二.用ph ...
- SpringMVC源码之Handler注册、获取以及请求controller中方法
总结 对requestMappingHandlerMapping进行initializeBean时register Handler http开始请求时,initHandlerMappings,Disp ...
- linux shell 脚本获取和替换文件中特定内容
1.从一串字符串中获取特定的信息 要求1:获取本机IP:menu.lst为系统镜象的IP配置文件,需要从中获取到本机IP信息(从文件获取信息) timeout title live find --se ...
- win32 获取 HotKey 控件中的内容(HKM_GETHOTKEY)
windows给我们提供了一个对话框控件HotKey非常好用,在设置热键的时候用起来很爽,但是一直百度就是没找到在win32下怎样通过消息获取这个控件里面的内容,找到的都是用MFC封装好的控件类来操作 ...
- [转]iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
一.在工程中添加AddressBook.framework和AddressBookUI.framework 二.获取通讯录 1.在infterface中定义数组并在init方法中初始化 ? 1 2 3 ...
- 正则获取 某段 DIV 中 的内容
string html = "<div class='aa'><div class='left'>324324<div>dsfsdf</div> ...
- [技巧篇]11.JavaScript原生态如何获取浏览器请求地址中的参数
var getAccessParams = function(){ var i,ilen,strs,keyName,keyValue, params={}, path = window.locatio ...
- 获取HTTP请求头中的地址
技术交流群:233513714 public static String getIpAddr(HttpServletRequest request) { String ip = request.get ...
随机推荐
- 在AngularJS中使用谷歌地图把当前位置显示出来
如何使用谷歌地图把当前位置显示出来呢? --在html5中,为我们提供了navigator.geolocation.getCurrentPosition(f1, f2)函数,f1是定位成功调用的函数, ...
- android:单位和尺寸
为了要让程序拥有更好的屏幕适配能力,在指定控件和布局大小的时候 最好使用 match_parent 和 wrap_content,尽量避免将控件的宽和高设定一个固定值.不过在 有些情况下,仅仅使用 m ...
- 使用JCOOKIES创建http cookie
jCookies,一个功能强大的操作http cookie的jquery插件,他能够让你存储任何数据类型如:字符串,数组,对象等.它通过JavaScript存储Cookies,然后通过服务器端代码如: ...
- web打印控件Lodop轻松输出清晰的图表和条码
一.仅用两行语句实现极其复杂的图表打印.类似如下两句: LODOP.ADD_PRINT_CHART(0,0,400,400,5,document.getElementByI d('table001') ...
- ArrayList vs LinkedList 空间占用
空间占用上,ArrayList完胜 看下两者的内存占用图 这三个图,横轴是list长度,纵轴是内存占用值.两条蓝线是LinkedList,两条红线是ArrayList,可以看到,LinkedLis ...
- CucumberPeople 1.3.2 发布
CucumberPeople 网站: http://alterhu.github.io/CucumberPeople/ This eclipse plugin based on RubyMine ,a ...
- Window下对nodejs多版本管理GNVM
Windows下对nodejs多版本的管理,实现随意切换! 官方地址: https://github.com/Kenshin/gnvm http://ksria.com/gnvm/ 01.下载GNVM ...
- device eth0 does not seem to be present, delaying initialization(转)
vmlite虚拟机启动出错,就把这个虚拟机删除掉重新建立,系统虚拟硬盘使用之前的,启动系统后不能上网,通过ifconfig查看网卡没启动,遂启动网卡服务,但是出错,就是:device eth0 doe ...
- [k8s]kube-dns/dashboard排错历险记(含sa加载用法/集群搭建)
kube-dns原理 参考: 组件架构看这个就够了 http://cizixs.com/2017/04/11/kubernetes-intro-kube-dns 设置细节看这个就够了 http://b ...
- 【Python】Python的安装与个人使用记录
下载 从官网上下载,目前,最新版是Python3,基于项目需求,我们使用的是Python2. 我是在CentOS上安装,下载的是Python-2.7.9.tgz. 安装 tar -zxvf Pytho ...