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;
}
}
}
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;
}
}
}