加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webService 接受提交的JSon数据

发布时间:2020-12-16 21:55:48 所属栏目:安全 来源:网络整理
导读:1、controller @RequestMapping(value = "saveJson")@ResponseBodypublic Map saveJson(HttpServletRequest request) throws IOException {Map map = new HashMapString,Object();String submitMethod = request.getMethod();String data;if (submitMethod.e

1、controller

@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、工具类

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读