多级反向代理获取真实客户端IP

package com.datatang.utils;


import java.text.DecimalFormat;


import javax.servlet.http.HttpServletRequest;


import org.junit.Test;


public class Utils {


/**
* 将字符串转换为int类型

* @param expression
*            要转换的字符串
* @param defValue
*            缺省值
* @return 转换后的int类型结果
*/
public static int strToInt(String expression, int defValue) {
if (StringUtil.isNullOrEmpty(expression)) {
return defValue;
}
return isNumber(expression) ? Integer.parseInt(expression) : defValue;
}


/**
* 正则表达式数字验证

* @param str
* @return
*/
public static boolean isNumber(String str) {
java.util.regex.Pattern pattern = java.util.regex.Pattern
.compile("[0-9]*");
java.util.regex.Matcher match = pattern.matcher(str);
if (match.matches() == false) {
return false;
} else {
return true;
}
}


/**
* 多级反向代理获取真实客户端IP

* @param request
* @return
*/
public static String getRealIP(HttpServletRequest request) {
// 运用多级反向代理,确保获取客户端的真实IP
String realIP = request.getHeader("x-forwarded-for");


if (realIP == null || realIP.length() == 0
|| "unknown".equalsIgnoreCase(realIP)) {
realIP = request.getHeader("Proxy-Client-IP");
}
if (realIP == null || realIP.length() == 0
|| "unknown".equalsIgnoreCase(realIP)) {
realIP = request.getHeader("WL-Proxy-Client-IP");
}
if (realIP == null || realIP.length() == 0
|| "unknown".equalsIgnoreCase(realIP)) {
// 此处取得真实IP
realIP = request.getRemoteAddr();
}


return realIP;


}





    
  
    /**
     * 随机串
     * @param codeLen
     * @return
     */
    public static String getRandomCode() {
    return getRandomCode(10);
    }
    
    /**
     * 随机串
     * @param codeLen 随机字符串的长度
     * @return
     */
    public static String getRandomCode(int codeLen)
    {
        //自定义随机码字符串序列(使用逗号分隔)
        String codeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";


        String[] arr = codeSerial.split(",");


        String code = "";


int randValue = -1;


        for (int i = 0; i < codeLen; i++)
        {
        randValue = (int) (Math.random() * arr.length);
            code += arr[randValue];
        }


        return code;
    }
    
    @Test
    public void testGetRandomCode() {
   
    System.out.println(getRandomCode(2));
    System.out.println(getRandomCode());
    }
    
    /**
     * 去除小数点后面的数,并且四舍五入
     * @param number
     * @return
     */
    public static int formatFloat(float number){
    try {
DecimalFormat df = new DecimalFormat();
df.applyPattern("0");
String formatPaySum = df.format(number);
return Integer.parseInt(formatPaySum);
} catch (NumberFormatException e) {
return 0;
}
    }
    
    /**
     * 保留小数点后两位
     * @param money
     * @return
     */
    public static String retainPoint(float money){
    try {
DecimalFormat df = new DecimalFormat("0.00");
return df.format(money);
} catch (Exception e) {
return null;
}
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值