oConvertUtils.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. package org.jeecg.common.util;
  2. import org.apache.commons.lang.StringEscapeUtils;
  3. import javax.servlet.http.HttpServletRequest;
  4. import java.lang.reflect.Field;
  5. import java.math.BigDecimal;
  6. import java.math.BigInteger;
  7. import java.net.InetAddress;
  8. import java.net.NetworkInterface;
  9. import java.net.SocketException;
  10. import java.net.UnknownHostException;
  11. import java.sql.Date;
  12. import java.util.*;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. /**
  16. *
  17. * @Author 张代浩
  18. *
  19. */
  20. public class oConvertUtils {
  21. public static boolean isEmpty(Object object) {
  22. if (object == null) {
  23. return (true);
  24. }
  25. if ("".equals(object)) {
  26. return (true);
  27. }
  28. if ("null".equals(object)) {
  29. return (true);
  30. }
  31. return (false);
  32. }
  33. public static boolean isNotEmpty(Object object) {
  34. if (object != null && !"".equals(object) && !"null".equals(object)) {
  35. return (true);
  36. }
  37. return (false);
  38. }
  39. public static String decode(String strIn, String sourceCode, String targetCode) {
  40. String temp = code2code(strIn, sourceCode, targetCode);
  41. return temp;
  42. }
  43. private static String code2code(String strIn, String sourceCode, String targetCode) {
  44. if (strIn == null || "".equals(strIn.trim())) {
  45. return strIn;
  46. }
  47. String strOut = null;
  48. try {
  49. byte[] b = strIn.getBytes(sourceCode);
  50. strOut = new String(b, targetCode);
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. return null;
  54. }
  55. return strOut;
  56. }
  57. public static int getInt(String s, int defval) {
  58. if (s == null || s.trim().equals("")) {
  59. return (defval);
  60. }
  61. try {
  62. return (Integer.parseInt(s));
  63. } catch (NumberFormatException e) {
  64. return (defval);
  65. }
  66. }
  67. public static int getInt(String s) {
  68. if (s == null || s.trim().equals("")) {
  69. return 0;
  70. }
  71. try {
  72. return (Integer.parseInt(s));
  73. } catch (NumberFormatException e) {
  74. return 0;
  75. }
  76. }
  77. public static int getInt(String s, Integer df) {
  78. if (s == null || s.trim().equals("")) {
  79. return df;
  80. }
  81. try {
  82. return (Integer.parseInt(s));
  83. } catch (NumberFormatException e) {
  84. return 0;
  85. }
  86. }
  87. public static Integer[] getInts(String[] s) {
  88. if (s == null) {
  89. return null;
  90. }
  91. Integer[] integer = new Integer[s.length];
  92. for (int i = 0; i < s.length; i++) {
  93. integer[i] = Integer.parseInt(s[i]);
  94. }
  95. return integer;
  96. }
  97. public static double getDouble(String s, double defval) {
  98. if (s == null || s.trim().equals("")) {
  99. return (defval);
  100. }
  101. try {
  102. return (Double.parseDouble(s));
  103. } catch (NumberFormatException e) {
  104. return (defval);
  105. }
  106. }
  107. public static double getDou(Double s, double defval) {
  108. if (s == null) {
  109. return (defval);
  110. }
  111. return s;
  112. }
  113. public static int getInt(Object object, int defval) {
  114. if (isEmpty(object)) {
  115. return (defval);
  116. }
  117. try {
  118. return (Integer.parseInt(object.toString()));
  119. } catch (NumberFormatException e) {
  120. return (defval);
  121. }
  122. }
  123. public static Integer getInt(Object object) {
  124. if (isEmpty(object)) {
  125. return null;
  126. }
  127. try {
  128. return (Integer.parseInt(object.toString()));
  129. } catch (NumberFormatException e) {
  130. return null;
  131. }
  132. }
  133. public static int getInt(BigDecimal s, int defval) {
  134. if (s == null) {
  135. return (defval);
  136. }
  137. return s.intValue();
  138. }
  139. public static Integer[] getIntegerArray(String[] object) {
  140. int len = object.length;
  141. Integer[] result = new Integer[len];
  142. try {
  143. for (int i = 0; i < len; i++) {
  144. result[i] = Integer.getInteger(object[i].trim());
  145. }
  146. return result;
  147. } catch (NumberFormatException e) {
  148. return null;
  149. }
  150. }
  151. public static String getString(String s) {
  152. return (getString(s, ""));
  153. }
  154. /**
  155. * 转义成Unicode编码
  156. * @param s
  157. * @return
  158. */
  159. public static String escapeJava(Object s) {
  160. return StringEscapeUtils.escapeJava(getString(s));
  161. }
  162. public static String getString(Object object) {
  163. if (isEmpty(object)) {
  164. return "";
  165. }
  166. return (object.toString().trim());
  167. }
  168. public static String getString(int i) {
  169. return (String.valueOf(i));
  170. }
  171. public static String getString(float i) {
  172. return (String.valueOf(i));
  173. }
  174. public static String getString(String s, String defval) {
  175. if (isEmpty(s)) {
  176. return (defval);
  177. }
  178. return (s.trim());
  179. }
  180. public static String getString(Object s, String defval) {
  181. if (isEmpty(s)) {
  182. return (defval);
  183. }
  184. return (s.toString().trim());
  185. }
  186. public static long stringToLong(String str) {
  187. return Long.valueOf(str);
  188. }
  189. /**
  190. * 获取本机IP
  191. */
  192. public static String getIp() {
  193. String ip = null;
  194. try {
  195. InetAddress address = InetAddress.getLocalHost();
  196. ip = address.getHostAddress();
  197. } catch (UnknownHostException e) {
  198. e.printStackTrace();
  199. }
  200. return ip;
  201. }
  202. /**
  203. * 判断一个类是否为基本数据类型。
  204. *
  205. * @param clazz
  206. * 要判断的类。
  207. * @return true 表示为基本数据类型。
  208. */
  209. private static boolean isBaseDataType(Class clazz) {
  210. return (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(BigDecimal.class) || clazz.equals(BigInteger.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class) || clazz.isPrimitive());
  211. }
  212. /**
  213. * @param request
  214. * IP
  215. * @return IP Address
  216. */
  217. public static String getIpAddrByRequest(HttpServletRequest request) {
  218. String ip = request.getHeader("x-forwarded-for");
  219. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  220. ip = request.getHeader("Proxy-Client-IP");
  221. }
  222. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  223. ip = request.getHeader("WL-Proxy-Client-IP");
  224. }
  225. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  226. ip = request.getRemoteAddr();
  227. }
  228. return ip;
  229. }
  230. /**
  231. * @return 本机IP
  232. * @throws SocketException
  233. */
  234. public static String getRealIp() throws SocketException {
  235. // 本地IP,如果没有配置外网IP则返回它
  236. String localip = null;
  237. // 外网IP
  238. String netip = null;
  239. Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
  240. InetAddress ip = null;
  241. // 是否找到外网IP
  242. boolean finded = false;
  243. while (netInterfaces.hasMoreElements() && !finded) {
  244. NetworkInterface ni = netInterfaces.nextElement();
  245. Enumeration<InetAddress> address = ni.getInetAddresses();
  246. while (address.hasMoreElements()) {
  247. ip = address.nextElement();
  248. if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(':') == -1) {
  249. netip = ip.getHostAddress();
  250. finded = true;
  251. break;
  252. } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(':') == -1) {
  253. localip = ip.getHostAddress();
  254. }
  255. }
  256. }
  257. if (netip != null && !"".equals(netip)) {
  258. return netip;
  259. } else {
  260. return localip;
  261. }
  262. }
  263. /**
  264. * java去除字符串中的空格、回车、换行符、制表符
  265. *
  266. * @param str
  267. * @return
  268. */
  269. public static String replaceBlank(String str) {
  270. String dest = "";
  271. if (str != null) {
  272. Pattern p = Pattern.compile("\\s*|\t|\r|\n");
  273. Matcher m = p.matcher(str);
  274. dest = m.replaceAll("");
  275. }
  276. return dest;
  277. }
  278. /**
  279. * 判断元素是否在数组内
  280. *
  281. * @param substring
  282. * @param source
  283. * @return
  284. */
  285. public static boolean isIn(String substring, String[] source) {
  286. if (source == null || source.length == 0) {
  287. return false;
  288. }
  289. for (int i = 0; i < source.length; i++) {
  290. String aSource = source[i];
  291. if (aSource.equals(substring)) {
  292. return true;
  293. }
  294. }
  295. return false;
  296. }
  297. /**
  298. * SET转换MAP
  299. *
  300. * @param setobj
  301. * @return
  302. */
  303. public static Map<Object, Object> setToMap(Set<Object> setobj) {
  304. Map<Object, Object> map = new HashMap<>();
  305. for (Iterator iterator = setobj.iterator(); iterator.hasNext();) {
  306. Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) iterator.next();
  307. map.put(entry.getKey().toString(), entry.getValue() == null ? "" : entry.getValue().toString().trim());
  308. }
  309. return map;
  310. }
  311. public static boolean isInnerIp(String ipAddress) {
  312. boolean isInnerIp = false;
  313. long ipNum = getIpNum(ipAddress);
  314. /**
  315. * 私有IP:A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址
  316. **/
  317. long aBegin = getIpNum("10.0.0.0");
  318. long aEnd = getIpNum("10.255.255.255");
  319. long bBegin = getIpNum("172.16.0.0");
  320. long bEnd = getIpNum("172.31.255.255");
  321. long cBegin = getIpNum("192.168.0.0");
  322. long cEnd = getIpNum("192.168.255.255");
  323. isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || "127.0.0.1".equals(ipAddress);
  324. return isInnerIp;
  325. }
  326. private static long getIpNum(String ipAddress) {
  327. String[] ip = ipAddress.split("\\.");
  328. long a = Integer.parseInt(ip[0]);
  329. long b = Integer.parseInt(ip[1]);
  330. long c = Integer.parseInt(ip[2]);
  331. long d = Integer.parseInt(ip[3]);
  332. return a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
  333. }
  334. private static boolean isInner(long userIp, long begin, long end) {
  335. return (userIp >= begin) && (userIp <= end);
  336. }
  337. /**
  338. * 将下划线大写方式命名的字符串转换为驼峰式。
  339. * 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
  340. * 例如:hello_world->helloWorld
  341. *
  342. * @param name
  343. * 转换前的下划线大写方式命名的字符串
  344. * @return 转换后的驼峰式命名的字符串
  345. */
  346. public static String camelName(String name) {
  347. StringBuilder result = new StringBuilder();
  348. // 快速检查
  349. if (name == null || name.isEmpty()) {
  350. // 没必要转换
  351. return "";
  352. } else if (!name.contains("_")) {
  353. // 不含下划线,仅将首字母小写
  354. //update-begin--Author:zhoujf Date:20180503 for:TASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
  355. //update-begin--Author:zhoujf Date:20180503 for:TASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
  356. return name.substring(0, 1).toLowerCase() + name.substring(1).toLowerCase();
  357. //update-end--Author:zhoujf Date:20180503 for:TASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
  358. }
  359. // 用下划线将原始字符串分割
  360. String[] camels = name.split("_");
  361. for (String camel : camels) {
  362. // 跳过原始字符串中开头、结尾的下换线或双重下划线
  363. if (camel.isEmpty()) {
  364. continue;
  365. }
  366. // 处理真正的驼峰片段
  367. if (result.length() == 0) {
  368. // 第一个驼峰片段,全部字母都小写
  369. result.append(camel.toLowerCase());
  370. } else {
  371. // 其他的驼峰片段,首字母大写
  372. result.append(camel.substring(0, 1).toUpperCase());
  373. result.append(camel.substring(1).toLowerCase());
  374. }
  375. }
  376. return result.toString();
  377. }
  378. /**
  379. * 将下划线大写方式命名的字符串转换为驼峰式。
  380. * 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
  381. * 例如:hello_world,test_id->helloWorld,testId
  382. *
  383. * @param names
  384. * 转换前的下划线大写方式命名的字符串
  385. * @return 转换后的驼峰式命名的字符串
  386. */
  387. public static String camelNames(String names) {
  388. if (names == null || "".equals(names)) {
  389. return null;
  390. }
  391. StringBuffer sf = new StringBuffer();
  392. String[] fs = names.split(",");
  393. for (String field : fs) {
  394. field = camelName(field);
  395. sf.append(field + ",");
  396. }
  397. String result = sf.toString();
  398. return result.substring(0, result.length() - 1);
  399. }
  400. //update-begin--Author:zhoujf Date:20180503 for:TASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
  401. /**
  402. * 将下划线大写方式命名的字符串转换为驼峰式。(首字母写)
  403. * 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
  404. * 例如:hello_world->HelloWorld
  405. *
  406. * @param name
  407. * 转换前的下划线大写方式命名的字符串
  408. * @return 转换后的驼峰式命名的字符串
  409. */
  410. public static String camelNameCapFirst(String name) {
  411. StringBuilder result = new StringBuilder();
  412. // 快速检查
  413. if (name == null || name.isEmpty()) {
  414. // 没必要转换
  415. return "";
  416. } else if (!name.contains("_")) {
  417. // 不含下划线,仅将首字母小写
  418. return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
  419. }
  420. // 用下划线将原始字符串分割
  421. String[] camels = name.split("_");
  422. for (String camel : camels) {
  423. // 跳过原始字符串中开头、结尾的下换线或双重下划线
  424. if (camel.isEmpty()) {
  425. continue;
  426. }
  427. // 其他的驼峰片段,首字母大写
  428. result.append(camel.substring(0, 1).toUpperCase());
  429. result.append(camel.substring(1).toLowerCase());
  430. }
  431. return result.toString();
  432. }
  433. //update-end--Author:zhoujf Date:20180503 for:TASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
  434. /**
  435. * 将驼峰命名转化成下划线
  436. * @param para
  437. * @return
  438. */
  439. public static String camelToUnderline(String para){
  440. if(para.length()<3){
  441. return para.toLowerCase();
  442. }
  443. StringBuilder sb=new StringBuilder(para);
  444. //定位
  445. int temp = 0;
  446. //从第三个字符开始 避免命名不规范
  447. for(int i=2;i<para.length();i++){
  448. if(Character.isUpperCase(para.charAt(i))){
  449. sb.insert(i+temp, "_");
  450. temp+=1;
  451. }
  452. }
  453. return sb.toString().toLowerCase();
  454. }
  455. /**
  456. * 随机数
  457. * @param place 定义随机数的位数
  458. */
  459. public static String randomGen(int place) {
  460. String base = "qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCRFVTGBYHNUJMIKLOP0123456789";
  461. StringBuffer sb = new StringBuffer();
  462. Random rd = new Random();
  463. for(int i=0;i<place;i++) {
  464. sb.append(base.charAt(rd.nextInt(base.length())));
  465. }
  466. return sb.toString();
  467. }
  468. /**
  469. * 获取类的所有属性,包括父类
  470. *
  471. * @param object
  472. * @return
  473. */
  474. public static Field[] getAllFields(Object object) {
  475. Class<?> clazz = object.getClass();
  476. List<Field> fieldList = new ArrayList<>();
  477. while (clazz != null) {
  478. fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
  479. clazz = clazz.getSuperclass();
  480. }
  481. Field[] fields = new Field[fieldList.size()];
  482. fieldList.toArray(fields);
  483. return fields;
  484. }
  485. /**
  486. * 将map的key全部转成小写
  487. * @param list
  488. * @return
  489. */
  490. public static List<Map<String, Object>> toLowerCasePageList(List<Map<String, Object>> list){
  491. List<Map<String, Object>> select = new ArrayList<>();
  492. for (Map<String, Object> row : list) {
  493. Map<String, Object> resultMap = new HashMap<>();
  494. Set<String> keySet = row.keySet();
  495. for (String key : keySet) {
  496. String newKey = key.toLowerCase();
  497. resultMap.put(newKey, row.get(key));
  498. }
  499. select.add(resultMap);
  500. }
  501. return select;
  502. }
  503. }