123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- package com.yihu.utils;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import java.util.Random;
- import java.util.UUID;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import net.sf.json.JSONObject;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- import org.dom4j.io.OutputFormat;
- import org.dom4j.io.XMLWriter;
- import com.coreframework.util.AppConfig;
- public class StringUtil {
- /**
- * Code节点名称
- */
- private static final String CODE = "Code";
- /**
- * Message节点名称
- */
- private static final String MESSAGE = "Message";
-
-
- public static String createUUID() {
- return UUID.randomUUID().toString()
- .replaceAll("-", "").toUpperCase();
- }
-
-
- public static String formatXML(String retStr) throws Exception {
- String res = null;
- Document doc = null;
- try {
- doc = DocumentHelper.parseText(retStr);
- } catch (DocumentException e) {
- e.printStackTrace();
- throw e;
- }
- XMLWriter output = null;
- OutputFormat format = OutputFormat.createPrettyPrint();
- format.setEncoding("GBK");
- format.setNewLineAfterDeclaration(false);
- format.setExpandEmptyElements(true);
- format.setSuppressDeclaration(true);
-
- OutputStream outputStream = new ByteArrayOutputStream();
- XMLWriter writer = new XMLWriter(outputStream, format);
- writer.write(doc);
- writer.close();
- res = outputStream.toString();
- return res;
- }
-
- // String is null or not
- public static boolean isEmpty(String str) {
- if ("".equals(str) || "null".equals(str) || "NULL".equals(str)
- || str == null || "undefined".equals(str)){
- return true;
- }
- if ("".equals(str.trim())){
- return true;
- }
- return false;
- }
- // List is null or not
- public static boolean isEmpty(List list) {
- if (list != null && list.size() > 0){
- return false;
- }
- return true;
- }
- // Map is null or not
- public static boolean isEmpty(Map map) {
- if (map == null || map.size() == 0){
- return true;
- }
- return false;
- }
- // Object is null or not
- public static boolean isEmpty(Object obj) {
- if (obj == null){
- return true;
- }
- return false;
- }
- // to do empty string
- public static String chEmptyStr(String str) {
- if ("".equals(str) || "null".equals(str) || "NULL".equals(str)
- || str == null){
- return "";
- }
- return str.trim();
- }
- public static String chMap(Map map, String key) {
- String res = "";
- if (map != null) {
- return chEmptyStr(map.get(key) + "");
- }
- return res;
- }
- // chang clob type to String
- // public static String clobToStr(Clob clob) throws Exception {
- // return (clob != null ? clob.getSubString(1, (int) clob.length()) : "");
- // }
- public static int parseInt(Integer i) {
- if (i == null){
- return 0;
- }
- return i;
- }
- public static int parseInt(Integer i, int v) {
- if (i == null){
- return v;
- }
- return i;
- }
- public static boolean isNum(String str) {
- Pattern pattern = Pattern.compile("[0-9]*");
- Matcher isNum = pattern.matcher(str);
- if (isNum.matches()){
- return true;
- }
- return false;
- }
- public static boolean isEnNum(String str) {
- String first = str.substring(0, 1);
- String last = str.substring(str.length() - 1, str.length());
- String middle = str.substring(1, str.length() - 1);
- if (StringUtil.isNum(middle)) {
- if (StringUtil.isNum(first)
- || Pattern.compile("(?i)[a-zA-Z]").matcher(first).find()) {
- if (StringUtil.isNum(last)
- || Pattern.compile("(?i)[a-zA-Z]").matcher(last).find()) {
- if (!"a".equals(first) && !"A".equals(first)) {
- return true;
- }
- }
- }
- }
- return false;
- }
-
- public static boolean checkDate(String date) {
- Date d = null;
- boolean res = false;
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- sdf.setLenient(false);
- try {
- d = sdf.parse(date);
- res = true;
- } catch (Exception e) {
- }
- return res;
- }
-
- public static Integer[] getBirthYMD(String identityID) {
- int y = 0, m = 0, d = 0;
- if (identityID.length() == 15) {
- y = Integer.parseInt("19" + identityID.substring(6, 8));
- m = Integer.parseInt(identityID.substring(8, 10));
- d = Integer.parseInt(identityID.substring(10, 12));
- }
- if (identityID.length() == 18) {
- y = Integer.parseInt(identityID.substring(6, 10));
- m = Integer.parseInt(identityID.substring(10, 12));
- d = Integer.parseInt(identityID.substring(12, 14));
- }
- Integer[] a = new Integer[3];
- a[0] = y;
- a[1] = m;
- a[2] = d;
- return a;
- }
-
- public static Integer[] getBirthYMDbyBirth(String birth) {
- int y = 0, m = 0, d = 0;
- if (!StringUtil.isEmpty(birth)) {
- y = Integer.parseInt(birth.substring(0, 4));
- m = Integer.parseInt(birth.substring(5, 7));
- d = Integer.parseInt(birth.substring(8, 10));
- }
- Integer[] a = new Integer[3];
- a[0] = y;
- a[1] = m;
- a[2] = d;
- return a;
- }
-
- public static boolean isNeedVirtual(String cardno) {
- if (StringUtil.isEmpty(cardno)){
- return true;
- }
- if (cardno.trim().length() != 8 && cardno.trim().length() != 12){
- return true;
- }
- return false;
- }
- public static String getWeek(int weekindex) {
- switch (weekindex) {
- case 0:
- return "星期天";
- case 1:
- return "星期一";
- case 2:
- return "星期二";
- case 3:
- return "星期三";
- case 4:
- return "星期四";
- case 5:
- return "星期五";
- case 6:
- return "星期六";
- default:
- return "";
- }
- }
- /**
- * 判断字符串是否存在
- *
- * @param str
- * @param arr
- * 英文逗号隔开的字符串数组
- * @return
- */
- public static boolean strInArr(String str, String[] arr) {
- boolean b = false;
- if (arr != null && arr.length > 0) {
- for (String s : arr) {
- if (str.equals(s)) {
- b = true;
- break;
- }
- }
- }
- return b;
- }
- /**
- * 去掉数组第一个值
- *
- * @param arr
- * @return
- */
- public static String deleteFirstNode(String[] arr) {
- if (arr.length < 2){
- return null;
- }
- String[] arrNew = new String[arr.length - 1];
- for (int i = 0; i < arr.length; i++) {
- if (i == 0) {
- continue;
- }
- arrNew[i - 1] = arr[i];
- }
- String ts = "";
- for (String s : arrNew) {
- ts += s + ",";
- }
- return ts.substring(0, ts.length() - 1);
- }
- public static String delHtmlTagaaa(String str1) {
- if(StringUtil.isEmpty(str1)) {
- return "";
- }
- String str = "";
- str = str1;
- str = str.replaceAll("</?[^>]+>", ""); // 剔出了<html>的标签
- str = str.replace(" ", "");
- //str = str.replace(".", "");
- //str = str.replace("\"", "‘");
- //str = str.replace("'", "‘");
- str=str.replaceAll("'", "\"");
-
- return str;
- }
-
- public static String editHtmlTag(String str) {
- if(StringUtil.isEmpty(str)) {
- return "";
- }
- return str.replaceAll("'", "\"");
- }
- public static String retrim(String str) {
- if(StringUtil.isEmpty(str)) {
- return "";
- }
- String temp=str.trim();
- temp=temp.replaceAll(" ", "");
- temp=temp.replaceAll(" ", "");
-
- return temp;
- }
-
- public static String getPermitway(String waystatus) {
- String a = waystatus.substring(0,1);
- String b = waystatus.substring(1,2);
- String c = waystatus.substring(2,3);
- List<String> list = new ArrayList<String>();
-
- if("1".equals(a)) {
- list.add("1");
- }
- if("1".equals(b)) {
- list.add("2");
- }
- if("1".equals(c)) {
- list.add("3");
- }
- String temp="";
- if(list!=null&&list.size()>0) {
- for(String str : list) {
- temp+=str+",";
- }
- temp = temp.substring(0,temp.length()-1);
- }
- return temp;
- }
-
- /**
- * to get standard dept code with four numbers
- *
- * @param sd
- * @return
- */
- public static String getStandardDeptId(String sd) {
- if(!StringUtil.isEmpty(sd)) {
- String zero = "";
- for(int i=0;i<4-sd.length();i++) {
- zero+="0";
- }
- return zero+sd;
- }
- return "";
- }
-
- public static JSONObject jsonResult(){
- return jsonResult(1,"SUCCESS");
- }
-
- public static JSONObject jsonResult(int code, String message){
- JSONObject j=new JSONObject();
- j.put(CODE, code);
- j.put(MESSAGE, message);
- return j;
- }
-
- public static JSONObject jsonResult(int code, String message, Object result){
- JSONObject j=new JSONObject();
- j.put(CODE, code);
- j.put(MESSAGE, message);
- Object rs = result;
- if(result instanceof com.common.json.JSONObject
- || result instanceof com.common.json.JSONArray){
- rs = result.toString();
- }
- j.put("Result", rs);
- return j;
- }
-
- public static void write(HttpServletResponse response,String value) {
- try {
- response.setContentType("text/html;charset=UTF-8");
- response.getWriter().write(value);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static String getRandomNumber(int length){
- Random random = new Random();
- random.setSeed(System.currentTimeMillis());
- String code= Integer.toString(Math.abs(random.nextInt()));
- if(code.length()>length){
- code=code.substring(0,length);
- }else{
- while(code.length()<length){
- code="0"+code;
- }
-
- }
- return code;
- }
-
- /**
- * 判断是否为内网IP
- * @param ip
- * @return
- */
- public static boolean checkInIp(String ip){
- String inIPs=AppConfig.getValue("inIp","10.,172.,192.,");//内部IP的识别网段
- boolean isInIp=false;//是否为内部IP
- if(inIPs!=null){
- String[] arrInIp=inIPs.split(",");
- for(String inIp:arrInIp){
- if(ip.startsWith(inIp)){
- isInIp=true;
- break;
- }
- }
- }
- return isInIp;
- }
-
- public static String getIp(HttpServletRequest request) {
- String ip = request.getRemoteAddr();
- boolean isInIp=checkInIp(ip);
- if(!isInIp){
- return ip;//如果不是内部ip,则直接返回
- }
-
- ip= request.getHeader("X-Forwarded-For");
- if(ip!=null&&!"".equals(ip) && !"unKnown".equalsIgnoreCase(ip)){
-
-
- //多次反向代理后会有多个ip值,第一个ip才是真实ip
- int index = ip.indexOf(",");
- if(index != -1){
- String[] arrIp=ip.split(",");
- for(String subIp:arrIp){
- if(!checkInIp(subIp)){//找到第一个不是内部IP的进行返回
- return subIp;
- }
- }
- }else{
- return ip;
- }
- }
- ip = request.getHeader("X-Real-IP");
- if(ip!=null&&!"".equals(ip)&& !"unKnown".equalsIgnoreCase(ip)){
- return ip;
- }
- return request.getRemoteAddr();
- }
- }
|