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); } }

获取 request 中 json 数据的更多相关文章

  1. Go net/http获取body中json格式数据

    Go net/http获取body中json格式数据 package main import ( "encoding/json" "fmt" "io/ ...

  2. Jquery Ajax和getJSON获取后台普通Json数据和层级Json数据解析

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 基于BootStrap的initupload()实现Excel上传和获取excel中的数据

    简单说明:后边要做exl解析(还没做呢),所以先有一个excel的的上传以及获取excel中的数据,展示出来. 代码: //html代码 <div class="btn-group&q ...

  4. js中json数据简单处理(JSON.parse()和js中嵌套html)

    js中json数据简单处理(JSON.parse()和js中嵌套html) 一.总结 1.html中嵌套js:<script>js代码</script> 2.js中嵌套html ...

  5. Springboot中使用自定义参数注解获取 token 中用户数据

    使用自定义参数注解获取 token 中User数据 使用背景 在springboot项目开发中需要从token中获取用户信息时通常的方式要经历几个步骤 拦截器中截获token TokenUtil工具类 ...

  6. php获取数组中重复数据的两种方法

    分享下php获取数组中重复数据的两种方法. 1,利用php提供的函数,array_unique和array_diff_assoc来实现 <?php function FetchRepeatMem ...

  7. java获取request中的参数、java解析URL问号后的参数

    java获取request中的参数.java解析URL问号后的参数.有时候我们需要从request中获取参数,或者获取拼接在Url后面的参数,有时候一个一个去拿有点麻烦,一起拿出来放在一个map里面需 ...

  8. 获取request中的查询参数

    //获取request中的查询参数 public static Map<String, Object> getRequestParamsByMap(HttpServletRequest r ...

  9. jquery中json数据转换为字典

    首先在前台页面中的json数据为 var recipe = {}; recipe["name"] = $("#name").val(); recipe[&quo ...

随机推荐

  1. 【JSP报错】—— org.apache.jasper.JasperException: Unable to compile class for JSP

    org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: [52] ...

  2. [Xcode 实际操作]八、网络与多线程-(3)使用UIApplication对象拨打电话

    目录:[Swift]Xcode实际操作 本文将演示如何使用应用程序单例对象,拨打电话的功能. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] 注:需要使用真机进行测 ...

  3. Android近场通信---NFC基础(三)(转)

    转自 http://blog.csdn.net/think_soft/article/details/8180203 过滤NFC的Intent 要在你想要处理被扫描到的NFC标签时启动你的应用程序,可 ...

  4. TYVJ1424占卜DIY

    Description lyd学会了使用扑克DIY占卜.方法如下:一副去掉大小王的扑克共52张,打乱后均分为13堆,编号1~13,每堆4张,其中第13堆称作“生命牌”,也就是说你有4条命.这里边,4张 ...

  5. Centos 5.11 升级 Openssl

    由于Openssl版本较低,故此升级版本来解决一些小问题. 1:查看openssl版本: [root@server-008 ~]# openssl versionOpenSSL 0.9.8e-fips ...

  6. Linux —— 文件搜索命令

    文件搜索命令(配置文件/etc/updatedb.conf) locate 文件名 搜索速度非常快 在后台数据库中按照文件名搜索 updatedb 更新数据库 只可以按照文件名搜索 whereis 命 ...

  7. C++中this指针的理解

    C++中this指针的理解 先要理解class的意思.class应该理解为一种类型,象int,char一样,是用户自定义的类型.用这个类型可以来声明一个变量,比如int x, myclass my等等 ...

  8. [LOJ#10042] 收集雪花

    题目链接: 点我 题目分析: 双指针扫描可以保证在\(O(n)\)的时间复杂度内处理这道题.另外,虽然这个题标签是\(hash\),但是字符串\(hash\)是可以卡掉的,所以建议直接离散化. 维护两 ...

  9. Spring+JCaptcha验证码使用示例

    1,导入jcaptcha.jar包,这里用的是1.0版本 2,编写captcha-context.xml配置文件(非必须,可在spring配置文件中直接添加): <?xml version=&q ...

  10. jQuery取得/设置select的值

    本来以为jQuery("#select1").val();是取得选中的值, 那么jQuery("#select1").text();就是取得的文本. 这是不正确 ...