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. import javax.servlet.http.HttpServletRequest;
  3. /**
  4. * request 对象的相关操作
  5. * @author zhangtengda
  6. * @version 1.0
  7. * @created 2015年5月2日 下午8:25:43
  8. */
  9. public class GetRequestJsonUtils {
  10. /***
  11. * 获取 request 中 json 字符串的内容
  12. *
  13. * @param request
  14. * @return : <code>byte[]</code>
  15. * @throws IOException
  16. */
  17. public static String getRequestJsonString(HttpServletRequest request)
  18. throws IOException {
  19. String submitMehtod = request.getMethod();
  20. // GET
  21. if (submitMehtod.equals("GET")) {
  22. return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");
  23. // POST
  24. } else {
  25. return getRequestPostStr(request);
  26. }
  27. }
  28. /**
  29. * 描述:获取 post 请求的 byte[] 数组
  30. * <pre>
  31. * 举例:
  32. * </pre>
  33. * @param request
  34. * @return
  35. * @throws IOException
  36. */
  37. public static byte[] getRequestPostBytes(HttpServletRequest request)
  38. throws IOException {
  39. int contentLength = request.getContentLength();
  40. if(contentLength<0){
  41. return null;
  42. }
  43. byte buffer[] = new byte[contentLength];
  44. for (int i = 0; i < contentLength;) {
  45. int readlen = request.getInputStream().read(buffer, i,
  46. contentLength - i);
  47. if (readlen == -1) {
  48. break;
  49. }
  50. i += readlen;
  51. }
  52. return buffer;
  53. }
  54. /**
  55. * 描述:获取 post 请求内容
  56. * <pre>
  57. * 举例:
  58. * </pre>
  59. * @param request
  60. * @return
  61. * @throws IOException
  62. */
  63. public static String getRequestPostStr(HttpServletRequest request)
  64. throws IOException {
  65. byte buffer[] = getRequestPostBytes(request);
  66. String charEncoding = request.getCharacterEncoding();
  67. if (charEncoding == null) {
  68. charEncoding = "UTF-8";
  69. }
  70. return new String(buffer, charEncoding);
  71. }
  72. }
//在controll中进行调用
String content = HttpJsonUtils.getPostByApplicationForm(request);
LOGGER.info("content*************" + content);
JSONObject jsObject = JSONObject.fromObject(content);
try {
ipbegin = jsObject.getLong("ipbegin");
ipend = jsObject.getLong("ipend");
province = jsObject.getString("province");
isopen = jsObject.getString("isopen");
opertime = jsObject.getString("opertime");
sign = jsObject.getString("sign");
} catch (Exception e) {
// TODO: handle exception
e.getMessage();
LOGGER.info("发生错误*****" + e.getMessage());
}
LOGGER.info("ipbegin********************************" + ipbegin);
LOGGER.info("ipend********************************" + ipend);
LOGGER.info("province********************************" + province);
LOGGER.info("isopen********************************" + isopen);
LOGGER.info("opertime********************************" + opertime);
LOGGER.info("sign********************************" + sign);

  

方式二:通过获取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. String[] paramValues = request.getParameterValues(paramName);
  11. if (paramValues.length == 1) {
  12. String paramValue = paramValues[0];
  13. if (paramValue.length() != 0) {
  14. map.put(paramName, paramValue);
  15. }
  16. }
  17. }
  18. Set<Map.Entry<String, String>> set = map.entrySet();
  19. String text = "";
  20. for (Map.Entry entry : set) {
  21. logger.info(entry.getKey() + ":" + entry.getValue());
  22. text += entry.getKey() + ":" + entry.getValue();
  23. logger.info("text------->"+text);
  24. }
  25. if(text.startsWith("[") && text.endsWith("]")){
  26. return text;
  27. }
  28. return "";
  29. }

附上一点常用的Content-type的方式

application/x-javascript text/xml->xml数据 application/x-javascript->json对象 application/x-www-form-urlencoded->表单数据 application/json;charset=utf-8 -> json数据

最后附上发送方式的连接

http://www.cnblogs.com/SimonHu1993/p/7295765.html

(转)获取 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. Python 升级致yum 问题,pip 异常

    升级 Python 导致 yum 和 pip 异常: 一些storm 和 自定义项目 需要升级python版本:Linux 系统默认是2.6 版本 ,所以需要根据业务进行升级操作:Python 官方下 ...

  2. Java SE之Java工作原理

     在Java中引入了虚拟机的概念,即在机器和编译程序之间加入了一层抽象的虚拟的机器.这台虚拟的机器在任何平台上都提供给编译程序一个的共同的接口.编译程序只需要面向虚拟机,生成虚拟机能够理解的代码,然后 ...

  3. [CERC2016]机棚障碍 Hangar Hurdles(kruskal重构树+树上倍增)

    题面 \(solution:\) 某蒟蒻的心路历程: 这一题第一眼感觉很奇怪 带障碍物的图,最大的集装箱? 首先想到的就是限制我集装箱大小条件的是什么: 如果我要在某一个点上放一个集装箱且使它最大, ...

  4. POI读取Excel(xls、xlsx均可以)——(四)

    maven构建的项目-->pom.xml文件 eclipse提供Dependencies直接添加依赖jar包的工具:直接搜索poi以及poi-ooxml即可,maven会自动依赖需要的jar包: ...

  5. CMake 示例

    1.需求 [1].使用第三方动/静太库 [2].本身代码部分编译为动/静态库 [3]多项目管理 原文转自:http://blog.csdn.net/shuyong1999/article/detail ...

  6. 八大最安全的Linux发行版,具备匿名功能,做服务器的首选,web,企业服务器等

    10 best Linux distros for privacy fiends and security buffs in 2017 Introduction The awesome operati ...

  7. phantomjs 中如何使用xpath

    function getNodeInfo(inputcsvPath) { var htmlnodeInfo = page.evaluate(function () { //_Ltg var XPATH ...

  8. ES系列十三、Elasticsearch Suggester API(自动补全)

    1.概念 1.补全api主要分为四类 Term Suggester(纠错补全,输入错误的情况下补全正确的单词) Phrase Suggester(自动补全短语,输入一个单词补全整个短语) Comple ...

  9. opencv学习笔记(九)Mat 访问图像像素的值

    对图像的像素进行访问,可以实现空间增强,反色,大部分图像特效系列都是基于像素操作的.图像容器Mat是一个矩阵的形式,一般情况下是二维的.单通道灰度图一般存放的是<uchar>类型,其数据存 ...

  10. 罗克韦尔 Allen-Bradley MicroLogix 1400 查看、设置IP

    =============================================== 2019/4/14_第1次修改                       ccb_warlock == ...