oConvertUtils.java 17 KB

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