获取 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





转载于:https://www.cnblogs.com/jpfss/p/10966352.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值