kongdeqiang
2022-12-16 daf6a95086087ec99232eea8b4648b7541881f7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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;
    }
 
}