All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.jiangzeyin.controller.base.RequestUtil Maven / Gradle / Ivy

There is a newer version: 1.1.11
Show newest version
package cn.jiangzeyin.controller.base;

import cn.jiangzeyin.StringUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 请求工具类
 *
 * @author jiangzeyin
 */
public final class RequestUtil {
    /**
     * 更加url 获取get 参数
     *
     * @param url url
     * @return map
     * @throws UnsupportedEncodingException 编码异常
     */
    public static Map convertUrlMap(String url) throws UnsupportedEncodingException {
        if (StringUtil.isEmpty(url))
            return null;
        Map mapRequest = new HashMap<>();
        url = url.trim().toLowerCase();
        String[] arrSplit = url.split("[?]");
        if (arrSplit.length <= 1)
            return mapRequest;
        String allParam = arrSplit[1];
        if (StringUtil.isEmpty(allParam))
            return mapRequest;
        arrSplit = allParam.split("[&]");
        for (String strSplit : arrSplit) {
            String[] arrSplitEqual = strSplit.split("[=]");
            //解析出键值
            if (arrSplitEqual.length > 1) {
                //正确解析
                mapRequest.put(arrSplitEqual[0], URLDecoder.decode(arrSplitEqual[1], "UTF-8"));
            } else {
                if (!arrSplitEqual[0].equals("")) {
                    //只有参数没有值,不加入
                    mapRequest.put(arrSplitEqual[0], "");
                }
            }
        }
        return mapRequest;
    }


    /**
     * @param request req
     * @param name    name
     * @return cookie
     * @author jiangzeyin
     */
    public static Cookie getCookieByName(HttpServletRequest request, String name) {
        Map cookieMap = readCookieMap(request);
        return cookieMap.getOrDefault(name, null);
    }

    /**
     * @param request req
     * @return map
     * @author jiangzeyin
     */
    private static Map readCookieMap(HttpServletRequest request) {
        Map cookieMap = new HashMap<>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }

    /**
     * 获取headr
     *
     * @param request req
     * @return map
     * @author jiangzeyin
     */
    public static Map getHeaderMapValues(HttpServletRequest request) {
        Enumeration enumeration = request.getHeaderNames();
        Map headerMapValues = new HashMap<>();
        if (enumeration != null)
            for (; enumeration.hasMoreElements(); ) {
                String name = enumeration.nextElement();
                headerMapValues.put(name, request.getHeader(name));
            }
        return headerMapValues;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy