Http 请求工具类

public class AppInterFaceUtil {

	/**
	 * 获取request 传的参数
	 * 
	 * @param request
	 * @param method
	 *            post/get
	 * @return
	 * @throws IOException
	 */
	public static String getInputStreamParameter(HttpServletRequest request) throws IOException {
		String data = "";
		String method = request.getMethod();
		if ("POST".equals(method)) {
			BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
			String line = null;
			StringBuffer sb = new StringBuffer();
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			data = sb.toString();
		} else if ("GET".equals(method)) {
			data = request.getParameter("data");
		}
		if (StringUtils.isBlank(data)) {
			Map<String, String[]> mapdata = request.getParameterMap();
			if (null != mapdata) {
				HashMap<String, Object> dest = new HashMap<String, Object>(mapdata.size());
				for (Map.Entry<String, String[]> entry : mapdata.entrySet()) {
					if (entry.getValue() != null) {
						if (entry.getValue().length == 1) {
							dest.put(entry.getKey(), entry.getValue()[0]);
						} else {
							dest.put(entry.getKey(), entry.getValue());
						}
					}
				}
				data = JSON.toJSONString(dest);
			}
		}
		return data;
	}

	/**
	 * 设置response
	 * 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 */
	public static void setCharacterEncoding(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);
	}

	/**
	 * 获取请求的ip
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {// 比较时忽略大小写
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}

String jsonParam = AppInterFaceUtil.getInputStreamParameter(req);

 取得json 格式的请求参数  

/**
     * 用户查询文件列表
     * @param account 用户工号
     * @param systemCode 系统代码
     * @param file 文件对象
     * @return
     */
    @RequestMapping(value = "/file/{account}/userFileList/")
    @ResponseBody
    public Object userFileList(@PathVariable("account")String account, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        try { 
             
            String data = AppInterFaceUtil.getInputStreamParameter(request);
            //参数错误
            if(StringUtils.isBlank(data)){
                return ResultMessage.packFailure(FileCodeMsgUtils.NOT_PARAM.getCode(), FileCodeMsgUtils.NOT_PARAM.getMsg());
            }

            Map<String,Object> paramMap=JSON.parseObject(data);
}

调用

posted on 2016-10-18 17:04  左侧码工  阅读(197)  评论(0)    收藏  举报