获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody标签接收后面跟实体对象就行了,spring会帮你自动拼装成对象,如果Content-type设置成application/x-www-form-urlencoded;charset=utf-8就不能用spring的东西了,只能以常规的方式获取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 : <code>byte[]</code>
* @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[] 数组
* <pre>
* 举例:
* </pre>
* @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 请求内容
* <pre>
* 举例:
* </pre>
* @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);
}
方式二:通过获取Map的方式处理
这种刚方式存在弊端,如果json数据中存在=号,数据会在等号的地方断掉,后面的数据会被存储成map的values,需要重新拼装key和values的值,拼装成原来的json串
/**
* 方法说明 :通过获取map的方式
*/
@SuppressWarnings("rawtypes")
private String getParameterMap(HttpServletRequest request) {
Map map = request.getParameterMap();
String text = "";
if (map != null) {
Set set = map.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Entry) iterator.next();
if (entry.getValue() instanceof String[]) {
logger.info("==A==entry的key: " + entry.getKey());
String key = (String) entry.getKey();
if (key != null && !"id".equals(key) && key.startsWith("[") && key.endsWith("]")) {
text = (String) entry.getKey();
break;
}
String[] values = (String[]) entry.getValue();
for (int i = 0; i < values.length; i++) {
logger.info("==B==entry的value: " + values[i]);
key += "="+values[i];
}
if (key.startsWith("[") && key.endsWith("]")) {
text = (String) entry.getKey();
break;
}
} else if (entry.getValue() instanceof String) {
logger.info("==========entry的key: " + entry.getKey());
logger.info("==========entry的value: " + entry.getValue());
}
}
}
return text;
}
方式三:通过获取所有参数名的方式
这种方式也存在弊端 对json串中不能传特殊字符,比如/=, \=, /, ~等的这样的符号都不能有如果存在也不会读出来,他的模式和Map的方式是差不多的,也是转成Map处理的
/**
* 方法说明 :通过获取所有参数名的方式
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private String getParamNames(HttpServletRequest request) {
Map map = new HashMap();
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement(); String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() != 0) {
map.put(paramName, paramValue);
}
}
} Set<Map.Entry<String, String>> set = map.entrySet();
String text = "";
for (Map.Entry entry : set) {
logger.info(entry.getKey() + ":" + entry.getValue());
text += entry.getKey() + ":" + entry.getValue();
logger.info("text------->"+text);
}
if(text.startsWith("[") && text.endsWith("]")){
return text;
}
return "";
}
附上一点常用的Content-type的方式
application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据 application/json;charset=utf-8 -> json数据
最后附上发送方式的连接
http://blog.csdn.net/mingtianhaiyouwo/article/details/51381853
request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody标签接收后面跟实体对象就行了,spring会帮你自动拼装成对象,如果Content-type设置成application/x-www-form-urlencoded;charset=utf-8就不能用spring的东西了,只能以常规的方式获取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 : <code>byte[]</code>
- * @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[] 数组
- * <pre>
- * 举例:
- * </pre>
- * @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 请求内容
- * <pre>
- * 举例:
- * </pre>
- * @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);
- }
- }
方式二:通过获取Map的方式处理
这种刚方式存在弊端,如果json数据中存在=号,数据会在等号的地方断掉,后面的数据会被存储成map的values,需要重新拼装key和values的值,拼装成原来的json串
- /**
- * 方法说明 :通过获取map的方式
- */
- @SuppressWarnings("rawtypes")
- private String getParameterMap(HttpServletRequest request) {
- Map map = request.getParameterMap();
- String text = "";
- if (map != null) {
- Set set = map.entrySet();
- Iterator iterator = set.iterator();
- while (iterator.hasNext()) {
- Map.Entry entry = (Entry) iterator.next();
- if (entry.getValue() instanceof String[]) {
- logger.info("==A==entry的key: " + entry.getKey());
- String key = (String) entry.getKey();
- if (key != null && !"id".equals(key) && key.startsWith("[") && key.endsWith("]")) {
- text = (String) entry.getKey();
- break;
- }
- String[] values = (String[]) entry.getValue();
- for (int i = 0; i < values.length; i++) {
- logger.info("==B==entry的value: " + values[i]);
- key += "="+values[i];
- }
- if (key.startsWith("[") && key.endsWith("]")) {
- text = (String) entry.getKey();
- break;
- }
- } else if (entry.getValue() instanceof String) {
- logger.info("==========entry的key: " + entry.getKey());
- logger.info("==========entry的value: " + entry.getValue());
- }
- }
- }
- return text;
- }
方式三:通过获取所有参数名的方式
这种方式也存在弊端 对json串中不能传特殊字符,比如/=, \=, /, ~等的这样的符号都不能有如果存在也不会读出来,他的模式和Map的方式是差不多的,也是转成Map处理的
- /**
- * 方法说明 :通过获取所有参数名的方式
- */
- @SuppressWarnings({ "rawtypes", "unchecked" })
- private String getParamNames(HttpServletRequest request) {
- Map map = new HashMap();
- Enumeration paramNames = request.getParameterNames();
- while (paramNames.hasMoreElements()) {
- String paramName = (String) paramNames.nextElement();
- String[] paramValues = request.getParameterValues(paramName);
- if (paramValues.length == 1) {
- String paramValue = paramValues[0];
- if (paramValue.length() != 0) {
- map.put(paramName, paramValue);
- }
- }
- }
- Set<Map.Entry<String, String>> set = map.entrySet();
- String text = "";
- for (Map.Entry entry : set) {
- logger.info(entry.getKey() + ":" + entry.getValue());
- text += entry.getKey() + ":" + entry.getValue();
- logger.info("text------->"+text);
- }
- if(text.startsWith("[") && text.endsWith("]")){
- return text;
- }
- return "";
- }
附上一点常用的Content-type的方式
application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据 application/json;charset=utf-8 -> json数据
最后附上发送方式的连接
http://blog.csdn.net/mingtianhaiyouwo/article/details/51381853
获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据的更多相关文章
- (转)获取 request 中用POST方式"Content-type"是"application/x-www-form-urlencoded;charset=utf-8"发送的 json 数据
request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody ...
- spring mvc中几种获取request对象的方式
在使用spring进行web开发的时候,优势会用到request对象,用来获取访问ip.请求头信息等 这里收集几种获取request对象的方式 方法一:在controller里面的加参数 public ...
- 类型:JQuery;问题:ajax调用ashx文件;结果:ashx文件怎么获取$.ajax()方法发送的json数据
ashx文件怎么获取$.ajax()方法发送的json数据 作者:careful 和ajax相关 新浪微博QQ空间QQ微博百度搜藏腾讯朋友QQ收藏百度空间人人网开心网0 $.ajax({ t ...
- .NET获取文件的MIME类型(Content Type)
第一种:这种获取MIME类型(Content Type)的方法需要在.NET 4.5之后才能够支持,但是非常简单. 优点:方便快捷 缺点:只能在.NET 4.5之后使用 public FileResu ...
- Struts2获取request的几种方式汇总
Struts2获取request三种方法 struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象. 在Action中 ...
- 分享知识-快乐自己:Struts2中 获取 Request和Session
目录结构: POM: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnco ...
- JSON数据解析(GSON方式) (转)
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 在上一篇博文<Andro ...
- Android JSON数据解析(GSON方式)
要创建和解析JSON数据,也可以使用GSON来完成.GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库.使用GSON,可以很容易的将一串JSON数据转换为一个Jav ...
- 使用@RequestBody注解获取Ajax提交的json数据
最近在学习有关springMVC的知识,今天学习如何使用@RequestBody注解来获取Ajax提交的json数据内容. Ajax部分代码如下: 1 $(function(){ 2 $(" ...
随机推荐
- [Luogu] 四子连棋
https://www.luogu.org/problemnew/show/P2346 广搜 #include<iostream> #include<cstring> #inc ...
- codeforces 1272F dp+记录路径
题意 给出两个括号序列 \(S\) 和 \(T\),让你构造一个最短的合法括号序列使 \(S\) 和 \(T\) 是它的子序列. 分析 设 \(dp[i][j][k]\) 为这个最短的合法括号序列的前 ...
- CPU中断的机制概论
cpu的数学模型是图灵机,图灵机是一个线性运行指令的机器,它的执行序列是完全确定的,中断机制是使得cpu具有异步处理能力的机制,就是先暂停他的序列 来执行一些其他指令.具体来说就是,从硬件实现上,其 ...
- Echarts案例-折线图
一:先在官网下载 https://www.echartsjs.com/zh/download.html 然后再建立工程,导入这两个包: 写代码: <!DOCTYPE html> <h ...
- 单词拼接(dfs/回溯/递归)
单词拼接传送门 //单词拼接 #include<stdio.h> #include<string.h> #include<algorithm> using name ...
- 数据库groub by分组后,把多行数据合并成一行数据(Oracle、Postgres)
关键字 row_number() over (partition by) 例如,下面的数据, 这是按照name分组后,展示property值. 我们想得到这样的值; 第一步:将每一组的proper ...
- CISCO实验记录八:ACL访问控制
1.使用ACL实现免ping #access-list 100 deny icmp 192.168.0.1 0.0.0.0 192.168.1.2 0.0.0.0 #access-list 100 p ...
- 《梁宁·产品思维30讲》课程学习笔记(内含全套音频+ppt资料
科技进步.产品迭代.公司演化.组织变迁……不变的是用户的情绪和人性. 那些信奉“用户驱动”的人,从普通人变成了行业大佬,建立了自己的世界.乔布斯.马化腾.马云.雷军.张小龙.周鸿祎.傅盛……这些改变世 ...
- js常用时间转换函数
1.秒转换为时分秒 // 秒转换为时分秒 export function secExchangeMoment(value = 0){ let secondTime = parseInt(value); ...
- XM概述
概述: Extensible Markup Language: 可扩展的标记语言 特点: 语法很严格 标签自定义 作用: * 存储数据 * 做配置文件 * 用于进行数据传输 文档声明: 标示这个文档是 ...