91dcadc64e8461a9d614fd28bce74047cb02c764.svn-base 17 KB

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