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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
public class UserFun { static final String[] unitCN = new String[]{"京","","","","万兆","","","","兆","","","","万亿","","","","亿","","","","万","","","",""}; public String ggg(Method method){ return method.getClass().getName() + "." + method.getName(); }
public static Cookie newCookie(String name, String value){ Cookie randomCookie = new Cookie(); randomCookie.setPath("/"); return randomCookie; } public static int compareDatesDay(Date date1,Date date2){ int days = 0; try { days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24)); } catch (Exception e) { e.printStackTrace(); } return days; }
public static String getRandomString(int len){ char[] chars = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h', 'i','j','k','l','m','n','o','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','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; Random random = new Random(); StringBuffer buffer = new StringBuffer(); for(int i=0;i<len;i++){ buffer.append(chars[random.nextInt(chars.length)]); } return buffer.toString(); }
public static String getIpAddr(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 formartNumByCnUnit(double num, int digit){ if(digit<0){ digit = 0; } String numberStr = (new BigDecimal(String.valueOf(num))).toPlainString(); String ends = ""; char[] splits = null; if(numberStr.indexOf(".")>-1){ splits = (numberStr.substring(0,numberStr.indexOf("."))).toCharArray(); char[] ends0 = (numberStr.substring(numberStr.indexOf(".")+1, numberStr.length())).toCharArray(); for(int i=ends0.length-1;i>=0;i--){ if(ends0[i] != '0'){ for(int j=0;j<=i;j++){ ends += ends0[j]; } break; } } }else{ splits = numberStr.toCharArray(); } StringBuffer retStr = new StringBuffer(); String unit = ""; for(int i=0;i<splits.length;i++){ if(splits.length>4 && (unitCN.length-splits.length+i)%4==0 && unit.length()==0){ unit = unitCN[unitCN.length-splits.length+i]; retStr.append(splits[i]); String others = ""; for(int j=i+1;j<splits.length && j-i<=digit+1;j++){ others += splits[j]; } if(others.length()>0){ int oth = Integer.parseInt(others); if(oth>0){ if(others.length()>digit){ double tmps = (Math.round(oth*Math.pow(10,-1)))*Math.pow(10,-digit); others = ((tmps + "").split("\\.")).length>1?((tmps + "").split("\\."))[1]:""; others = others.length()>=digit ? others.substring(0, digit) : others; if(tmps >= 1){ int tmpInt = Integer.parseInt(retStr.toString()) + 1; if(tmpInt==10000){ retStr = new StringBuffer("1"); unit = unitCN[unitCN.length-splits.length+i-4]; }else{ retStr = new StringBuffer(tmpInt+""); } } } while(others.endsWith("0")){ others = others.substring(0, others.length()-1); } others = others.length() == 0 ? "" : "." + others; }else{ others = ""; } } retStr.append(others + unit); break; }else{ retStr.append(splits[i]); } } if(unit.length()==0 && ends.length()>0){ retStr.append("." + ends); } return retStr.toString(); }
public static List<String> listrem(List<String> listA, List<String> listB){ for (Iterator<String> itA = listA.iterator(); itA.hasNext();){ String temp = itA.next(); for (int i = 0; i < listB.size(); i++){ if (temp.equals(listB.get(i))){ itA.remove(); break; } } } return listA; } public static int getRandomInt(int min,int max){ return (int)(min+Math.random()*(max-min+1)); } public static String getUUID32(){ String uuid = UUID.randomUUID().toString(); uuid = uuid.replace("-", ""); return uuid; } public static String getStringNoNullTrim(String str) { return str != null && !"null".equals(str.trim()) ? str.trim() : ""; }
public static String getStackTraceForHtml(Throwable e, int linenum) { String returnStr = ""; int lineCount = 0; int moreInt = 0; int causeLength = 0; StringBuffer buf = new StringBuffer(); StackTraceElement[] elements = e.getStackTrace(); Throwable causeThrowable = e.getCause(); StackTraceElement[] elementsCause = null; if (causeThrowable != null) { elementsCause = causeThrowable.getStackTrace(); causeLength = elementsCause.length; } if (lineCount >= linenum && linenum != -1) { if (moreInt == 0) { moreInt = elements.length + causeLength; buf.append("<br> ... " + moreInt + " more"); } } else { buf.append(e.getClass().getName() + ":" + getStringNoNullTrim(e.getMessage())); ++lineCount; } int i; String opsition; for(i = 0; i < elements.length; ++i) { if (lineCount >= linenum && linenum != -1) { if (moreInt == 0) { moreInt = elements.length - i + causeLength; buf.append("<br> ... " + moreInt + " more"); } } else { opsition = "Unknown Source"; if (elements[i].getFileName() != null) { opsition = elements[i].getFileName() + ":" + elements[i].getLineNumber(); } buf.append("<br> at " + elements[i].getClassName() + "." + elements[i].getMethodName() + "(" + opsition + ")"); ++lineCount; } } if (causeThrowable != null) { if (lineCount >= linenum && linenum != -1) { if (moreInt == 0) { moreInt = causeLength; buf.append("<br> ... " + causeLength + " more"); } } else { buf.append("<br>Caused by:" + causeThrowable.getClass().getName() + ":" + getStringNoNullTrim(causeThrowable.getMessage()) + "<br>"); lineCount += 2; } } if (causeThrowable != null) { for(i = 0; i < causeLength; ++i) { if (lineCount >= linenum && linenum != -1) { if (moreInt == 0) { moreInt = causeLength - i; buf.append("<br> ... " + moreInt + " more"); } } else { opsition = "Unknown Source"; if (elementsCause[i].getFileName() != null) { opsition = elementsCause[i].getFileName() + ":" + elementsCause[i].getLineNumber(); } buf.append("<br> at " + elementsCause[i].getClassName() + "." + elementsCause[i].getMethodName() + "(" + opsition + ")"); ++lineCount; } } } returnStr = buf.toString(); returnStr = returnStr.trim(); return returnStr; }
public static Object doGetMethod(Object ob , String name)throws Exception{ Method[] m = ob.getClass().getMethods(); for(int i = 0;i < m.length;i++){ if(("get"+name).toLowerCase().equals(m[i].getName().toLowerCase())){ return m[i].invoke(ob); } } return null; }
public static boolean checkExistLoadBalancer(){ try { Class.forName("com.detech.dems.common.loadbalance.core.CustomSpringCloudLoadBalancer"); return true; } catch (ClassNotFoundException e) { } return false; }
public static boolean checkExistGatewayApplication(){ try { Class.forName("com.detech.dems.gateway.GatewayApplication"); return true; } catch (ClassNotFoundException e) { } return false; }
public static final boolean strToBoolean(String flag) { if("0".equals(flag)){ return false; } if("1".equals(flag)){ return true; } if("Y".equals(flag)){ return true; } if("N".equals(flag)){ return false; } return StringUtils.isEmpty(flag); }
public static String getLocalIp() throws UnknownHostException { String host = "127.0.0.1"; try { String address = InetAddress.getLocalHost().getHostAddress(); if (address != null) { host = address; } } catch (UnknownHostException e) { e.printStackTrace(); } return host; } public static String getServerIp() { String ip = "127.0.0.1"; try{ Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface ni = interfaces.nextElement(); String name = ni.getName(); if (name.toLowerCase().startsWith("br-") || name.toLowerCase().startsWith("docker") || name.toLowerCase().startsWith("veth")) { continue; } Enumeration<InetAddress> addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) { return addr.getHostAddress(); } } } }catch (SocketException e){ e.printStackTrace(); } return ip; } }
|