
package com.fengjx.ttwx.common.utils;

import com.fengjx.ttwx.modules.common.constants.AppConfig;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

/**
 * web相关工具类 Created by fengjx on 2014/11/14 0014.
 */
public final class WebUtil {

    /**
     * 获取所有request请求参数key-value
     * 
     * @param request
     * @return
     */
    public static Map<String, String> getRequestParams(HttpServletRequest request) {

        Map<String, String> params = new HashMap<String, String>();
        if (null != request) {
            Set<String> paramsKey = request.getParameterMap().keySet();
            for (String key : paramsKey) {
                params.put(key, request.getParameter(key));
            }
        }
        return params;
    }

    /**
     * 获取所有request请求参数key-value，过滤掉空值
     *
     * @param request
     * @return
     */
    public static Map<String, String> getNotBlankRequestParams(HttpServletRequest request) {
        Map<String, String> params = new HashMap<String, String>();
        if (null != request) {
            Set<String> paramsKey = request.getParameterMap().keySet();
            for (String key : paramsKey) {
                if(StringUtils.isNotBlank(request.getParameter(key))){
                    params.put(key, request.getParameter(key));
                }
            }
        }
        return params;
    }

    /**
     * 获得客户端IP
     * 
     * @param request
     * @return
     */
    public static String getRealIp(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();
        }
        return ip;
    }

    public static String getRealIpV2(HttpServletRequest request) {
        String accessIP = request.getHeader("x-forwarded-for");
        if (null == accessIP)
            return request.getRemoteAddr();
        return accessIP;
    }

    public static String urlEncode(String source) throws UnsupportedEncodingException {
        String res = source;
        res = URLEncoder.encode(source, "UTF-8");
        return res;
    }

    /**
     * 判断是否是ajax请求
     * 
     * @param request
     * @return
     */
    public static boolean validAjax(HttpServletRequest request) {
        String flag = request.getHeader("Request-Flag");
        if (AppConfig.REQUEST_FLAG_AJAX.equals(flag)) {
            return true;
        }
        String requestType = request.getHeader("X-Requested-With");
        if ("XMLHttpRequest".equals(requestType)) {
            return true;
        }
        return false;
    }

}

@@ -92,16 +92,17 @@
 Config;%0A
+%0A
 import o
@@ -1348,16 +1348,17 @@
       if
+ 
 (StringU
@@ -1400,16 +1400,17 @@
 r(key)))
+ 
 %7B%0A      
@@ -2479,44 +2479,8 @@
 rce)
- throws UnsupportedEncodingException
  %7B%0A 
@@ -2507,16 +2507,34 @@
 source;%0A
+        try %7B%0A    
         
@@ -2571,24 +2571,128 @@
 , %22UTF-8%22);%0A
+        %7D catch (UnsupportedEncodingException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A
         retu

修改urlEncode方法，不抛异常