前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >webService 接受提交的JSon数据

webService 接受提交的JSon数据

作者头像
week
发布2018-08-24 16:25:54
1K0
发布2018-08-24 16:25:54
举报
文章被收录于专栏:用户画像用户画像

1、controller

代码语言:javascript
复制
@RequestMapping(value = "saveJson")
@ResponseBody
public Map saveJson(HttpServletRequest request) throws IOException {
		Map map = new HashMap<String,Object>();
		String submitMethod = request.getMethod();
		String data;
		if (submitMethod.equals("GET")) {
			data= new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");
		} else {
			data= getRequestPostStr(request);
		}
                map.put("data",data);
                return map;
}

2、工具类

代码语言:javascript
复制
package com.thinkgem.jeesite.modules.util;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by boxiaotong on 2017/1/17.
 */
public class RequestUtil {

    /**
     * 描述:获取 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);
    }
    /**
     * 描述:获取 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;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年01月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档