|
@@ -0,0 +1,501 @@
|
|
|
+package com.hrsk.cloud.eg.infrastructure.utils;
|
|
|
+
|
|
|
+
|
|
|
+import org.apache.commons.lang3.ArrayUtils;
|
|
|
+
|
|
|
+import java.lang.reflect.Array;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 字符操作公用类
|
|
|
+ */
|
|
|
+public class StringDecodeUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字符串后几位替换成另一个值
|
|
|
+ *
|
|
|
+ * @param str 源字符串
|
|
|
+ * @param lastStr 要替换的字符串
|
|
|
+ * @param lastLength 需要替换的源字符串后几位
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String replaceLast(String str, String lastStr, int lastLength) {
|
|
|
+ if (notNullAndEmpty(str) && lastStr != null && str.length() >= lastLength) {
|
|
|
+ str = str.substring(0, str.length() - lastLength) + lastStr;
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符空NULL判断
|
|
|
+ *
|
|
|
+ * @param @param obj
|
|
|
+ * @param @return 设定文件
|
|
|
+ * @return Boolean 返回类型
|
|
|
+ * @throws
|
|
|
+ * @Title: isNullEmpty
|
|
|
+ * @Description: TODO
|
|
|
+ */
|
|
|
+ public static Boolean notNullAndEmpty(Object obj) {
|
|
|
+ return (obj != null && !"".equals(obj.toString()) && !"null".equalsIgnoreCase(obj.toString()));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * unicode转汉字
|
|
|
+ *
|
|
|
+ * @param @param theString
|
|
|
+ * @param @return 设定文件
|
|
|
+ * @return String 返回类型
|
|
|
+ * @throws
|
|
|
+ * @Title: decodeUnicode
|
|
|
+ * @Description: TODO
|
|
|
+ */
|
|
|
+ public static String decodeUnicode(String theString) {
|
|
|
+ char aChar;
|
|
|
+ int len = theString.length();
|
|
|
+ StringBuffer outBuffer = new StringBuffer(len);
|
|
|
+ for (int x = 0; x < len; ) {
|
|
|
+ aChar = theString.charAt(x++);
|
|
|
+ if (aChar == '\\') {
|
|
|
+ aChar = theString.charAt(x++);
|
|
|
+ if (aChar == 'u') {
|
|
|
+ int value = 0;
|
|
|
+ for (int i = 0; i < 4; i++) {
|
|
|
+ aChar = theString.charAt(x++);
|
|
|
+ switch (aChar) {
|
|
|
+ case '0':
|
|
|
+ case '1':
|
|
|
+ case '2':
|
|
|
+ case '3':
|
|
|
+ case '4':
|
|
|
+ case '5':
|
|
|
+ case '6':
|
|
|
+ case '7':
|
|
|
+ case '8':
|
|
|
+ case '9':
|
|
|
+ value = (value << 4) + aChar - '0';
|
|
|
+ break;
|
|
|
+ case 'a':
|
|
|
+ case 'b':
|
|
|
+ case 'c':
|
|
|
+ case 'd':
|
|
|
+ case 'e':
|
|
|
+ case 'f':
|
|
|
+ value = (value << 4) + 10 + aChar - 'a';
|
|
|
+ break;
|
|
|
+ case 'A':
|
|
|
+ case 'B':
|
|
|
+ case 'C':
|
|
|
+ case 'D':
|
|
|
+ case 'E':
|
|
|
+ case 'F':
|
|
|
+ value = (value << 4) + 10 + aChar - 'A';
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new IllegalArgumentException(
|
|
|
+ "Malformed encoding.");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ outBuffer.append((char) value);
|
|
|
+ } else {
|
|
|
+ if (aChar == 't') {
|
|
|
+ aChar = '\t';
|
|
|
+ } else if (aChar == 'r') {
|
|
|
+ aChar = '\r';
|
|
|
+ } else if (aChar == 'n') {
|
|
|
+ aChar = '\n';
|
|
|
+ } else if (aChar == 'f') {
|
|
|
+ aChar = '\f';
|
|
|
+ }
|
|
|
+ outBuffer.append(aChar);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ outBuffer.append(aChar);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return outBuffer.toString();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 空值检查
|
|
|
+ *
|
|
|
+ * @param pInput 要检查的字符串
|
|
|
+ * @return boolean 返回检查结果,但传入的字符串为空的场合,返回真
|
|
|
+ */
|
|
|
+ public static boolean isNull(Object pInput) {
|
|
|
+ // 判断参数是否为空或者’’
|
|
|
+ if (pInput == null || "’’".equals(pInput) || "".equals(pInput)
|
|
|
+ || "null".equals(pInput) || "\"null\"".equals(pInput)) {
|
|
|
+ return true;
|
|
|
+ } else if ("java.lang.String".equals(pInput.getClass().getName())) {
|
|
|
+ // 判断传入的参数的String类型的
|
|
|
+ // 替换各种空格
|
|
|
+ String tmpInput = Pattern.compile("[\\r|\\n|\\u3000]").matcher(
|
|
|
+ (String) pInput).replaceAll("");
|
|
|
+ // 匹配空
|
|
|
+ return Pattern.compile("^(\\s)*$").matcher(tmpInput).matches();
|
|
|
+ } else {
|
|
|
+ // 方法类
|
|
|
+ Method method = null;
|
|
|
+ try {
|
|
|
+ // 访问传入参数的size方法
|
|
|
+ method = pInput.getClass().getMethod("size");
|
|
|
+ // 判断size大小
|
|
|
+ // size为0的场合
|
|
|
+ if (Integer.parseInt(String.valueOf(method.invoke(pInput))) == 0) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ try {
|
|
|
+ // 访问传入参数的getItemCount方法
|
|
|
+ method = pInput.getClass().getMethod("getItemCount");
|
|
|
+ // 判断size大小
|
|
|
+ // getItemCount为0的场合
|
|
|
+ if (Integer.parseInt(String.valueOf(method.invoke(pInput))) == 0) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ try {
|
|
|
+ // 判断传入参数的长度
|
|
|
+ // 长度为0的场合
|
|
|
+ if (Array.getLength(pInput) == 0) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } catch (Exception exx) {
|
|
|
+ try {
|
|
|
+ // 访问传入参数的hasNext方法
|
|
|
+ method = Iterator.class.getMethod("hasNext");
|
|
|
+ // 转换hasNext的值
|
|
|
+ return !Boolean.valueOf(String.valueOf(method
|
|
|
+ .invoke(pInput))) ? true : false;
|
|
|
+ } catch (Exception exxx) {
|
|
|
+ // 以上场合不满足返回假
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long obj2Long(Object val) {
|
|
|
+ if (notNullAndEmpty(val)) {
|
|
|
+ return Long.valueOf(val.toString().trim());
|
|
|
+ } else {
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Float obj2Float(Object val) {
|
|
|
+ if (notNullAndEmpty(val)) {
|
|
|
+ return Float.parseFloat(val.toString().trim());
|
|
|
+ } else {
|
|
|
+ return 0F;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Object to Double
|
|
|
+ *
|
|
|
+ * @return Double
|
|
|
+ * @throws Exception
|
|
|
+ * @author 刘滔
|
|
|
+ * @see
|
|
|
+ */
|
|
|
+ public static Double obj2Dou(Object val) {
|
|
|
+ if (notNullAndEmpty(val)) {
|
|
|
+ return Double.parseDouble(val.toString().trim());
|
|
|
+ } else {
|
|
|
+ return 0d;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Object to int
|
|
|
+ *
|
|
|
+ * @return Integer
|
|
|
+ * @throws Exception
|
|
|
+ * @author 刘滔
|
|
|
+ * @see
|
|
|
+ */
|
|
|
+ public static int obj2Int(Object val) {
|
|
|
+ if (notNullAndEmpty(val)) {
|
|
|
+ return Integer.parseInt(val.toString().trim());
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Object to String
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ * @throws Exception
|
|
|
+ * @author 刘滔
|
|
|
+ * @see
|
|
|
+ */
|
|
|
+ public static String obj2Str(Object val) {
|
|
|
+ if (notNullAndEmpty(val)) {
|
|
|
+ return val.toString().trim();
|
|
|
+ } else {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Object to boolean
|
|
|
+ *
|
|
|
+ * @param Object
|
|
|
+ * @return boolean
|
|
|
+ * @throws Exception
|
|
|
+ * @author 刘滔
|
|
|
+ * @see
|
|
|
+ */
|
|
|
+// public static boolean obj2Bln(Object val){
|
|
|
+// if(org.apache.commons.lang.xwork.StringUtils.equalsIgnoreCase("1",
|
|
|
+// val.toString().trim())){
|
|
|
+// return true;
|
|
|
+// }else{
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回唯一图片名称
|
|
|
+ *
|
|
|
+ * @param @return 设定文件
|
|
|
+ * @return String 返回类型
|
|
|
+ * @throws
|
|
|
+ * @Title: createImgName
|
|
|
+ */
|
|
|
+ public static String createImgName() {
|
|
|
+ Random r = new Random();
|
|
|
+ String name = new SimpleDateFormat("yyyyMMddHHmmssSSS")
|
|
|
+ .format(new Date())
|
|
|
+ + r.nextInt(100000) + ".jpg";
|
|
|
+ return name;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件后缀
|
|
|
+ *
|
|
|
+ * @param fileName
|
|
|
+ * @return
|
|
|
+ * @author 刘滔
|
|
|
+ * @see
|
|
|
+ */
|
|
|
+ public static String getExtention(String fileName) {
|
|
|
+ if (isNull(fileName)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ int pos = fileName.lastIndexOf(".");
|
|
|
+ return fileName.substring(pos);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回唯一名称字符串
|
|
|
+ *
|
|
|
+ * @param @return 设定文件
|
|
|
+ * @return String 返回类型
|
|
|
+ * @throws
|
|
|
+ * @Title: createName
|
|
|
+ */
|
|
|
+ public static String createName() {
|
|
|
+ Random r = new Random();
|
|
|
+ String name = new SimpleDateFormat("yMdHHssSSS").format(new Date());
|
|
|
+ return name;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将两个数组合并成一个数组
|
|
|
+ *
|
|
|
+ * @param first
|
|
|
+ * @param second
|
|
|
+ * @return
|
|
|
+ * @author 刘滔
|
|
|
+ * @see
|
|
|
+ */
|
|
|
+ public static String[] concat(String[] first, String[] second) {
|
|
|
+ if (isNull(first) && isNull(second)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (isNull(first) && !isNull(second)) {
|
|
|
+ return second;
|
|
|
+ }
|
|
|
+ if (!isNull(first) && isNull(second)) {
|
|
|
+ return first;
|
|
|
+ }
|
|
|
+ String[] both = (String[]) ArrayUtils.addAll(first, second);
|
|
|
+ return both;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串数组合并
|
|
|
+ *
|
|
|
+ * @param first
|
|
|
+ * @param second
|
|
|
+ * @return
|
|
|
+ * @author 刘滔
|
|
|
+ * @see
|
|
|
+ */
|
|
|
+ public static String[] concat2(String[] first, String[] second) {
|
|
|
+ // if (isNull(first) && isNull(second)) {
|
|
|
+ // return null;
|
|
|
+ // }
|
|
|
+ // if(isNull(first) && !isNull(second)) {
|
|
|
+ // return second;
|
|
|
+ // }
|
|
|
+ // if(!isNull(first) && isNull(second)) {
|
|
|
+ // return first;
|
|
|
+ // }
|
|
|
+ String[] both = new String[first.length + second.length];
|
|
|
+ System.arraycopy(first, 0, both, 0, first.length);
|
|
|
+ System.arraycopy(second, 0, both, first.length, second.length);
|
|
|
+ return both;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * 可将多个数组合并
|
|
|
+ *
|
|
|
+ * @param first
|
|
|
+ * @param rest
|
|
|
+ * @return
|
|
|
+ * @author 刘滔
|
|
|
+ * @see
|
|
|
+ */
|
|
|
+ // public static T[] concatAll(T[] first, T[]... rest) {
|
|
|
+ // int totalLength = first.length;
|
|
|
+ // for (T[] array : rest) {
|
|
|
+ // totalLength += array.length;
|
|
|
+ // }
|
|
|
+ // T[] result = Arrays.copyOf(first, totalLength);
|
|
|
+ // int offset = first.length;
|
|
|
+ // for (T[] array : rest) {
|
|
|
+ // System.arraycopy(array, 0, result, offset, array.length);
|
|
|
+ // offset += array.length;
|
|
|
+ // }
|
|
|
+ // return result;
|
|
|
+ // }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将路径中的斜杠统一
|
|
|
+ *
|
|
|
+ * @param path
|
|
|
+ * @author 刘滔
|
|
|
+ * @see
|
|
|
+ */
|
|
|
+ public static String replacePath(String path) {
|
|
|
+ if (path == "" || path == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ char[] chars = path.toCharArray();
|
|
|
+ StringBuffer sbStr = new StringBuffer(256);
|
|
|
+ int len = chars.length;
|
|
|
+ for (int i = 0; i < len; i++) {
|
|
|
+ if ('\\' == chars[i]) {
|
|
|
+ sbStr.append('/');
|
|
|
+ } else {
|
|
|
+ sbStr.append(chars[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sbStr.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字符串转换成数组
|
|
|
+ *
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ * @author 刘滔
|
|
|
+ * @see
|
|
|
+ */
|
|
|
+ public static String[] toArray(String str) {
|
|
|
+ if (isNull(str)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ StringTokenizer sTokenizer = new StringTokenizer(str, ",");
|
|
|
+ List<String> sList = new ArrayList<String>();
|
|
|
+ while (sTokenizer.hasMoreTokens()) {
|
|
|
+ sList.add(sTokenizer.nextToken());
|
|
|
+ }
|
|
|
+ int size = sList.size();
|
|
|
+ String[] result = new String[size];
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ result[i] = sList.get(i);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 将List<Map>转为x,y轴的图表二维数组,Map的key必须为VAL_X和VAL_Y
|
|
|
+ * @Title: listMapToArray
|
|
|
+ * @Description: TODO
|
|
|
+ * @param @param list
|
|
|
+ * @param @return 设定文件
|
|
|
+ * @return Object[][] 返回类型
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+// public static Object[][] listMapToArray(List<Map> list,String type){
|
|
|
+// Object[][] xy=new Object[list.size()][2];
|
|
|
+// for (int i = 0; i < list.size(); i++) {
|
|
|
+// Map m=list.get(i);
|
|
|
+// if(type=="d"||type.equals("d")){
|
|
|
+// Date d=new Date(m.get("VAL_X").toString());
|
|
|
+// String strd=DateUtils.formatDate(d, DateUtils.YYYYMMDDHHMMSSPATTERN);
|
|
|
+// strd=DateUtils.addHour(strd, 8);
|
|
|
+// xy[i][0]=DateUtils.parseDate(strd).getTime();
|
|
|
+// }else{
|
|
|
+// xy[i][0]=m.get("VAL_X");
|
|
|
+// }
|
|
|
+// xy[i][1]=m.get("VAL_Y");
|
|
|
+// }
|
|
|
+// return xy;
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化时间
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @param format
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getFormDate(Date date, String format) {
|
|
|
+ if (null == date) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ SimpleDateFormat sf = new SimpleDateFormat(format);
|
|
|
+ return sf.format(date);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /*public static void main(String[] args) {
|
|
|
+// String strs = "25";
|
|
|
+// String[] str = toArray(strs);
|
|
|
+// int gidLen = str.length;
|
|
|
+// Long[] gids = new Long[gidLen];
|
|
|
+// for (int i = 0; i < gidLen; i++) {
|
|
|
+// log.info(">>> " + StringUtils.obj2Long(str[i]));
|
|
|
+// gids[i] = StringUtils.obj2Long(str[i]);
|
|
|
+// log.info(gids[i]);
|
|
|
+// }
|
|
|
+ Date d=new Date();
|
|
|
+ log.info(d.getTime());
|
|
|
+ // for (String string : str) {
|
|
|
+ // log.info(obj2Long(25));
|
|
|
+ // }
|
|
|
+
|
|
|
+ }*/
|
|
|
+
|
|
|
+}
|