872f33e5dc176c0a87d307438f4b973d0269ce70.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. package com.yihu.utils;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Random;
  11. import java.util.UUID;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import net.sf.json.JSONObject;
  17. import org.dom4j.Document;
  18. import org.dom4j.DocumentException;
  19. import org.dom4j.DocumentHelper;
  20. import org.dom4j.io.OutputFormat;
  21. import org.dom4j.io.XMLWriter;
  22. import com.coreframework.util.AppConfig;
  23. public class StringUtil {
  24. /**
  25. * Code节点名称
  26. */
  27. private static final String CODE = "Code";
  28. /**
  29. * Message节点名称
  30. */
  31. private static final String MESSAGE = "Message";
  32. public static String createUUID() {
  33. return UUID.randomUUID().toString()
  34. .replaceAll("-", "").toUpperCase();
  35. }
  36. public static String formatXML(String retStr) throws Exception {
  37. String res = null;
  38. Document doc = null;
  39. try {
  40. doc = DocumentHelper.parseText(retStr);
  41. } catch (DocumentException e) {
  42. e.printStackTrace();
  43. throw e;
  44. }
  45. XMLWriter output = null;
  46. OutputFormat format = OutputFormat.createPrettyPrint();
  47. format.setEncoding("GBK");
  48. format.setNewLineAfterDeclaration(false);
  49. format.setExpandEmptyElements(true);
  50. format.setSuppressDeclaration(true);
  51. OutputStream outputStream = new ByteArrayOutputStream();
  52. XMLWriter writer = new XMLWriter(outputStream, format);
  53. writer.write(doc);
  54. writer.close();
  55. res = outputStream.toString();
  56. return res;
  57. }
  58. // String is null or not
  59. public static boolean isEmpty(String str) {
  60. if ("".equals(str) || "null".equals(str) || "NULL".equals(str)
  61. || str == null || "undefined".equals(str)){
  62. return true;
  63. }
  64. if ("".equals(str.trim())){
  65. return true;
  66. }
  67. return false;
  68. }
  69. // List is null or not
  70. public static boolean isEmpty(List list) {
  71. if (list != null && list.size() > 0){
  72. return false;
  73. }
  74. return true;
  75. }
  76. // Map is null or not
  77. public static boolean isEmpty(Map map) {
  78. if (map == null || map.size() == 0){
  79. return true;
  80. }
  81. return false;
  82. }
  83. // Object is null or not
  84. public static boolean isEmpty(Object obj) {
  85. if (obj == null){
  86. return true;
  87. }
  88. return false;
  89. }
  90. // to do empty string
  91. public static String chEmptyStr(String str) {
  92. if ("".equals(str) || "null".equals(str) || "NULL".equals(str)
  93. || str == null){
  94. return "";
  95. }
  96. return str.trim();
  97. }
  98. public static String chMap(Map map, String key) {
  99. String res = "";
  100. if (map != null) {
  101. return chEmptyStr(map.get(key) + "");
  102. }
  103. return res;
  104. }
  105. // chang clob type to String
  106. // public static String clobToStr(Clob clob) throws Exception {
  107. // return (clob != null ? clob.getSubString(1, (int) clob.length()) : "");
  108. // }
  109. public static int parseInt(Integer i) {
  110. if (i == null){
  111. return 0;
  112. }
  113. return i;
  114. }
  115. public static int parseInt(Integer i, int v) {
  116. if (i == null){
  117. return v;
  118. }
  119. return i;
  120. }
  121. public static boolean isNum(String str) {
  122. Pattern pattern = Pattern.compile("[0-9]*");
  123. Matcher isNum = pattern.matcher(str);
  124. if (isNum.matches()){
  125. return true;
  126. }
  127. return false;
  128. }
  129. public static boolean isEnNum(String str) {
  130. String first = str.substring(0, 1);
  131. String last = str.substring(str.length() - 1, str.length());
  132. String middle = str.substring(1, str.length() - 1);
  133. if (StringUtil.isNum(middle)) {
  134. if (StringUtil.isNum(first)
  135. || Pattern.compile("(?i)[a-zA-Z]").matcher(first).find()) {
  136. if (StringUtil.isNum(last)
  137. || Pattern.compile("(?i)[a-zA-Z]").matcher(last).find()) {
  138. if (!"a".equals(first) && !"A".equals(first)) {
  139. return true;
  140. }
  141. }
  142. }
  143. }
  144. return false;
  145. }
  146. public static boolean checkDate(String date) {
  147. Date d = null;
  148. boolean res = false;
  149. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  150. sdf.setLenient(false);
  151. try {
  152. d = sdf.parse(date);
  153. res = true;
  154. } catch (Exception e) {
  155. }
  156. return res;
  157. }
  158. public static Integer[] getBirthYMD(String identityID) {
  159. int y = 0, m = 0, d = 0;
  160. if (identityID.length() == 15) {
  161. y = Integer.parseInt("19" + identityID.substring(6, 8));
  162. m = Integer.parseInt(identityID.substring(8, 10));
  163. d = Integer.parseInt(identityID.substring(10, 12));
  164. }
  165. if (identityID.length() == 18) {
  166. y = Integer.parseInt(identityID.substring(6, 10));
  167. m = Integer.parseInt(identityID.substring(10, 12));
  168. d = Integer.parseInt(identityID.substring(12, 14));
  169. }
  170. Integer[] a = new Integer[3];
  171. a[0] = y;
  172. a[1] = m;
  173. a[2] = d;
  174. return a;
  175. }
  176. public static Integer[] getBirthYMDbyBirth(String birth) {
  177. int y = 0, m = 0, d = 0;
  178. if (!StringUtil.isEmpty(birth)) {
  179. y = Integer.parseInt(birth.substring(0, 4));
  180. m = Integer.parseInt(birth.substring(5, 7));
  181. d = Integer.parseInt(birth.substring(8, 10));
  182. }
  183. Integer[] a = new Integer[3];
  184. a[0] = y;
  185. a[1] = m;
  186. a[2] = d;
  187. return a;
  188. }
  189. public static boolean isNeedVirtual(String cardno) {
  190. if (StringUtil.isEmpty(cardno)){
  191. return true;
  192. }
  193. if (cardno.trim().length() != 8 && cardno.trim().length() != 12){
  194. return true;
  195. }
  196. return false;
  197. }
  198. public static String getWeek(int weekindex) {
  199. switch (weekindex) {
  200. case 0:
  201. return "星期天";
  202. case 1:
  203. return "星期一";
  204. case 2:
  205. return "星期二";
  206. case 3:
  207. return "星期三";
  208. case 4:
  209. return "星期四";
  210. case 5:
  211. return "星期五";
  212. case 6:
  213. return "星期六";
  214. default:
  215. return "";
  216. }
  217. }
  218. /**
  219. * 判断字符串是否存在
  220. *
  221. * @param str
  222. * @param arr
  223. * 英文逗号隔开的字符串数组
  224. * @return
  225. */
  226. public static boolean strInArr(String str, String[] arr) {
  227. boolean b = false;
  228. if (arr != null && arr.length > 0) {
  229. for (String s : arr) {
  230. if (str.equals(s)) {
  231. b = true;
  232. break;
  233. }
  234. }
  235. }
  236. return b;
  237. }
  238. /**
  239. * 去掉数组第一个值
  240. *
  241. * @param arr
  242. * @return
  243. */
  244. public static String deleteFirstNode(String[] arr) {
  245. if (arr.length < 2){
  246. return null;
  247. }
  248. String[] arrNew = new String[arr.length - 1];
  249. for (int i = 0; i < arr.length; i++) {
  250. if (i == 0) {
  251. continue;
  252. }
  253. arrNew[i - 1] = arr[i];
  254. }
  255. String ts = "";
  256. for (String s : arrNew) {
  257. ts += s + ",";
  258. }
  259. return ts.substring(0, ts.length() - 1);
  260. }
  261. public static String delHtmlTagaaa(String str1) {
  262. if(StringUtil.isEmpty(str1)) {
  263. return "";
  264. }
  265. String str = "";
  266. str = str1;
  267. str = str.replaceAll("</?[^>]+>", ""); // 剔出了<html>的标签
  268. str = str.replace("&nbsp;", "");
  269. //str = str.replace(".", "");
  270. //str = str.replace("\"", "‘");
  271. //str = str.replace("'", "‘");
  272. str=str.replaceAll("'", "\"");
  273. return str;
  274. }
  275. public static String editHtmlTag(String str) {
  276. if(StringUtil.isEmpty(str)) {
  277. return "";
  278. }
  279. return str.replaceAll("'", "\"");
  280. }
  281. public static String retrim(String str) {
  282. if(StringUtil.isEmpty(str)) {
  283. return "";
  284. }
  285. String temp=str.trim();
  286. temp=temp.replaceAll(" ", "");
  287. temp=temp.replaceAll(" ", "");
  288. return temp;
  289. }
  290. public static String getPermitway(String waystatus) {
  291. String a = waystatus.substring(0,1);
  292. String b = waystatus.substring(1,2);
  293. String c = waystatus.substring(2,3);
  294. List<String> list = new ArrayList<String>();
  295. if("1".equals(a)) {
  296. list.add("1");
  297. }
  298. if("1".equals(b)) {
  299. list.add("2");
  300. }
  301. if("1".equals(c)) {
  302. list.add("3");
  303. }
  304. String temp="";
  305. if(list!=null&&list.size()>0) {
  306. for(String str : list) {
  307. temp+=str+",";
  308. }
  309. temp = temp.substring(0,temp.length()-1);
  310. }
  311. return temp;
  312. }
  313. /**
  314. * to get standard dept code with four numbers
  315. *
  316. * @param sd
  317. * @return
  318. */
  319. public static String getStandardDeptId(String sd) {
  320. if(!StringUtil.isEmpty(sd)) {
  321. String zero = "";
  322. for(int i=0;i<4-sd.length();i++) {
  323. zero+="0";
  324. }
  325. return zero+sd;
  326. }
  327. return "";
  328. }
  329. public static JSONObject jsonResult(){
  330. return jsonResult(1,"SUCCESS");
  331. }
  332. public static JSONObject jsonResult(int code, String message){
  333. JSONObject j=new JSONObject();
  334. j.put(CODE, code);
  335. j.put(MESSAGE, message);
  336. return j;
  337. }
  338. public static JSONObject jsonResult(int code, String message, Object result){
  339. JSONObject j=new JSONObject();
  340. j.put(CODE, code);
  341. j.put(MESSAGE, message);
  342. Object rs = result;
  343. if(result instanceof com.common.json.JSONObject
  344. || result instanceof com.common.json.JSONArray){
  345. rs = result.toString();
  346. }
  347. j.put("Result", rs);
  348. return j;
  349. }
  350. public static void write(HttpServletResponse response,String value) {
  351. try {
  352. response.setContentType("text/html;charset=UTF-8");
  353. response.getWriter().write(value);
  354. } catch (IOException e) {
  355. e.printStackTrace();
  356. }
  357. }
  358. public static String getRandomNumber(int length){
  359. Random random = new Random();
  360. random.setSeed(System.currentTimeMillis());
  361. String code= Integer.toString(Math.abs(random.nextInt()));
  362. if(code.length()>length){
  363. code=code.substring(0,length);
  364. }else{
  365. while(code.length()<length){
  366. code="0"+code;
  367. }
  368. }
  369. return code;
  370. }
  371. /**
  372. * 判断是否为内网IP
  373. * @param ip
  374. * @return
  375. */
  376. public static boolean checkInIp(String ip){
  377. String inIPs=AppConfig.getValue("inIp","10.,172.,192.,");//内部IP的识别网段
  378. boolean isInIp=false;//是否为内部IP
  379. if(inIPs!=null){
  380. String[] arrInIp=inIPs.split(",");
  381. for(String inIp:arrInIp){
  382. if(ip.startsWith(inIp)){
  383. isInIp=true;
  384. break;
  385. }
  386. }
  387. }
  388. return isInIp;
  389. }
  390. public static String getIp(HttpServletRequest request) {
  391. String ip = request.getRemoteAddr();
  392. boolean isInIp=checkInIp(ip);
  393. if(!isInIp){
  394. return ip;//如果不是内部ip,则直接返回
  395. }
  396. ip= request.getHeader("X-Forwarded-For");
  397. if(ip!=null&&!"".equals(ip) && !"unKnown".equalsIgnoreCase(ip)){
  398. //多次反向代理后会有多个ip值,第一个ip才是真实ip
  399. int index = ip.indexOf(",");
  400. if(index != -1){
  401. String[] arrIp=ip.split(",");
  402. for(String subIp:arrIp){
  403. if(!checkInIp(subIp)){//找到第一个不是内部IP的进行返回
  404. return subIp;
  405. }
  406. }
  407. }else{
  408. return ip;
  409. }
  410. }
  411. ip = request.getHeader("X-Real-IP");
  412. if(ip!=null&&!"".equals(ip)&& !"unKnown".equalsIgnoreCase(ip)){
  413. return ip;
  414. }
  415. return request.getRemoteAddr();
  416. }
  417. }