oConvertUtils.java 16 KB

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