package com.wgcloud.util;
|
|
|
import com.googlecode.aviator.AviatorEvaluator;
|
import com.googlecode.aviator.Expression;
|
import com.wgcloud.util.staticvar.StaticKeys;
|
import org.apache.commons.lang3.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
|
/**
|
* @version v3.3
|
* @ClassName:FormatUtil.java
|
* @author: http://www.wgstart.com
|
* @date: 2021年1月16日
|
* @Description: FormatUtil.java
|
* @Copyright: 2019-2021 wgcloud. All rights reserved.
|
*/
|
public class FormatUtil {
|
|
private static Logger logger = LoggerFactory.getLogger(FormatUtil.class);
|
|
|
|
/**
|
* 秒转为天小时分钟
|
*
|
* @param l
|
* @return
|
*/
|
public static String timesToDay(Long l) {
|
if (l == null) {
|
return "";
|
}
|
StringBuffer sb = new StringBuffer();
|
long seconds = 1;
|
long minutes = 60 * seconds;
|
long hours = 60 * minutes;
|
long days = 24 * hours;
|
if (l / days >= 1) {
|
sb.append((int) (l / days) + "天");
|
}
|
if (l % days / hours >= 1) {
|
sb.append((int) (l % days / hours) + "小时");
|
}
|
if (l % days % hours / minutes >= 1) {
|
sb.append((int) (l % days % hours / minutes) + "分钟");
|
}
|
if (l % days % hours % minutes / seconds >= 1) {
|
sb.append((int) (l % days % hours % minutes / seconds) + "秒");
|
}
|
return sb.toString();
|
}
|
|
/**
|
* 秒转为天
|
*
|
* @param l
|
* @return
|
*/
|
public static String secondsToDays(Long l) {
|
if (l == null) {
|
return "";
|
}
|
StringBuffer sb = new StringBuffer();
|
long seconds = 1;
|
long minutes = 60 * seconds;
|
long hours = 60 * minutes;
|
long days = 24 * hours;
|
if (l / days >= 1) {
|
sb.append((int) (l / days) + "天");
|
}
|
return sb.toString();
|
}
|
|
|
/**
|
* 截取字符串,超过指定长度时候截取,不超过时候返回原值
|
*
|
* @param str
|
* @param len
|
* @return
|
*/
|
public static String getString(String str, int len) {
|
if (StringUtils.isEmpty(str)) {
|
return "";
|
}
|
if (str.length() <= len) {
|
return str;
|
}
|
return str.substring(0, len);
|
}
|
|
/**
|
* kb转为m
|
*
|
* @param str
|
* @return
|
*/
|
public static String kbToM(String str) {
|
if (StringUtils.isEmpty(str)) {
|
return "0KB";
|
}
|
double result = 0;
|
double mod = 1024d;
|
try {
|
double strDouble = Double.valueOf(str);
|
if (strDouble > 1024) {
|
result = strDouble / mod;
|
return formatDouble(result, 2) + "MB";
|
}
|
} catch (Exception e) {
|
logger.error("kb转为M错误:", e);
|
return str + "KB";
|
}
|
return str + "KB";
|
}
|
|
/**
|
* m转为g
|
*
|
* @param str
|
* @return
|
*/
|
public static String mToG(String str) {
|
if (StringUtils.isEmpty(str)) {
|
return "0M";
|
}
|
double result = 0;
|
double mod = 1024d;
|
try {
|
double strDouble = Double.valueOf(str);
|
if (strDouble > 1024) {
|
result = strDouble / mod;
|
return formatDouble(result, 2) + "G";
|
}
|
} catch (Exception e) {
|
logger.error("m转为g错误:", e);
|
return str + "M";
|
}
|
return str + "M";
|
}
|
|
/**
|
* G转为T
|
*
|
* @param str
|
* @return
|
*/
|
public static String gToT(String str) {
|
if (StringUtils.isEmpty(str)) {
|
return "0G";
|
}
|
double result = 0;
|
double mod = 1024d;
|
try {
|
double strDouble = Double.valueOf(str);
|
if (strDouble > 1024) {
|
result = strDouble / mod;
|
return formatDouble(result, 1) + "T";
|
}
|
} catch (Exception e) {
|
logger.error("G转为T错误:", e);
|
return str + "G";
|
}
|
return formatDouble(Double.valueOf(str), 1) + "G";
|
}
|
|
/**
|
* bytes转为"B", "KB", "MB"", "G", "T", "P", "E", "Z", "Y"
|
* 如果不是byte,那就是KB,那就转为byte
|
*
|
* @param str
|
* @return
|
*/
|
public static String bytesFormatUnit(String str, String snmpUnit) {
|
if (StringUtils.isEmpty(str) || "0".equals(str)) {
|
return "0B";
|
}
|
try {
|
long bytes = Long.valueOf(str);
|
if (!StaticKeys.BYTES_STR.equals(snmpUnit)) {
|
bytes = bytes * 1024;
|
}
|
int k = 1024;
|
String[] sizes = new String[]{"B", "KB", "M", "G", "T", "P", "E", "Z", "Y"};
|
int i = (int) Math.floor(Math.log(bytes) / Math.log(k));
|
if (i > sizes.length - 1) {
|
i = sizes.length - 1;
|
}
|
|
return formatDouble(bytes / Math.pow(k, i), 2) + sizes[i];
|
} catch (Exception e) {
|
logger.error("bytesFormatUnit错误", e);
|
}
|
return str + "B";
|
}
|
|
/**
|
* byte转为m
|
* 如果不是byte,那就是KB,那就转为byte
|
*
|
* @param str
|
* @return
|
*/
|
public static String byteToM(String str, String snmpUnit) {
|
if (StringUtils.isEmpty(str)) {
|
return "0B";
|
}
|
double result = 0;
|
double mod = 1024d;
|
try {
|
double strDouble = Double.valueOf(str);
|
if (!StaticKeys.BYTES_STR.equals(snmpUnit)) {
|
strDouble = strDouble * 1024;
|
}
|
result = strDouble / mod / mod;
|
return formatDouble(result, 2) + "MB";
|
} catch (Exception e) {
|
logger.error("kb转为M错误:", e);
|
return str + "KB";
|
}
|
}
|
|
/**
|
* 格式化double数据,截取小数点后数字
|
*
|
* @param str
|
* @param num
|
* @return
|
*/
|
public static double formatDouble(Double str, int num) {
|
if (str == null) {
|
return 0;
|
}
|
java.math.BigDecimal b = new java.math.BigDecimal(str);
|
double myNum3 = b.setScale(num, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
|
return myNum3;
|
}
|
|
|
/**
|
* 查询sql中是否有敏感字符,有返回敏感字符串,否则返回空
|
*
|
* @param sql
|
* @return
|
*/
|
public static String haveSqlDanger(String sql, String sqlInKeys) {
|
if (StringUtils.isEmpty(sql)) {
|
return "";
|
}
|
sql = sql.toLowerCase();
|
sqlInKeys = sqlInKeys.toLowerCase();
|
String[] sqlinkeys = sqlInKeys.split(",");
|
for (String sqlinkey : sqlinkeys) {
|
if (sql.indexOf(sqlinkey) > -1) {
|
return sqlinkey;
|
}
|
}
|
return "";
|
}
|
|
/**
|
* 查询下发指令内容中是否有block敏感字符,有返回敏感字符串,否则返回空
|
*
|
* @param shell 下发指令内容
|
* @param linuxBlock linux屏蔽字符
|
* @param winBlock windows屏蔽字符
|
* @return
|
*/
|
public static String haveBlockDanger(String shell, String linuxBlock, String winBlock) {
|
if (StringUtils.isEmpty(shell)) {
|
return "";
|
}
|
if (StringUtils.isEmpty(linuxBlock) && StringUtils.isEmpty(winBlock)) {
|
return "";
|
}
|
shell = shell.toLowerCase();
|
String[] blocks = linuxBlock.split(",");
|
for (String blockStr : blocks) {
|
if (shell.indexOf(blockStr) > -1) {
|
return blockStr;
|
}
|
}
|
blocks = winBlock.split(",");
|
for (String blockStr : blocks) {
|
if (shell.indexOf(blockStr) > -1) {
|
return blockStr;
|
}
|
}
|
return "";
|
}
|
|
/**
|
* 表达式校验,成立返回true,否则返回false
|
* @param expression
|
* @param value
|
* @return
|
*/
|
public static boolean validateExpression(String expression, Object value) {
|
if(StringUtils.isEmpty(expression) || null == value){
|
return false;
|
}
|
// 编译表达式
|
Expression compiledExp = AviatorEvaluator.compile(expression);
|
Map<String, Object> env = new HashMap<String, Object>();
|
env.put("result", value);
|
// 执行表达式
|
Boolean result = (Boolean) compiledExp.execute(env);
|
return result;
|
}
|
|
}
|