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串了

方式一:通过流的方方式

  1.  
    import java.io.IOException;
  2.  
     
  3.  
    import javax.servlet.http.HttpServletRequest;
  4.  
     
  5.  
     
  6.  
    /**
  7.  
    * request 对象的相关操作
  8.  
    * @author zhangtengda
  9.  
    * @version 1.0
  10.  
    * @created 2015年5月2日 下午8:25:43
  11.  
    */
  12.  
    public class GetRequestJsonUtils {
  13.  
     
  14.  
    /***
  15.  
    * 获取 request 中 json 字符串的内容
  16.  
    *
  17.  
    * @param request
  18.  
    * @return : <code>byte[]</code>
  19.  
    * @throws IOException
  20.  
    */
  21.  
    public static String getRequestJsonString(HttpServletRequest request)
  22.  
    throws IOException {
  23.  
    String submitMehtod = request.getMethod();
  24.  
    // GET
  25.  
    if (submitMehtod.equals("GET")) {
  26.  
    return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");
  27.  
    // POST
  28.  
    } else {
  29.  
    return getRequestPostStr(request);
  30.  
    }
  31.  
    }
  32.  
     
  33.  
    /**
  34.  
    * 描述:获取 post 请求的 byte[] 数组
  35.  
    * <pre>
  36.  
    * 举例:
  37.  
    * </pre>
  38.  
    * @param request
  39.  
    * @return
  40.  
    * @throws IOException
  41.  
    */
  42.  
    public static byte[] getRequestPostBytes(HttpServletRequest request)
  43.  
    throws IOException {
  44.  
    int contentLength = request.getContentLength();
  45.  
    if(contentLength<0){
  46.  
    return null;
  47.  
    }
  48.  
    byte buffer[] = new byte[contentLength];
  49.  
    for (int i = 0; i < contentLength;) {
  50.  
     
  51.  
    int readlen = request.getInputStream().read(buffer, i,
  52.  
    contentLength - i);
  53.  
    if (readlen == -1) {
  54.  
    break;
  55.  
    }
  56.  
    i += readlen;
  57.  
    }
  58.  
    return buffer;
  59.  
    }
  60.  
     
  61.  
    /**
  62.  
    * 描述:获取 post 请求内容
  63.  
    * <pre>
  64.  
    * 举例:
  65.  
    * </pre>
  66.  
    * @param request
  67.  
    * @return
  68.  
    * @throws IOException
  69.  
    */
  70.  
    public static String getRequestPostStr(HttpServletRequest request)
  71.  
    throws IOException {
  72.  
    byte buffer[] = getRequestPostBytes(request);
  73.  
    String charEncoding = request.getCharacterEncoding();
  74.  
    if (charEncoding == null) {
  75.  
    charEncoding = "UTF-8";
  76.  
    }
  77.  
    return new String(buffer, charEncoding);
  78.  
    }
  79.  
     
  80.  
    }

方式二:通过获取Map的方式处理

这种刚方式存在弊端,如果json数据中存在=号,数据会在等号的地方断掉,后面的数据会被存储成map的values,需要重新拼装key和values的值,拼装成原来的json串

  1.  
    /**
  2.  
    * 方法说明 :通过获取map的方式
  3.  
    */
  4.  
    @SuppressWarnings("rawtypes")
  5.  
    private String getParameterMap(HttpServletRequest request) {
  6.  
    Map map = request.getParameterMap();
  7.  
    String text = "";
  8.  
    if (map != null) {
  9.  
    Set set = map.entrySet();
  10.  
    Iterator iterator = set.iterator();
  11.  
    while (iterator.hasNext()) {
  12.  
    Map.Entry entry = (Entry) iterator.next();
  13.  
    if (entry.getValue() instanceof String[]) {
  14.  
    logger.info("==A==entry的key: " + entry.getKey());
  15.  
    String key = (String) entry.getKey();
  16.  
    if (key != null && !"id".equals(key) && key.startsWith("[") && key.endsWith("]")) {
  17.  
    text = (String) entry.getKey();
  18.  
    break;
  19.  
    }
  20.  
    String[] values = (String[]) entry.getValue();
  21.  
    for (int i = 0; i < values.length; i++) {
  22.  
    logger.info("==B==entry的value: " + values[i]);
  23.  
    key += "="+values[i];
  24.  
    }
  25.  
    if (key.startsWith("[") && key.endsWith("]")) {
  26.  
    text = (String) entry.getKey();
  27.  
    break;
  28.  
    }
  29.  
    } else if (entry.getValue() instanceof String) {
  30.  
    logger.info("==========entry的key: " + entry.getKey());
  31.  
    logger.info("==========entry的value: " + entry.getValue());
  32.  
    }
  33.  
    }
  34.  
    }
  35.  
    return text;
  36.  
    }

方式三:通过获取所有参数名的方式

这种方式也存在弊端  对json串中不能传特殊字符,比如/=, \=, /, ~等的这样的符号都不能有如果存在也不会读出来,他的模式和Map的方式是差不多的,也是转成Map处理的

  1.  
    /**
  2.  
    * 方法说明 :通过获取所有参数名的方式
  3.  
    */
  4.  
    @SuppressWarnings({ "rawtypes", "unchecked" })
  5.  
    private String getParamNames(HttpServletRequest request) {
  6.  
    Map map = new HashMap();
  7.  
    Enumeration paramNames = request.getParameterNames();
  8.  
    while (paramNames.hasMoreElements()) {
  9.  
    String paramName = (String) paramNames.nextElement();
  10.  
     
  11.  
    String[] paramValues = request.getParameterValues(paramName);
  12.  
    if (paramValues.length == 1) {
  13.  
    String paramValue = paramValues[0];
  14.  
    if (paramValue.length() != 0) {
  15.  
    map.put(paramName, paramValue);
  16.  
    }
  17.  
    }
  18.  
    }
  19.  
     
  20.  
    Set<Map.Entry<String, String>> set = map.entrySet();
  21.  
    String text = "";
  22.  
    for (Map.Entry entry : set) {
  23.  
    logger.info(entry.getKey() + ":" + entry.getValue());
  24.  
    text += entry.getKey() + ":" + entry.getValue();
  25.  
    logger.info("text------->"+text);
  26.  
    }
  27.  
    if(text.startsWith("[") && text.endsWith("]")){
  28.  
    return text;
  29.  
    }
  30.  
    return "";
  31.  
    }

附上一点常用的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 数据的更多相关文章

  1. (转)获取 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 ...

  2. spring mvc中几种获取request对象的方式

    在使用spring进行web开发的时候,优势会用到request对象,用来获取访问ip.请求头信息等 这里收集几种获取request对象的方式 方法一:在controller里面的加参数 public ...

  3. 类型:JQuery;问题:ajax调用ashx文件;结果:ashx文件怎么获取$.ajax()方法发送的json数据

    ashx文件怎么获取$.ajax()方法发送的json数据 作者:careful 和ajax相关     新浪微博QQ空间QQ微博百度搜藏腾讯朋友QQ收藏百度空间人人网开心网0 $.ajax({  t ...

  4. .NET获取文件的MIME类型(Content Type)

    第一种:这种获取MIME类型(Content Type)的方法需要在.NET 4.5之后才能够支持,但是非常简单. 优点:方便快捷 缺点:只能在.NET 4.5之后使用 public FileResu ...

  5. Struts2获取request的几种方式汇总

    Struts2获取request三种方法 struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象. 在Action中 ...

  6. 分享知识-快乐自己:Struts2中 获取 Request和Session

    目录结构: POM: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnco ...

  7. JSON数据解析(GSON方式) (转)

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 在上一篇博文<Andro ...

  8. Android JSON数据解析(GSON方式)

    要创建和解析JSON数据,也可以使用GSON来完成.GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库.使用GSON,可以很容易的将一串JSON数据转换为一个Jav ...

  9. 使用@RequestBody注解获取Ajax提交的json数据

    最近在学习有关springMVC的知识,今天学习如何使用@RequestBody注解来获取Ajax提交的json数据内容. Ajax部分代码如下: 1 $(function(){ 2 $(" ...

随机推荐

  1. 【csp模拟赛6】相遇--LCA

    对于30%的数据:暴力枚举判断 对于60%的数据:还是暴力枚举,把两条路径都走一遍计一下数就行,出现一个点被访问两次即可判定重合 对于100%的数据:找出每条路径中距离根最近的点(lca),判断这个点 ...

  2. 8月清北学堂培训 Day3

    今天是赵和旭老师的讲授~ 状态压缩 dp 状态压缩是设计 dp 状态的一种方式. 当普通的 dp 状态维数很多(或者说维数与输入数据有关),但每一维总量很少时,可以将多维状态压缩为一维来记录. 这种题 ...

  3. 内存管理4-Autoreleasepool

    自动释放池是OC里面的一种内存回收机制,一般可以将一些临时变量添加到自动释放池中,统一回收释放,当自动释放池销毁时,池里面的所有对象都会调用一次release,也就是计数器会减1,但是自动释放池被销毁 ...

  4. codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...

  5. flask + nginx + uwsgi + ubuntu18.04部署python restful接口

    目录 参考链接 效果展示 一.准备工作 1.1 可运行的python demo: 1.2 更新系统环境 二.创建python虚拟环境 三.设置flask应用程序 四.配置uWSGI 五.设置系统启动 ...

  6. Gi命令行操作

    一.本地库初始化 命令:git init 效果: 注意:.git 目录中存放的是本地库相关的子目录和文件,不要删除,也不要胡乱修改 二.设置签名 形式 用户名:user Email 地址:user@1 ...

  7. js去掉字符串中的所有空格

    1.使用js去掉字符串中的所有空格 1.1.定义一个去空格函数方法 function Trim(str,is_global){ var result; result = str.replace(/(^ ...

  8. REGIONAL SCRUM GATHERING(RSG)2019 CHINA.

    欢迎参加 REGIONAL SCRUM GATHERING(RSG)2019 CHINA. 今年RSG将于2019年8月23号~24号,在北京新世界酒店举办.在为期2天的敏捷大会中,将有接近40位国内 ...

  9. Swift 基本语法

    如果创建的是 OS X playground 需要引入 Cocoa : import Cocoa /* 我的第一个 Swift 程序 */ var myString = "Hello, Wo ...

  10. React Native使用code-push实现热更新

    这里就不记录了,下面的传送门介绍的通俗易懂,很详细,一步一步很容易实现成功. http://www.jianshu.com/p/f8689ccf0007