java 用’逗号’切割字符串 (中英文’逗号’通用)
public class SplitUtil {
public static List<String> stringSplit(String str) {
List<String> lis = new ArrayList<>();
if (str.indexOf(",") != -1) {
String[] split = str.split(",");
for (int i = 0; i < split.length; i++) {
String url = split[i];
lis.add(url);
}
} else if (str.indexOf(",") != -1) {
String[] split = str.split(",");
for (int i = 0; i < split.length; i++) {
String url = split[i];
lis.add(url);
}
} else {
String[] split = str.split(",");
for (int i = 0; i < split.length; i++) {
String url = split[i];
lis.add(url);
}
}
return lis;
}
}
Java SHA-256加密
1、利用Apache的工具类实现加密:
导入maven依赖
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${common-codec.version}</version>
</dependency>
实现代码:
/***
* 利用Apache的工具类实现SHA-256加密
* @param str 加密后的报文
* @return
*/
public static String getSHA256Str(String str) {
MessageDigest messageDigest;
String encdeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
encdeStr = Hex.encodeHexString(hash);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encdeStr;
}
2、利用Java自带的实现加密:
/**
* 利用java原生的摘要实现SHA256加密
*
* @param str 加密后的报文
* @return
*/
public static String getSHA256StrJava(String str) {
MessageDigest messageDigest;
String encodeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8"));
encodeStr = byte2Hex(messageDigest.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encodeStr;
}
/**
* 将byte转为16进制
*
* @param bytes
* @return
*/
private static String byte2Hex(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length() == 1) {
//1得到一位的进行补0操作
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString();
}
断言工具类
参考链接:https://blog.youkuaiyun.com/weixin_42581660/article/details/114969289
public class AssertUtil {
/**
* 功能描述:
* 〈判断数组是否为空〉
*/
public static <T> boolean isEmpty(T[] obj) {
return null == obj || 0 == obj.length;
}
/**
* 功能描述:
* 〈判断数组是否不为空〉
*/
public static <T> boolean isNotEmpty(T[] obj) {
return !isEmpty(obj);
}
/**
* 功能描述:
* 〈判断对象是否为空〉
*/
public static boolean isEmpty(Object obj) {
return null == obj;
}
/**
* 功能描述:
* 〈判断对象是否不为空〉
*/
public static boolean isNotEmpty(Object obj) {
return !isEmpty(obj);
}
/**
* 功能描述:
* 〈字符串是否为空〉
*/
public static boolean isEmpty(String str) {
return null == str || "".equals(str);
}
/**
* 功能描述:
* 〈字符串是否不为空〉
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* 功能描述:
* 〈判断集合是否为空〉
*/
public static boolean isEmpty(Collection obj) {
return null == obj || obj.isEmpty();
}
/**
* 功能描述:
* 〈判断集合是否不为空〉
*/
public static boolean isNotEmpty(Collection obj) {
return !isEmpty(obj);
}
/**
* 功能描述:
* 〈判断map集合是否为空〉
*/
public static boolean isEmpty(Map obj) {
return null == obj || obj.isEmpty();
}
/**
* 功能描述:
* 〈判断map集合是否不为空〉
*/
public static boolean isNotEmpty(Map obj) {
return !isEmpty(obj);
}
/**
* 功能描述:
* 〈char数值是否是数字〉
*/
public static boolean charIsNumb(int charValue) {
return charValue >= 48 && charValue <= 57 || charValue >= 96 && charValue <= 105;
}
/**
* 功能描述:
* 〈判断字符串是否是纯数字浮点类型〉
*/
public static boolean isFloat(String s) {
if (!(s.indexOf(".") > -1)) {
return false;
}
char[] chars = s.toCharArray();
boolean flag = true;
for (char aChar : chars) {
if (aChar != 46) {
if (!(aChar >= 48 && aChar <= 57 || aChar >= 96 && aChar <= 105)) {
flag = false;
break;
}
}
}
return flag;
}
/**
* 功能描述:
* 〈非纯数字浮点类型〉
*/
public static boolean isNotFloat(String s) {
return !isFloat(s);
}
/**
* 功能描述:
* 〈字符串是否是数字〉
*/
public static boolean isNumb(String str) {
if (isEmpty((Object) str)) {
return false;
} else {
char[] chr = str.toCharArray();
for (int i = 0; i < chr.length; ++i) {
if (chr[i] < '0' || chr[i] > '9') {
return false;
}
}
return true;
}
}
/**
* 功能描述:
* 〈判断字符串是否不是数字〉
*/
public static boolean isNotNumb(String str) {
return !isNumb(str);
}
/**
* 功能描述:
* 〈判断字符串是否有长度,并自定义异常信息〉
*/
public static void hasLength(String str, String msg) {
if (str == null || str.length() < 1) {
throw new RuntimeException(msg);
}
}
/**
* 功能描述:
* 〈自定义参数校验异常〉
*/
public static void paramCheck(String msg, Object... obj) {
for (Object o : obj) {
// 参数异常
if (isEmpty(o)) {
throw new RuntimeException(msg);
}
}
}
/**
* 功能描述:
* 〈可变参数,判断是否所有对象都为空〉
*/
public static boolean isAllEmpty(Object... obj) {
Object[] var1 = obj;
int var2 = obj.length;
for (int var3 = 0; var3 < var2; ++var3) {
Object o = var1[var3];
if (!isEmpty(o)) {
return false;
}
}
return true;
}
/**
* 功能描述:
* 〈可变参数-判断只要有任意一个对象为空,则为true〉
*/
public static boolean isAnyEmpty(Object... obj) {
Object[] var1 = obj;
int var2 = obj.length;
for (int var3 = 0; var3 < var2; ++var3) {
Object o = var1[var3];
if (isEmpty(o)) {
return true;
}
}
return false;
}
/**
* 功能描述:
* 〈可变参数 -判断是否所有参数都不为空〉
*/
public static boolean isAllNotEmpty(Object... obj) {
Object[] var1 = obj;
int var2 = obj.length;
for (int var3 = 0; var3 < var2; ++var3) {
Object o = var1[var3];
if (isEmpty(o)) {
return false;
}
}
return true;
}
/**
* 功能描述:
* 〈判断是否两个对象相等〉
*/
public static boolean isEqual(Object o1, Object o2) {
if (o1 == null) {
return o2 == null;
} else if (o2 == null) {
return false;
} else if (o1.getClass().isArray()) {
for (int i = 0; i < ((Object[]) ((Object[]) o1)).length; ++i) {
if (!isEqual(((Object[]) ((Object[]) o1))[i], ((Object[]) ((Object[]) o2))[i])) {
return false;
}
}
return true;
} else if (Collection.class.isAssignableFrom(o1.getClass())) {
Iterator i1 = ((Collection) o1).iterator();
Iterator i2 = ((Collection) o2).iterator();
if (((Collection) o1).size() != ((Collection) o2).size()) {
return false;
} else {
for (int i = 0; i < ((Collection) o1).size(); ++i) {
if (!isEqual(i1.next(), i2.next())) {
return false;
}
}
return true;
}
} else if (!Map.class.isAssignableFrom(o1.getClass())) {
return o1.equals(o2);
} else {
Map<Object, Object> m1 = (Map) o1;
Map<Object, Object> m2 = (Map) o2;
if (m1.size() != m2.size()) {
return false;
} else if (!isEqual(m1.keySet(), m2.keySet())) {
return false;
} else {
Iterator var4 = m1.entrySet().iterator();
Map.Entry o;
do {
if (!var4.hasNext()) {
return true;
}
o = (Map.Entry) var4.next();
} while (m2.containsKey(o.getKey()) && isEqual(o.getValue(), m2.get(o.getKey())));
return false;
}
}
}
/**
* 功能描述:
* 〈判断两个对象是否不相等〉
*/
public static boolean isNotEqual(Object o1, Object o2) {
return !isEqual(o1, o2);
}
/**
* 功能描述:
* 〈比较两个集合是否相等〉
*/
public static boolean compare(List<Comparable> l1, List<Comparable> l2) {
if (l1 != null && !l1.isEmpty()) {
if (l2 != null && !l2.isEmpty()) {
Collections.sort(l1);
Collections.sort(l2);
if (l1.size() != l2.size()) {
return false;
} else {
for (int i = 0; i < l1.size(); ++i) {
if (((Comparable) l1.get(i)).compareTo(l2.get(i)) != 0) {
return false;
}
}
return true;
}
} else {
return false;
}
} else {
return l2 == null || l2.isEmpty();
}
}
}
这个工具类就可以抛出自定义异常,可以方便写serviceimpl的时候少些几行代码
除了可以抛出自定义异常外,里面抛出异常的位置都可以改写为你自己想写的东西,比如打印日志啊,返回boolean类型啊,等等等等
public class AssertUtil {
//如果不是true,则抛异常
public static void isTrue(boolean expression, String msg) {
if (!expression) {
throw new BusinessException(msg);
}
}
//如果是true,则抛异常
public static void isFalse(boolean expression, String msg) {
if (expression) {
throw new BusinessException(msg);
}
}
//如果是空或者空字符串,则抛异常
public static void isNotEmpty(String str, String msg) {
if (StringUtils.isEmpty(str)) {
throw new BusinessException(msg);
}
}
//如果数组为空或者长度小于1,则抛异常
public static void isNotEmpty(Object[] str, String msg) {
if (CollectionUtils.isEmpty(str)) {
throw new BusinessException(msg);
}
}
//如果集合为空或者长度小于1,则抛异常
public static void isNotEmpty(Collection collection, String msg) {
if (CollectionUtils.isEmpty(collection)) {
throw new BusinessException(msg);
}
}
//如果字符串为空或者长度小于1,则放行,否则抛异常
public static void isEmpty(String str, String msg) {
if (StringUtils.isNotEmpty(str)) {
throw new BusinessException(msg);
}
}
//如果集合为空或者长度小于1,则放行,否则抛异常
public static void isEmpty(Collection collection, String msg) {
if (CollectionUtils.isNotEmpty(collection)) {
throw new BusinessException(msg);
}
}
//如果集合为空或者长度小于1,则抛异常
public static void isNotEmpty(Map map, String msg) {
if (MapUtils.isEmpty(map)) {
throw new BusinessException(msg);
}
}
//如果对象时空,则抛异常
public static void isNotNull(Object object, String msg) {
if (object == null) {
throw new BusinessException(msg);
}
}
public static void isNotNull(Object[] objects, String msg) {
if (CollectionUtils.isEmpty(objects)) {
throw new BusinessException(msg);
}
}
//对象数组空
public static void isNotNull(Object[] objects, ResponseCode responseCode, String msg) {
if (CollectionUtils.isEmpty(objects)) {
throw new BusinessException(responseCode, msg);
}
}
//如果不是true,则抛异常
public static void isTrue(boolean expression, ResponseCode responseCode, String msg) {
if (!expression) {
throw new BusinessException(responseCode, msg);
}
}
//如果是空或者空字符串,则抛异常
public static void isNotEmpty(String str, ResponseCode responseCode, String msg) {
if (StringUtils.isEmpty(str)) {
throw new BusinessException(responseCode, msg);
}
}
//如果数组为空或者长度小于1,则抛异常
public static void isNotEmpty(Object[] str, ResponseCode responseCode, String msg) {
if (CollectionUtils.isEmpty(str)) {
throw new BusinessException(responseCode, msg);
}
}
//如果集合为空或者长度小于1,则抛异常
public static void isNotEmpty(Collection collection, ResponseCode responseCode, String msg) {
if (CollectionUtils.isEmpty(collection)) {
throw new BusinessException(responseCode, msg);
}
}
//如果集合为空或者长度小于1,则抛异常
public static void isNotEmpty(Map map, ResponseCode responseCode, String msg) {
if (MapUtils.isEmpty(map)) {
throw new BusinessException(responseCode, msg);
}
}
//如果对象时空,则抛异常
public static void isNotNull(Object object, ResponseCode responseCode, String msg) {
if (object == null) {
throw new BusinessException(responseCode, msg);
}
}
//比较对象
public static void equals(Object obj1, Object obj2, String msg) {
if (!equals(obj1, obj2)) {
throw new BusinessException(msg);
}
}
//比较对象的相等
public static boolean equals(final Object cs1, final Object cs2) {
if (cs1 == cs2) {
return true;
}
if (null == cs1 && null != cs2) {
return false;
}
if (null != cs1 && null == cs2) {
return false;
}
if (cs1 == null || null == cs2) {
return true;
}
if (cs1 instanceof String && cs2 instanceof String) {
return cs1.equals(cs2);
}
return cs1.equals(cs2);
}
}
获取文件的MD5值
DigestUtils.md5Hex(new FileInputStream(path));