对比新文件 |
| | |
| | | package com.gk.firework.Domain.Utils; |
| | | |
| | | import sun.misc.BASE64Encoder; |
| | | |
| | | import java.math.BigInteger; |
| | | import java.nio.charset.Charset; |
| | | import java.security.MessageDigest; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * 继承自Spring util的工具类,减少jar依赖 |
| | | * @author L.cm |
| | | */ |
| | | public class StringUtils extends org.springframework.util.StringUtils { |
| | | /** |
| | | * Check whether the given {@code CharSequence} contains actual <em>text</em>. |
| | | * <p>More specifically, this method returns {@code true} if the |
| | | * {@code CharSequence} is not {@code null}, its length is greater than |
| | | * 0, and it contains at least one non-whitespace character. |
| | | * <p><pre class="code"> |
| | | * StringUtils.isBlank(null) = true |
| | | * StringUtils.isBlank("") = true |
| | | * StringUtils.isBlank(" ") = true |
| | | * StringUtils.isBlank("12345") = false |
| | | * StringUtils.isBlank(" 12345 ") = false |
| | | * </pre> |
| | | * @param str the {@code CharSequence} to check (may be {@code null}) |
| | | * @return {@code true} if the {@code CharSequence} is not {@code null}, |
| | | * its length is greater than 0, and it does not contain whitespace only |
| | | * @see Character#isWhitespace |
| | | */ |
| | | public static boolean isBlank(final CharSequence cs) { |
| | | return !StringUtils.isNotBlank(cs); |
| | | } |
| | | |
| | | /** |
| | | * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p> |
| | | * |
| | | * <pre> |
| | | * StringUtils.isNotBlank(null) = false |
| | | * StringUtils.isNotBlank("") = false |
| | | * StringUtils.isNotBlank(" ") = false |
| | | * StringUtils.isNotBlank("bob") = true |
| | | * StringUtils.isNotBlank(" bob ") = true |
| | | * </pre> |
| | | * |
| | | * @param cs the CharSequence to check, may be null |
| | | * @return {@code true} if the CharSequence is |
| | | * not empty and not null and not whitespace |
| | | * @see Character#isWhitespace |
| | | */ |
| | | public static boolean isNotBlank(final CharSequence cs) { |
| | | return StringUtils.hasText(cs); |
| | | } |
| | | |
| | | /** |
| | | * Convert a {@code Collection} into a delimited {@code String} (e.g. CSV). |
| | | * <p>Useful for {@code toString()} implementations. |
| | | * @param coll the {@code Collection} to convert |
| | | * @param delim the delimiter to use (typically a ",") |
| | | * @return the delimited {@code String} |
| | | */ |
| | | public static String join(Collection<?> coll, String delim) { |
| | | return StringUtils.collectionToDelimitedString(coll, delim); |
| | | } |
| | | |
| | | /** |
| | | * Convert a {@code String} array into a delimited {@code String} (e.g. CSV). |
| | | * <p>Useful for {@code toString()} implementations. |
| | | * @param arr the array to display |
| | | * @param delim the delimiter to use (typically a ",") |
| | | * @return the delimited {@code String} |
| | | */ |
| | | public static String join(Object[] arr, String delim) { |
| | | return StringUtils.arrayToDelimitedString(arr, delim); |
| | | } |
| | | |
| | | public static List<String> toList(String id) { |
| | | List<String> list = new ArrayList<String>(); |
| | | String[] str = id.split(","); |
| | | for (int i = 0; i < str.length; i++) { |
| | | list.add(str[i]); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | public static String todbstr(String str) { |
| | | return "'"+ str +"'"; |
| | | } |
| | | |
| | | public static String MD5Encoder(String s,String charset){ |
| | | try{ |
| | | byte[] btInput = s.getBytes(charset); |
| | | MessageDigest mdInst = MessageDigest.getInstance("MD5"); |
| | | mdInst.update(btInput); |
| | | |
| | | byte[] md = mdInst.digest(); |
| | | StringBuffer sb = new StringBuffer(); |
| | | for(int i =0 ;i<md.length;i++) |
| | | { |
| | | int val = ((int)md[i]) & 0xff; |
| | | if(val<16){ |
| | | sb.append("0"); |
| | | } |
| | | sb.append(Integer.toHexString(val)); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public static String getMD5(String str) { |
| | | try { |
| | | MessageDigest md = MessageDigest.getInstance("MD5"); |
| | | md.update(str.getBytes()); |
| | | // digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符 |
| | | // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值 |
| | | return new BigInteger(1, md.digest()).toString(16); |
| | | } catch (Exception e) { |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | public static String MD5OpenApiEncoder(String s,String charset){ |
| | | try{ |
| | | byte[] btInput = s.getBytes(charset); |
| | | MessageDigest mdInst = MessageDigest.getInstance("MD5"); |
| | | mdInst.update(btInput); |
| | | |
| | | byte[] md = mdInst.digest(); |
| | | StringBuffer sb = new StringBuffer(); |
| | | for(int i =0 ;i<md.length;i++) |
| | | { |
| | | int val = ((int)md[i]) & 0xff; |
| | | if(val<16){ |
| | | sb.append("0"); |
| | | } |
| | | sb.append(Integer.toHexString(val)); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public static String md5(String message) { |
| | | try { |
| | | MessageDigest md; |
| | | |
| | | md = MessageDigest.getInstance("md5"); |
| | | byte m5[] = md.digest(message.getBytes()); |
| | | BASE64Encoder encoder = new BASE64Encoder(); |
| | | return encoder.encode(m5); |
| | | } catch (Exception e) { |
| | | // TODO Auto-generated catch block |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | public static Boolean isSame(String str1,String str2) |
| | | { |
| | | |
| | | if(StringUtils.isNotBlank(str1) && StringUtils.isBlank(str2)) |
| | | { |
| | | return false; |
| | | } |
| | | else if(StringUtils.isBlank(str1) && StringUtils.isNotBlank(str2)) |
| | | { |
| | | return false; |
| | | } |
| | | else if(StringUtils.isBlank(str1) && StringUtils.isBlank(str2)) |
| | | { |
| | | return true; |
| | | } |
| | | else { |
| | | if(!str1.equals(str2)) { |
| | | return false; |
| | | } |
| | | else { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static boolean isContainChinese(String str){ |
| | | |
| | | if (StringUtils.isBlank(str)) { |
| | | return false; |
| | | } |
| | | Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]"); |
| | | Matcher m = p.matcher(str); |
| | | if (m.find()) { |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | public static String ytoMd5(String sourceStr) { |
| | | String result = ""; |
| | | try { |
| | | MessageDigest md5 = MessageDigest.getInstance("MD5"); |
| | | byte[] bytes = md5.digest((sourceStr).getBytes(Charset.forName("utf-8"))); |
| | | result = new String(org.apache.commons.codec.binary.Base64.encodeBase64(bytes),"utf-8"); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | } |