oConvertUtils.java 16 KB

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