美文网首页
对url的页面路径和参数的解析

对url的页面路径和参数的解析

作者: 自由主义者 | 来源:发表于2020-03-03 17:21 被阅读0次
    import javax.servlet.http.HttpServletRequest;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.Map;
    
    public class UrlUtil {
        /**
         * 解析出url请求的路径
         * @param strURL url地址
         * @return url路径
         */
        public static String getUrlPage(String strURL) {
            String strPage = null;  //请求的页面
            String[] arrSplit = null;
            strURL = strURL.trim();
            if (strURL.length() > 0) {
                arrSplit = strURL.split("[?]");
                if (arrSplit.length > 1) {
                    if (arrSplit[0] != null) {
                        strPage = arrSplit[0];
                    }
                }
            }
            return strPage;
        }
    
        /**
         * 解析出url参数中的键值对,放入map中
         * 注:URL参数中不能有"&",但可以有"="!!!("="已经做了处理)
         * @param url url地址
         * @return url请求参数部分
         */
        public static Map<String, String> getUrlParamsMap(String url) {
            Map<String, String> requestParams = new HashMap<>();
            String[] arrSplit = null;
    
            String strUrlParams = getUrlParams(url);
            if (strUrlParams == null) {
                return requestParams;
            }
            arrSplit = strUrlParams.split("&");
            for (String strSplit:arrSplit) {
                String[] arrSplitEqual = null;
                arrSplitEqual = strSplit.split("[=]");
                if (arrSplitEqual.length > 1) {
                    //因为参数中包含url,url中也会有"="号,所以根据"="拆分会得到长度大于2的字符串数组
                    if (arrSplitEqual.length > 2) {
                        for (int i = 2;i < arrSplitEqual.length;i++) {
                            arrSplitEqual[1] += ("=" + arrSplitEqual[i]);
                        }
                    }
                    requestParams.put(arrSplitEqual[0], arrSplitEqual[1]);
                } else if (!arrSplitEqual[0].equals("")) {
                    //只有参数,没有参数值,赋默认值""
                    requestParams.put(arrSplitEqual[0], "");
                    }
                }
            return requestParams;
        }
    
        /**
         * 去掉url中的路径,留下请求参数部分
         * URL类详解:http://www.runoob.com/java/java-url-processing.html
         * @param strURL url地址
         * @return url请求参数部分
         */
        private static String getUrlParams(String strURL) {
            URL url = null;
            try {
                url = new URL(strURL);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            String strAllParam = url.getQuery(); //请求的参数
            return strAllParam;
        }
    
        /**
         *返回url全路径
         */
        public static String getCompleteUrl() {
            HttpServletRequest request = RequestUtils.getRequest();
            String completeUrl  = request.getScheme()+"://" + request.getServerName() //服务器地址
                    + ":"
                    + request.getServerPort()           //端口号
                    + request.getContextPath()      //项目名称
                    + request.getServletPath()      //请求页面或其他地址
                    + "?" + (request.getQueryString()); //参数
            return completeUrl;
        }
    
        public static String getContextPath() {
            HttpServletRequest request = RequestUtils.getRequest();
            String contextPath  = request.getScheme()+"://" + request.getServerName() //服务器地址(域名)
                    + ":"
                    + request.getServerPort()           //端口号
                    + request.getContextPath();     //项目名称
            return contextPath;
        }
    
        /**
         *获取客户端的ip
         */
        public static String getIpAddress(HttpServletRequest request) {
            String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            if (ip.contains(",")) {
                return ip.split(",")[0];
            } else {
                return ip;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:对url的页面路径和参数的解析

          本文链接:https://www.haomeiwen.com/subject/quohlhtx.html