java request get json数据_Java中,获取request中json数据
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
/**
* request 对象的相关操作
* @author zhangtengda
* @version 1.0
* @created 2015年5月2日 下午8:25:43
*/
public class GetRequestJsonUtils {
/***
* 获取 request 中 json 字符串的内容
*
* @param request
* @return : byte[]
* @throws IOException
*/
public static String getRequestJsonString(HttpServletRequest request)
throws IOException {
String submitMehtod = request.getMethod();
// GET
if (submitMehtod.equals("GET")) {
return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");
// POST
} else {
return getRequestPostStr(request);
}
}
/**
* 描述:获取 post 请求的 byte[] 数组
*
* 举例:
*
* @param request
* @return
* @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;
}
/**
* 描述:获取 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);
}
}
java request get json数据_Java中,获取request中json数据相关推荐
- java匹配字符串所在位置_Java:获取字符串中匹配项位置的方法?
宝慕林4294392 执行此操作的方法家族是:int indexOf(String str)indexOf(String str, int fromIndex)int lastIndexOf(Stri ...
- mysql java 获取周_Java中获取Mysql中datetime类型的数据
由于Java中的日期类型只有Date类型,而Mysql中即有date型,又有datetime型,当我们想要在java中获取Mysql中datetime类型的数据或向Mysql数据库中插入datetim ...
- java定时器任务中获取request对象 @Scheduled 获取request 对象 quartz中获取request对象
java定时器任务中获取request对象 @Scheduled 获取request 对象 quartz中获取request对象 一.问题描述 1.在java定时任务中,使用 @Scheduled 注 ...
- Spring 中获取 request 的几种方法,及其线程安全性分析
概述 在使用Spring MVC开发Web系统时,经常需要在处理请求时使用request对象,比如获取客户端ip地址.请求的url.header中的属性(如cookie.授权信息).body中的数据等 ...
- Spring中获取request的方法及其线程安全性分析
在使用Spring MVC开发Web系统时,经常需要在处理请求时使用request对象,比如获取客户端ip地址.请求的url.header中的属性(如cookie.授权信息).body中的数据等.由于 ...
- Spring MVC中获取Request的方法及分析
一.概述 在使用Spring MVC开发Web系统时,经常需要在处理请求时使用request对象,比如获取客户端IP地址.请求的URL.header中的属性(如cookie.授权信息).body中的数 ...
- SpringMvc4中获取request、response对象的方法
springMVC4中获取request和response对象有以下两种简单易用的方法: 1.在control层获取 在control层中获取HttpServletRequest和HttpServle ...
- spring MVC中获取request和response
spring MVC中获取request和response: Java代码 HttpServletRequest request = ((ServletRequestAttributes) Req ...
- server sql 数据总行数_SqlServer中获取数据库中每个表的行数
CREATE TABLE #RowCounts(NumberOfRows BIGINT,TableName VARCHAR(128)) EXEC sp_MSForEachTable 'INSERT I ...
- Java中获取数据库中两个时间的相差秒数
场景 Java中获取数据库中结束时间与开始时间相差的秒数. 实现 Long betweenTime=0l;betweenTime =((b.getFinishTime().getTime()-b.ge ...
最新文章
- python操作mongodb进行读写
- (水题)987654321 problem -- SGU 107
- C++STL之next_permutation使用
- 成功解决连接SQL输出出现中文乱码问题(10001, 'oracle¿ìËÙÈëÃÅ', 'Íõº£ÁÁ', 'Ë®Àû³ö°æÉç',
- eclipse序列化生成serialVersionUID
- Hello Python程序演练
- test of ui5 duplicate control id
- 蓝桥杯评测_前n项和
- Android add external jar
- EasyExcel简单使用
- 【Kafka】Kafka幂等性原理及实现剖析
- ShadeGraph教程之节点详解2:Channel Nodes
- spring 事物配置几种
- HttpClient 4.0.x Tips
- js禁止输入框输入特殊符号或emoji表情
- AAC的ADTS头解析
- 视频分类之 UCF-101 上的 CNN 方法详解
- 第3章 远程连接管理Linux实践
- eclipse的中文版本安装方法
- 让 Google 搜索到自己的博客
热门文章
- ARM:钒和铁替代固氮酶的前世今生
- R语言ggplot2可视化绘制Marimekko/Mosaic图实战:自定义函数绘制Marimekko/Mosaic图(添加数值、标题、色彩配置)、ggmosaic包绘制Marimekko图
- R语言dplyr包将dataframe中的NA值替换(replace)为0实战:所有NA值替换(replace)为0、具体列的NA值替换(replace)为0、若干列的NA值替换(replace)为0
- python使用openweathermap API获取全世界主要城市天气信息
- prefetch下载SRA 数据
- 在高中生物里我们提到和遗传有关的物质有染色体,染色质,基因,dna
- Oxford Nanopore碱基识别(basecalling)软件性能大比拼
- SQLite - 下载安装与使用(Command Line Shell For SQLite)
- Oracle Database 11g的使用
- Assembly--软件PBcR和Canu