implements Serializable {
return new Resps(code, msg);
}
+ public static Resps fail() {
+ return new Resps(RespsConsts.CODE_FAIL, RespsConsts.MSG_FAIL_OPERATE);
+ }
+
public static Resps fail(String msg) {
return new Resps(RespsConsts.CODE_FAIL, msg);
}
diff --git a/src/main/java/com/yexuejc/base/util/AlgorithmUtils.java b/src/main/java/com/yexuejc/base/util/AlgorithmUtil.java
similarity index 68%
rename from src/main/java/com/yexuejc/base/util/AlgorithmUtils.java
rename to src/main/java/com/yexuejc/base/util/AlgorithmUtil.java
index 9daf60a..faef09f 100644
--- a/src/main/java/com/yexuejc/base/util/AlgorithmUtils.java
+++ b/src/main/java/com/yexuejc/base/util/AlgorithmUtil.java
@@ -9,13 +9,13 @@ package com.yexuejc.base.util;
* @author: maxf
* @date: 2017年11月23日 下午3:17:58
*/
-public class AlgorithmUtils {
+public class AlgorithmUtil {
private static final int LENGTH_1 = 1;
private static final int LENGTH_2 = 2;
private static final int LENGTH_3 = 3;
private static final int LENGTH_36 = 36;
- private AlgorithmUtils() {
+ private AlgorithmUtil() {
}
private static final String X36 = "0123456789abcdefghijklmnopqrstuvwxyz";
@@ -77,7 +77,7 @@ public class AlgorithmUtils {
* @throw
*/
public static int x36ConvertTo10(String pStr) {
- if (pStr == "") {
+ if (StrUtil.isEmpty(pStr)) {
return 0;
}
// 目标十进制数初始化为0
@@ -107,4 +107,47 @@ public class AlgorithmUtils {
static class NextCodeException extends Exception {
private static final long serialVersionUID = 8956943499144648985L;
}
+
+ /**
+ * 16进制转为2进制
+ *
+ * @param hexString 16进制字符串
+ */
+ public static String x16ConvertTo2(String hexString) {
+ if (hexString == null || hexString.length() % 2 != 0) {
+ return null;
+ }
+ StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < hexString.length(); i++) {
+ String tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
+ buf.append(tmp.substring(tmp.length() - 4));
+ }
+ return buf.toString();
+ }
+
+ /**
+ * 16进制转为2进制byte[]
+ *
+ * @param hexString 16进制字符串
+ * @return
+ */
+ public static byte[] hexStringToBytes(String hexString) {
+ if (hexString == null || "".equals(hexString)) {
+ return null;
+ }
+ hexString = hexString.toUpperCase();
+ int length = hexString.length() / 2;
+ char[] hexChars = hexString.toCharArray();
+ byte[] d = new byte[length];
+ for (int i = 0; i < length; i++) {
+ int pos = i * 2;
+ d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
+ }
+ return d;
+ }
+
+ private static byte charToByte(char c) {
+ return (byte) "0123456789ABCDEF".indexOf(c);
+ }
+
}
diff --git a/src/main/java/com/yexuejc/base/util/DateTimeUtil.java b/src/main/java/com/yexuejc/base/util/DateTimeUtil.java
index abb4dd2..50533e5 100644
--- a/src/main/java/com/yexuejc/base/util/DateTimeUtil.java
+++ b/src/main/java/com/yexuejc/base/util/DateTimeUtil.java
@@ -13,6 +13,9 @@ import java.util.Date;
* @date: 2018/3/27 10:44
*/
public class DateTimeUtil {
+ private DateTimeUtil() {
+ }
+
/**
* 获取本年第一天
*
@@ -106,8 +109,9 @@ public class DateTimeUtil {
* @return
*/
public static LocalDate getWeek4First(LocalDate date) {
- TemporalAdjuster FIRST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));
- return date.with(FIRST_OF_WEEK);
+ TemporalAdjuster firstOfWeek = TemporalAdjusters.ofDateAdjuster(localDate ->
+ localDate.minusDays(localDate.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));
+ return date.with(firstOfWeek);
}
/**
@@ -126,8 +130,9 @@ public class DateTimeUtil {
* @return
*/
public static LocalDate getWeek4Last(LocalDate date) {
- TemporalAdjuster LAST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
- return date.with(LAST_OF_WEEK);
+ TemporalAdjuster lastOfWeek = TemporalAdjusters.ofDateAdjuster(localDate ->
+ localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
+ return date.with(lastOfWeek);
}
/**
@@ -268,22 +273,22 @@ public class DateTimeUtil {
return df.format(dateTime);
}
- public static void main(String[] args) {
-// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-// System.out.println(df.format(zonedDateTime2Date(ZonedDateTime.now())));
-// System.out.println(df2.format(date2ZonedDateTime(new Date())));
+ /** public static void main(String[] args) {
+ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ System.out.println(df.format(zonedDateTime2Date(ZonedDateTime.now())));
+ System.out.println(df2.format(date2ZonedDateTime(new Date())));
-// System.out.println(getWeek4First());
+ System.out.println(getWeek4First());
System.out.println(format(getWeek4First(LocalDate.parse("2018-02-10")).atTime(LocalTime.MIN)));
System.out.println(format(getWeek4Last(LocalDate.parse("2018-02-10")).atTime(LocalTime.MAX)));
-// System.out.println(format(getMonth4First().atTime(LocalTime.MIN)));
-// System.out.println(format(getMonth4Last().atTime(LocalTime.MAX)));
+ System.out.println(format(getMonth4First().atTime(LocalTime.MIN)));
+ System.out.println(format(getMonth4Last().atTime(LocalTime.MAX)));
-// System.out.println(format(getYear4First().atTime(LocalTime.MIN)));
-// System.out.println(format(getYear4Last().atTime(LocalTime.MAX)));
+ System.out.println(format(getYear4First().atTime(LocalTime.MIN)));
+ System.out.println(format(getYear4Last().atTime(LocalTime.MAX)));
- }
+ }*/
}
diff --git a/src/main/java/com/yexuejc/base/util/DateUtil.java b/src/main/java/com/yexuejc/base/util/DateUtil.java
index b037fcf..3053c21 100644
--- a/src/main/java/com/yexuejc/base/util/DateUtil.java
+++ b/src/main/java/com/yexuejc/base/util/DateUtil.java
@@ -7,6 +7,14 @@ import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
+/**
+ * java.util.Date 时间工具类
+ *
+ * @author maxf
+ * @ClassName DateUtil
+ * @Description
+ * @date 2018/9/3 15:27
+ */
public class DateUtil {
private DateUtil() {
}
@@ -71,7 +79,7 @@ public class DateUtil {
/**
* 日期字符串转date
*
- * @param date
+ * @param dateStr
* @return Date
* @throws ParseException
*/
@@ -81,7 +89,7 @@ public class DateUtil {
}
/**
- * 日期字符串转date
+ * date转字符串
*
* @param date
* @return Date
@@ -95,6 +103,34 @@ public class DateUtil {
}
}
+ /**
+ * 日期字符串转dateTime
+ *
+ * @param dateStr
+ * @return
+ * @throws ParseException
+ */
+ public static Date str2dateTime(String dateStr) throws ParseException {
+ Date date = DATE_TIME_FORMAT.parse(dateStr);
+ return date;
+ }
+
+ /**
+ * dateTime转字符串
+ *
+ * @param date
+ * @return Date
+ * @throws ParseException
+ */
+ public static String dateTime2str(Date date) throws ParseException {
+ if (date != null) {
+ return DATE_TIME_FORMAT.format(date);
+ } else {
+ return "null";
+ }
+ }
+
+
/**
* 获取本周的日期
*
diff --git a/src/main/java/com/yexuejc/base/util/ExcelImportUtils.java b/src/main/java/com/yexuejc/base/util/ExcelImportUtil.java
similarity index 84%
rename from src/main/java/com/yexuejc/base/util/ExcelImportUtils.java
rename to src/main/java/com/yexuejc/base/util/ExcelImportUtil.java
index ecd599a..bf4e6af 100644
--- a/src/main/java/com/yexuejc/base/util/ExcelImportUtils.java
+++ b/src/main/java/com/yexuejc/base/util/ExcelImportUtil.java
@@ -8,8 +8,9 @@ package com.yexuejc.base.util;
* @author: maxf
* @date: 2017/12/27 16:42
*/
-public class ExcelImportUtils {
-
+public class ExcelImportUtil {
+ private ExcelImportUtil() {
+ }
/**
* 是否是2003的excel,返回true是2003
@@ -40,7 +41,8 @@ public class ExcelImportUtils {
* @return
*/
public static boolean validateExcel(String filePath) {
- if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
+ boolean b = filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath));
+ if (b) {
return false;
}
return true;
diff --git a/src/main/java/com/yexuejc/base/util/FileUtil.java b/src/main/java/com/yexuejc/base/util/FileUtil.java
index ed99e4b..337b895 100644
--- a/src/main/java/com/yexuejc/base/util/FileUtil.java
+++ b/src/main/java/com/yexuejc/base/util/FileUtil.java
@@ -1,38 +1,69 @@
package com.yexuejc.base.util;
-import java.io.File;
-import java.io.IOException;
+import sun.misc.BASE64Encoder;
+
+import java.io.*;
+import java.math.BigInteger;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.logging.Logger;
+import java.util.zip.CRC32;
/**
+ * 文件工具类
+ *
* @author maxf:yexue
* @className FileUtil
* @description 工具类
* @time 2017年11月3日 下午3:12:49
*/
public class FileUtil {
+ static Logger logger = Logger.getLogger(FileUtil.class.getName());
+
private FileUtil() {
}
private static final String TYPE_TAR_GZ = ".tar.gz";
private static final String TAR_GZ = "tar.gz";
+ /**
+ * 获取文件类型:不适合所有
+ *
+ * 根据文件名称截取.后的文件格式
+ *
+ *
+ * @param fileName
+ * @return
+ */
public static String getFileType(String fileName) {
- if (fileName.lastIndexOf(TYPE_TAR_GZ) > 0) {
- return TAR_GZ;
+ try {
+ if (fileName.lastIndexOf(TYPE_TAR_GZ) > 0) {
+ return TAR_GZ;
+ }
+ return fileName.substring(fileName.lastIndexOf(".") + 1);
+ } catch (Exception e) {
+ logger.severe("file doesn't exist or is not a file");
}
- return fileName.substring(fileName.lastIndexOf(".") + 1);
+ return null;
}
- // 判断文件是否存在
+ /**
+ * 判断文件是否存在
+ *
+ * @param file
+ */
public static void judeFileExists(File file) {
if (file.exists()) {
- System.out.println("file exists");
+ logger.severe("file exists");
} else {
- System.out.println("file not exists, create it ...");
+ logger.info("file not exists, create it ...");
try {
file.createNewFile();
} catch (IOException e) {
+ logger.severe("file create fail");
e.printStackTrace();
}
}
@@ -49,20 +80,338 @@ public class FileUtil {
* 4、File类的createTempFile方法创建临时文件,可以制定临时文件的文件名前缀、后缀及文件所在的目录,如果不指定目录,则存放在系统的临时文件夹下。
* 5、除mkdirs方法外,以上方法在创建文件和目录时,必须保证目标文件不存在,而且父目录存在,否则会创建失败
*
+ *
+ * @return 创建成功、失败
*/
- public static void judeDirExists(File file) {
-
+ public static boolean judeDirExists(File file) {
if (file.exists()) {
if (file.isDirectory()) {
- System.out.println("dir exists");
+ logger.severe("dir exists");
} else {
- System.out.println("the same name file exists, can not create dir");
+ logger.severe("the same name file exists, can not create dir");
}
} else {
- System.out.println("dir not exists, create it ...");
- file.mkdirs();
+ logger.info("dir not exists, create it ...");
+ return file.mkdirs();
}
-
+ return false;
}
+ /**
+ * 获取文件sha1
+ *
+ * @param file
+ * @return
+ */
+ public static String sha1(File file) {
+ FileInputStream in = null;
+ try {
+ in = new FileInputStream(file);
+ MessageDigest digest = MessageDigest.getInstance("SHA-1");
+ byte[] buffer = new byte[1024 * 1024 * 10];
+
+ int len = 0;
+ while ((len = in.read(buffer)) > 0) {
+ digest.update(buffer, 0, len);
+ }
+ String sha1 = new BigInteger(1, digest.digest()).toString(16);
+ int length = 40 - sha1.length();
+ if (length > 0) {
+ for (int i = 0; i < length; i++) {
+ sha1 = "0" + sha1;
+ }
+ }
+ return sha1;
+ } catch (NoSuchAlgorithmException e) {
+ logger.severe("system algorithm error.");
+ e.printStackTrace();
+ } catch (FileNotFoundException e) {
+ logger.severe("file doesn't exist or is not a file");
+ e.printStackTrace();
+ } catch (IOException e) {
+ logger.severe("The operation file is an IO exception.");
+ e.printStackTrace();
+ } finally {
+ try {
+ if (in != null) {
+ in.close();
+ }
+ } catch (IOException e) {
+ logger.severe("close FileInputStream IO exception.");
+ }
+ }
+ return null;
+ }
+
+
+ /***
+ * 计算SHA1码
+ *
+ * @return String 适用于上G大的文件
+ * @throws NoSuchAlgorithmException
+ * */
+ public static String sha1ByBigFile(File file) {
+ MessageDigest messagedigest = null;
+ try {
+ messagedigest = MessageDigest.getInstance("SHA-1");
+ FileInputStream in = new FileInputStream(file);
+ FileChannel ch = in.getChannel();
+ MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
+ messagedigest.update(byteBuffer);
+ return StrUtil.toHex(messagedigest.digest());
+ } catch (NoSuchAlgorithmException e) {
+ logger.severe("system algorithm error.");
+ e.printStackTrace();
+ } catch (FileNotFoundException e) {
+ logger.severe("file doesn't exist or is not a file");
+ e.printStackTrace();
+ } catch (IOException e) {
+ logger.severe("The operation file is an IO exception.");
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ /**
+ * 文件md5
+ *
+ * @param file
+ * @return
+ */
+ public static String md5(File file) {
+ FileInputStream in = null;
+ try {
+ in = new FileInputStream(file);
+ MessageDigest digest = MessageDigest.getInstance("MD5");
+ byte[] buffer = new byte[1024 * 1024 * 10];
+
+ int len = 0;
+ while ((len = in.read(buffer)) > 0) {
+ digest.update(buffer, 0, len);
+ }
+ String md5 = new BigInteger(1, digest.digest()).toString(16);
+ int length = 32 - md5.length();
+ if (length > 0) {
+ for (int i = 0; i < length; i++) {
+ md5 = "0" + md5;
+ }
+ }
+ return md5;
+ } catch (IOException e) {
+ logger.severe("The operation file is an IO exception.");
+ } catch (NoSuchAlgorithmException e) {
+ logger.severe("system algorithm error.");
+ } finally {
+ try {
+ if (in != null) {
+ in.close();
+ }
+ } catch (IOException e) {
+ logger.severe("close FileInputStream IO exception.");
+ }
+ }
+ return null;
+ }
+
+ /**
+ * 对一个文件获取md5值
+ *
+ * @return md5串
+ * @throws NoSuchAlgorithmException
+ */
+ public static String md5ByBigFile(File file) {
+
+ MessageDigest messagedigest = null;
+ try {
+ messagedigest = MessageDigest.getInstance("MD5");
+ FileInputStream in = new FileInputStream(file);
+ FileChannel ch = in.getChannel();
+ MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,
+ file.length());
+ messagedigest.update(byteBuffer);
+ return StrUtil.toHex(messagedigest.digest());
+ } catch (NoSuchAlgorithmException e) {
+ logger.severe("system algorithm error.");
+ e.printStackTrace();
+ } catch (FileNotFoundException e) {
+ logger.severe("file doesn't exist or is not a file");
+ e.printStackTrace();
+ } catch (IOException e) {
+ logger.severe("The operation file is an IO exception.");
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ /**
+ * 获取文件CRC32码
+ *
+ * @return String
+ */
+ public static String crc32(File file) {
+ CRC32 crc32 = new CRC32();
+ // MessageDigest.get
+ FileInputStream fileInputStream = null;
+ try {
+ fileInputStream = new FileInputStream(file);
+ byte[] buffer = new byte[8192];
+ int length;
+ while ((length = fileInputStream.read(buffer)) != -1) {
+ crc32.update(buffer, 0, length);
+ }
+ return crc32.getValue() + "";
+ } catch (FileNotFoundException e) {
+ logger.severe("file doesn't exist or is not a file");
+ e.printStackTrace();
+ return null;
+ } catch (IOException e) {
+ logger.severe("The operation file is an IO exception.");
+ e.printStackTrace();
+ return null;
+ } finally {
+ try {
+ if (fileInputStream != null) {
+ fileInputStream.close();
+ }
+ } catch (IOException e) {
+ logger.severe("close FileInputStream IO exception.");
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * 获取文件base64
+ *
+ * @param file
+ * @return
+ */
+ public static String base64(File file) {
+ FileInputStream fileInputStream = null;
+ byte[] data = null;
+ // 读取图片字节数组
+ try {
+ fileInputStream = new FileInputStream(file);
+ data = new byte[fileInputStream.available()];
+ fileInputStream.read(data);
+ fileInputStream.close();
+ } catch (IOException e) {
+ logger.severe("The operation file is an IO exception.");
+ e.printStackTrace();
+ }
+ // 对字节数组Base64编码
+ BASE64Encoder encoder = new BASE64Encoder();
+ return encoder.encode(data);
+ }
+
+ /**
+ * base64转文件
+ *
+ *
+ * 文件转base64请使用 {@link FileUtil#base64(File)}
+ *
+ *
+ * @param decode baseByte
+ * @param fileName 文件名称(包含路径)
+ * @return 返回保存地址
+ */
+ public static String base64ToFile(byte[] decode, String fileName) {
+
+ FileOutputStream out = null;
+ try {
+ out = new FileOutputStream(fileName);
+ out.write(decode);
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ } finally {
+ try {
+ if (out != null) {
+ out.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ return fileName;
+ }
+
+ /**
+ * 获取文件大小 :直接返回大小
+ *
+ * @param f
+ * @return f.length()
+ */
+ public static long size(File f) {
+ if (f.exists() && f.isFile()) {
+ return f.length();
+ } else {
+ logger.info("file doesn't exist or is not a file");
+ }
+ return 0;
+ }
+
+ /**
+ * 获取文件大小 : 用流的方式获取
+ *
+ * @param f
+ * @return
+ */
+ public static long size4Stream(File f) {
+ FileChannel fc = null;
+ try {
+ if (f.exists() && f.isFile()) {
+ FileInputStream fis = new FileInputStream(f);
+ fc = fis.getChannel();
+ return fc.size();
+ } else {
+ logger.info("file doesn't exist or is not a file");
+ }
+ } catch (FileNotFoundException e) {
+ logger.severe("file doesn't exist or is not a file");
+ } catch (IOException e) {
+ logger.severe("The operation file is an IO exception.");
+ } finally {
+ if (null != fc) {
+ try {
+ fc.close();
+ } catch (IOException e) {
+ logger.severe("close FileInputStream IO exception.");
+ }
+ }
+ }
+ return 0;
+ }
+
+ /*public static void main(String[] args) {
+ long size = FileUtil.size(new File("E:\\OS\\deepin-15.6-amd64\\DeepinCloudPrintServerInstaller_1.0.0.1.exe"));
+ System.out.println(size);
+ System.out.println(1024 * 1024 * 5);
+ if (size > 1024 * 1024 * 5) {
+ System.out.println("文件最大5M");
+ return;
+ }
+
+ long s1 = fileSize(new File("E:\\OS\\cn_windows_10_consumer_editions_version_1803_updated_march_2018_x64_dvd_12063766.iso"));
+ System.out.println(s1);
+ long s2 = fileSize4Stream(new File("E:\\OS\\cn_windows_10_consumer_editions_version_1803_updated_march_2018_x64_dvd_12063766.iso"));
+ System.out.println(s2);
+
+ String s1 = base64(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
+ System.out.println(s1);
+
+ String s = sha1(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
+ String s2 = sha1ByBigFile(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
+ System.out.println(s);
+ System.out.println(s2);
+
+
+ String md5 = md5(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
+ String md52 = md5ByBigFile(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
+ System.out.println(md5);
+ System.out.println(md52);
+
+
+ String crc32 = crc32(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
+ System.out.println(crc32);
+ }*/
}
diff --git a/src/main/java/com/yexuejc/base/util/IdcardValidator.java b/src/main/java/com/yexuejc/base/util/IdcardValidator.java
index a13a6eb..816ec9b 100644
--- a/src/main/java/com/yexuejc/base/util/IdcardValidator.java
+++ b/src/main/java/com/yexuejc/base/util/IdcardValidator.java
@@ -47,7 +47,7 @@ public class IdcardValidator {
/**
* 每位加权因子
*/
- private static int power[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
+ private static int[] power = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
/**
* 验证所有的身份证的合法性
@@ -167,9 +167,9 @@ public class IdcardValidator {
// 获取第18位
String idcard18Code = idcard.substring(17, 18);
- char c[] = idcard17.toCharArray();
+ char[] c = idcard17.toCharArray();
- int bit[] = converCharToInt(c);
+ int[] bit = converCharToInt(c);
int sum17 = 0;
@@ -290,11 +290,11 @@ public class IdcardValidator {
String idcard17 = idcard.substring(0, 6) + year + idcard.substring(8);
- char c[] = idcard17.toCharArray();
+ char[] c = idcard17.toCharArray();
String checkCode = "";
// 将字符数组转为整型数组
- int bit[] = converCharToInt(c);
+ int[] bit = converCharToInt(c);
int sum17 = 0;
sum17 = getPowerSum(bit);
diff --git a/src/main/java/com/yexuejc/base/util/ImgUtil.java b/src/main/java/com/yexuejc/base/util/ImgUtil.java
new file mode 100644
index 0000000..7aa3dbc
--- /dev/null
+++ b/src/main/java/com/yexuejc/base/util/ImgUtil.java
@@ -0,0 +1,429 @@
+package com.yexuejc.base.util;
+
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.Proxy;
+import java.net.URL;
+import java.util.Iterator;
+
+/**
+ * 图片处理工具类
+ *
+ * @author maxf
+ * @ClassName ImageUtil
+ * @Description
+ * @date 2018/9/3 15:25
+ */
+public class ImgUtil {
+ private ImgUtil() {
+ }
+
+ /**
+ * 将一张网络图片转化成Base64字符串
+ *
+ * @param imgURL 网络图片url
+ * @return String Base64字符串
+ */
+ public static String getImageStrFromUrl(String imgURL) {
+ ByteArrayOutputStream data = new ByteArrayOutputStream();
+ try {
+ byte[] by = new byte[1024];
+ InputStream is = getImageInputStreamFromUrl(null, imgURL);
+ // 将内容读取内存中
+ int len = -1;
+ while ((len = is.read(by)) != -1) {
+ data.write(by, 0, len);
+ }
+ // 关闭流
+ is.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ // 对字节数组Base64编码
+ BASE64Encoder encoder = new BASE64Encoder();
+ return encoder.encode(data.toByteArray());
+ }
+
+ /**
+ * 读取网络图片url为输入流
+ *
本方法未关闭io流,请自行关闭io流
+ * is.close();
+ *
+ * @param imgURL 网络图片url
+ * @return InputStream 输入流
+ * @throws IOException
+ */
+ public static InputStream getImageInputStreamFromUrl(Proxy proxy, String imgURL) throws IOException {
+ // 创建URL
+ URL url = new URL(imgURL);
+ // 创建链接
+ HttpURLConnection conn;
+ if (null == proxy) {
+ conn = (HttpURLConnection) url.openConnection();
+ } else {
+ // 如果有代理则通过代理下载
+ conn = (HttpURLConnection) url.openConnection(proxy);
+ }
+ conn.setRequestMethod("GET");
+ conn.setConnectTimeout(5000);
+ InputStream is = conn.getInputStream();
+ return is;
+ }
+
+ /**
+ * 读取网络图片url为输入流
+ * 本方法未关闭io流,请自行关闭io流
+ * is.close();
+ *
+ * @param imgURL
+ * @return
+ * @throws IOException
+ */
+ public static InputStream getImageInputStreamFromUrl(String imgURL) throws IOException {
+ return getImageInputStreamFromUrl(null, imgURL);
+ }
+
+
+ /**
+ * 将图片文件流转换成字节数组
+ *
+ * 好处就是字节数组可以多次利用,而流一旦读取过一次之后就不能再使用了
+ *
+ * @param proxy
+ * @param targetUrl
+ * @return
+ * @throws MalformedURLException
+ * @throws IOException
+ */
+ public static byte[] getByteArrayFromInputStream(Proxy proxy, String targetUrl)
+ throws MalformedURLException, IOException {
+ InputStream is = getImageInputStreamFromUrl(proxy, targetUrl);
+ return getByteArray(is);
+ }
+
+ /**
+ * 将图片文件流转换成字节数组
+ *
+ * 好处就是字节数组可以多次利用,而流一旦读取过一次之后就不能再使用了
+ *
+ * @param targetUrl
+ * @return
+ * @throws MalformedURLException
+ * @throws IOException
+ */
+ public static byte[] getByteArrayFromInputStream(String targetUrl)
+ throws MalformedURLException, IOException {
+ return getByteArrayFromInputStream(null, targetUrl);
+ }
+
+ /**
+ * 将图片文件流转换成字节数组
+ *
+ * @return
+ * @throws MalformedURLException
+ * @throws IOException
+ */
+ public static byte[] getByteArray(InputStream is)
+ throws MalformedURLException, IOException {
+ // 把文件写到字节数组保存起来
+ ByteArrayOutputStream fos = getByteArrayFromInputStream(is);
+ byte[] bytes = fos.toByteArray();
+ fos.close();
+ return bytes;
+ }
+
+ /**
+ * ByteArrayOutputStream转换成字节数组
+ *
+ * @param fos
+ * @return
+ * @throws MalformedURLException
+ * @throws IOException
+ */
+ public static byte[] getByteArray(ByteArrayOutputStream fos)
+ throws MalformedURLException, IOException {
+ // 把文件写到字节数组保存起来
+ byte[] bytes = fos.toByteArray();
+ fos.close();
+ return bytes;
+ }
+
+ /**
+ * 将图片文件流转换成ByteArrayOutputStream
+ *
本方法未关闭io流,请自行关闭io流
+ * is.close();
+ *
+ * @param is
+ * @return
+ * @throws IOException
+ */
+ public static ByteArrayOutputStream getByteArrayFromInputStream(InputStream is) throws IOException {
+ // 把文件写到字节数组保存起来
+ BufferedInputStream bis = new BufferedInputStream(is);
+ ByteArrayOutputStream fos = new ByteArrayOutputStream();
+ byte[] buffer = new byte[1024];
+ int len = 0;
+ while ((len = bis.read(buffer)) != -1) {
+ fos.write(buffer, 0, len);
+ }
+ bis.close();
+ return fos;
+ }
+
+ /**
+ * 如果你已经将图片文件流InputStream读取出来放到一个字节数组
+ *
+ * 那么根据这个字节数组也是可以转换成对应的图片流,并再次获取图片基本信息
+ *
+ * @param imgBytes
+ * @return
+ */
+ public static ImageInfo getImageInfoFromInputStream(byte[] imgBytes) {
+ ByteArrayInputStream in = new ByteArrayInputStream(imgBytes);
+ ImageInfo image = getImageInfoFromInputStream(in);
+ return image;
+ }
+
+ /**
+ * 从图片文件流读取图片文件的基本信息
+ *
+ * @param inputStream
+ * @return
+ */
+ public static ImageInfo getImageInfoFromInputStream(InputStream inputStream) {
+ ImageInputStream imgStream = null;
+ try {
+ // 创建Image流
+ imgStream = ImageIO.createImageInputStream(inputStream);
+ Iterator iter = ImageIO.getImageReaders(imgStream);
+ if (!iter.hasNext()) {
+ return null;
+ }
+ // 读取流中一帧就可以获取到高宽以及各式
+ ImageReader reader = iter.next();
+ reader.setInput(imgStream, true);
+ int width = reader.getWidth(0);
+ int height = reader.getHeight(0);
+ String type = reader.getFormatName();
+ ImageInfo bean = new ImageInfo();
+ bean.setWidth(width);
+ bean.setHeight(height);
+ bean.setType(type);
+ return bean;
+ } catch (IOException e) {
+ e.printStackTrace();
+ return null;
+ } finally {
+ try {
+ imgStream.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+
+ /**
+ * 将byte数组以Base64方式编码为字符串
+ *
+ * @param bytes 待编码的byte数组
+ * @return String 编码后的字符串
+ */
+ public static String encode(byte[] bytes) {
+ return new BASE64Encoder().encode(bytes);
+ }
+
+ /**
+ * 将以Base64方式编码的字符串解码为byte数组
+ *
+ * @param encodeStr 待解码的字符串
+ * @return byte[] 解码后的byte数组
+ * @throws IOException
+ */
+ public static byte[] decode(String encodeStr) throws IOException {
+ byte[] bt = null;
+ BASE64Decoder decoder = new BASE64Decoder();
+ bt = decoder.decodeBuffer(encodeStr);
+ return bt;
+ }
+
+ /**
+ * 将两个byte数组连接起来后,返回连接后的Byte数组
+ *
+ * @param front 拼接后在前面的数组
+ * @param after 拼接后在后面的数组
+ * @return byte[] 拼接后的数组
+ */
+ public static byte[] connectBytes(byte[] front, byte[] after) {
+ byte[] result = new byte[front.length + after.length];
+ System.arraycopy(front, 0, result, 0, after.length);
+ System.arraycopy(after, 0, result, front.length, after.length);
+ return result;
+ }
+
+ /**
+ * 将流保存至本机或者图片服务器
+ *
+ * @param in 图片流
+ * @param saveUrl 目标地址
+ * @throws IOException
+ */
+ public static void saveImgFromInputStream(InputStream in, String saveUrl) throws IOException {
+ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveUrl));
+ byte[] buffer = new byte[1024];
+ int len = 0;
+ while ((len = in.read(buffer)) != -1) {
+ bos.write(buffer, 0, len);
+ }
+ bos.close();
+ in.close();
+ }
+
+
+ /**
+ * 将图片以Base64方式编码为字符串
+ *
+ * @param imgUrl 图片的绝对路径(例如:D:\\1.jpg)
+ * @return String 编码后的字符串
+ * @throws IOException
+ */
+ public static String encodeImage(String imgUrl) throws IOException {
+ FileInputStream fis = new FileInputStream(imgUrl);
+ byte[] rs = new byte[fis.available()];
+ fis.read(rs);
+ fis.close();
+ return encode(rs);
+ }
+
+ /**
+ * 获取图片返回bate[]
+ *
+ * @param imgUrl 图片的绝对路径(例如:D:\\1.jpg)
+ * @return byte[]
+ * @throws IOException
+ */
+ public static byte[] byteImage(String imgUrl) throws IOException {
+ byte[] rs;
+ FileInputStream fis = null;
+ try {
+ fis = new FileInputStream(imgUrl);
+ rs = new byte[fis.available()];
+ } finally {
+ if (fis != null) {
+ fis.close();
+ }
+ }
+ return rs;
+ }
+
+ public static class ImageInfo {
+ /**
+ * 图片大小
+ */
+ private long size = 0;
+ /**
+ * 图片宽度
+ */
+ private int width = 0;
+ /**
+ * 图片高度
+ */
+ private int height = 0;
+ /**
+ * 图片类型
+ */
+ private String type = "jpg";
+ /**
+ * 符号要求的图片类型
+ */
+ private String[] validTypes;
+
+ /**
+ * 定义一个简单方法描述该图片是否符合要求
+ *
+ * @param validImg
+ * @return
+ */
+ public boolean isValidImg(ImageInfo validImg) {
+ if (null == validImg) {
+ return true;
+ }
+ return (this.getSize() <= validImg.getSize()) && (this.getWidth() <= validImg.getWidth())
+ && (this.getHeight() <= validImg.getHeight()) && isValidType(validImg);
+
+ }
+
+ private boolean isValidType(ImageInfo validImg) {
+ if (null == validImg.getValidTypes()) {
+ return true;
+ }
+ boolean isValid = false;
+ for (String validType : validImg.getValidTypes()) {
+ if (type.equalsIgnoreCase(validType)) {
+ isValid = true;
+ break;
+ }
+ }
+ return isValid;
+ }
+
+ private String validTypeToString() {
+ if (null == validTypes) {
+ return "";
+ }
+ StringBuffer sb = new StringBuffer("[");
+ for (String type : validTypes) {
+ sb.append(type).append(" ");
+ }
+ sb.append("]");
+ return sb.toString();
+ }
+
+ public long getSize() {
+ return size;
+ }
+
+ public void setSize(long size) {
+ this.size = size;
+ }
+
+ public int getWidth() {
+ return width;
+ }
+
+ public void setWidth(int width) {
+ this.width = width;
+ }
+
+ public int getHeight() {
+ return height;
+ }
+
+ public void setHeight(int height) {
+ this.height = height;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public String[] getValidTypes() {
+ return validTypes;
+ }
+
+ public void setValidTypes(String[] validTypes) {
+ this.validTypes = validTypes;
+ }
+ }
+}
diff --git a/src/main/java/com/yexuejc/base/util/JsonUtil.java b/src/main/java/com/yexuejc/base/util/JsonUtil.java
index 9b650da..f92a315 100644
--- a/src/main/java/com/yexuejc/base/util/JsonUtil.java
+++ b/src/main/java/com/yexuejc/base/util/JsonUtil.java
@@ -5,11 +5,24 @@ import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.type.MapType;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Map;
+import java.util.logging.Logger;
+/**
+ * json工具类,基于jackson
+ *
+ * @author maxf
+ * @ClassName JsonUtil
+ * @Description
+ * @date 2018/9/3 15:28
+ */
public class JsonUtil {
+ private static Logger log = Logger.getLogger(JsonUtil.class.getName());
+
private JsonUtil() {
}
@@ -60,8 +73,11 @@ public class JsonUtil {
try {
pojo = objectMapper.readValue(json, cls);
} catch (JsonParseException e) {
+ log.warning("json to Object JsonParseException.\n" + e.getMessage());
} catch (JsonMappingException e) {
+ log.warning("json to Object JsonMappingException.\n" + e.getMessage());
} catch (IOException e) {
+ log.warning("json to Object IOException.\n" + e.getMessage());
}
return pojo;
@@ -80,19 +96,23 @@ public class JsonUtil {
try {
pojo = objectMapper.readValue(json, cls);
} catch (JsonParseException e) {
+ log.warning("json to Object JsonParseException.\n" + e.getMessage());
} catch (JsonMappingException e) {
+ log.warning("json to Object JsonMappingException.\n" + e.getMessage());
} catch (IOException e) {
+ log.warning("json to Object IOException.\n" + e.getMessage());
}
return pojo;
}
+
/**
* Json字符串转换为Java对象
*
- * @param json
- * @param parametrized
- * @param parameterClasses
+ * @param json 字符串
+ * @param parametrized 容器类
+ * @param parameterClasses 实际类
* @return
*/
public static T json2Obj(String json, Class parametrized, Class>... parameterClasses) {
@@ -102,8 +122,61 @@ public class JsonUtil {
try {
pojo = objectMapper.readValue(json, javaType);
} catch (JsonParseException e) {
+ log.warning("json to Object JsonParseException.\n" + e.getMessage());
} catch (JsonMappingException e) {
+ log.warning("json to Object JsonMappingException.\n" + e.getMessage());
} catch (IOException e) {
+ log.warning("json to Object IOException.\n" + e.getMessage());
+ }
+ return pojo;
+ }
+
+ /**
+ * Json字符串转换为Java-Map对象
+ *
+ * @param json 字符串
+ * @param mapClass Map 继承类
+ * @param keyClass Key 类
+ * @param valueClass Value 类
+ * @param
+ * @return
+ */
+ public static T json2Obj(String json, Class extends Map> mapClass, Class> keyClass, Class> valueClass) {
+ T pojo = null;
+ MapType mapType = objectMapper.getTypeFactory().constructMapType(mapClass, keyClass, valueClass);
+ try {
+ pojo = objectMapper.readValue(json, mapType);
+ } catch (JsonParseException e) {
+ log.warning("json to Object JsonParseException.\n" + e.getMessage());
+ } catch (JsonMappingException e) {
+ log.warning("json to Object JsonMappingException.\n" + e.getMessage());
+ } catch (IOException e) {
+ log.warning("json to Object IOException.\n" + e.getMessage());
+ }
+ return pojo;
+ }
+
+ /**
+ * Json字符串转换为Java-Map对象
+ *
+ * @param json 字符串
+ * @param mapClass Map 继承类
+ * @param keyType Key 类
+ * @param valueType Value 类
+ * @param
+ * @return
+ */
+ public static T json2Obj(String json, Class extends Map> mapClass, JavaType keyType, JavaType valueType) {
+ T pojo = null;
+ MapType mapType = objectMapper.getTypeFactory().constructMapType(mapClass, keyType, valueType);
+ try {
+ pojo = objectMapper.readValue(json, mapType);
+ } catch (JsonParseException e) {
+ log.warning("json to Object JsonParseException.\n" + e.getMessage());
+ } catch (JsonMappingException e) {
+ log.warning("json to Object JsonMappingException.\n" + e.getMessage());
+ } catch (IOException e) {
+ log.warning("json to Object IOException.\n" + e.getMessage());
}
return pojo;
}
@@ -111,9 +184,9 @@ public class JsonUtil {
/**
* Json字符串转换为Java对象
*
- * @param json
- * @param parametrized
- * @param parameterClasses
+ * @param json 字符串
+ * @param parametrized 容器类
+ * @param parameterClasses 实际类
* @return
*/
public static T json2Obj(InputStream json, Class parametrized, Class>... parameterClasses) {
@@ -123,8 +196,11 @@ public class JsonUtil {
try {
pojo = objectMapper.readValue(json, javaType);
} catch (JsonParseException e) {
+ log.warning("json to Object JsonParseException.\n" + e.getMessage());
} catch (JsonMappingException e) {
+ log.warning("json to Object JsonMappingException.\n" + e.getMessage());
} catch (IOException e) {
+ log.warning("json to Object IOException.\n" + e.getMessage());
}
return pojo;
}
@@ -140,6 +216,7 @@ public class JsonUtil {
try {
json = objectMapper.writeValueAsString(pojo);
} catch (JsonProcessingException e) {
+ log.warning("json to Object JsonProcessingException.\n" + e.getMessage());
}
return json;
}
diff --git a/src/main/java/com/yexuejc/base/util/JwtUtil.java b/src/main/java/com/yexuejc/base/util/JwtUtil.java
index c855c61..fde70fb 100644
--- a/src/main/java/com/yexuejc/base/util/JwtUtil.java
+++ b/src/main/java/com/yexuejc/base/util/JwtUtil.java
@@ -6,21 +6,69 @@ import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.Map;
+/**
+ * jwt工具类
+ *
+ * 升级2.0
+ *
+ * 由静态类分装成单例类,可配置参数config()
+ *
+ *
+ * @author maxf
+ * @version 2.0
+ * @ClassName JwtUtil
+ * @Description
+ * @date 2018/9/3 15:28
+ */
public class JwtUtil {
- /** 加密用KEY */
- private static String JWT_SIGNATURE_KEY = "h%OG8Y3WgA5AN7&6Ke7I#C1XvneW0N8a";
- /** token类型 */
- private static String JWT_HEADER_TYP = "JWT";
- /** token发行商 */
- private static String JWT_CLAIMS_ISS = "yexuejc.com";
+
+ private JwtUtil() {
+ }
+
+ public static JwtUtil instace() {
+ return Instace.jwtUtil;
+ }
+
+ /**
+ * 参数配置:设置一次即可,多次设置会覆盖之前的
+ *
+ * @param key 加密key 默认:h%OG8Y3WgA5AN7&6Ke7I#C1XvneW0N8a
+ * @param type 加密类型:默认JWT
+ * @param iss token发行商: 默认yexuejc.com
+ * @return
+ */
+ public static JwtUtil config(String key, String type, String iss) {
+ JwtUtil jwtUtil = instace();
+ jwtUtil.JWT_SIGNATURE_KEY = key;
+ jwtUtil.JWT_HEADER_TYP = type;
+ jwtUtil.JWT_CLAIMS_ISS = iss;
+ return jwtUtil;
+ }
+
+ public static class Instace {
+ private static JwtUtil jwtUtil = new JwtUtil();
+ }
+
+ /**
+ * 加密用KEY
+ */
+ private String JWT_SIGNATURE_KEY = "h%OG8Y3WgA5AN7&6Ke7I#C1XvneW0N8a";
+ /**
+ * token类型
+ */
+ private String JWT_HEADER_TYP = "JWT";
+ /**
+ * token发行商
+ */
+ private String JWT_CLAIMS_ISS = "yexuejc.com";
/**
* 加密内容生成token
- *
+ *
* @param subjectObj
* @return
*/
- public static String compact(Object subjectObj) {
+ public String compact(Object subjectObj) {
String subject = null;
if (subjectObj instanceof String) {
subject = (String) subjectObj;
@@ -48,22 +96,22 @@ public class JwtUtil {
/**
* 解密token为一个Map
- *
+ *
* @param token
* @return
*/
- public static Map, ?> parse(String token) {
+ public Map, ?> parse(String token) {
return parse(token, Map.class);
}
/**
* 解密token为一个指定对象
- *
+ *
* @param token
* @param cls
* @return
*/
- public static T parse(String token, Class cls) {
+ public T parse(String token, Class cls) {
String subject = null;
try {
subject = Jwts.parser().setSigningKey(JWT_SIGNATURE_KEY).parseClaimsJws(token).getBody().getSubject();
diff --git a/src/main/java/com/yexuejc/base/util/MapRemoveNullUtil.java b/src/main/java/com/yexuejc/base/util/MapRemoveNullUtil.java
index 1442a8f..f71bf32 100644
--- a/src/main/java/com/yexuejc/base/util/MapRemoveNullUtil.java
+++ b/src/main/java/com/yexuejc/base/util/MapRemoveNullUtil.java
@@ -3,8 +3,15 @@ package com.yexuejc.base.util;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
-import java.util.Set;
+/**
+ * map相关工具
+ *
+ * @author maxf
+ * @ClassName MapRemoveNullUtil
+ * @Description
+ * @date 2018/9/3 15:32
+ */
public class MapRemoveNullUtil {
private MapRemoveNullUtil() {
}
@@ -28,10 +35,11 @@ public class MapRemoveNullUtil {
* @return
*/
public static Map removeNullKey(Map map) {
- Set set = map.keySet();
- for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
- Object obj = (Object) iterator.next();
- remove(obj, iterator);
+ for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
+ Map.Entry item = (Map.Entry) it.next();
+ if (StrUtil.isEmpty(item.getKey())) {
+ it.remove();
+ }
}
return map;
}
@@ -43,11 +51,11 @@ public class MapRemoveNullUtil {
* @return
*/
public static Map removeNullValue(Map map) {
- Set set = map.keySet();
- for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
- Object obj = (Object) iterator.next();
- Object value = (Object) map.get(obj);
- remove(value, iterator);
+ for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
+ Map.Entry item = (Map.Entry) it.next();
+ if (StrUtil.isEmpty(item.getValue())) {
+ it.remove();
+ }
}
return map;
}
diff --git a/src/main/java/com/yexuejc/base/util/MoneyUtils.java b/src/main/java/com/yexuejc/base/util/MoneyUtil.java
similarity index 53%
rename from src/main/java/com/yexuejc/base/util/MoneyUtils.java
rename to src/main/java/com/yexuejc/base/util/MoneyUtil.java
index 646dd80..8222c15 100644
--- a/src/main/java/com/yexuejc/base/util/MoneyUtils.java
+++ b/src/main/java/com/yexuejc/base/util/MoneyUtil.java
@@ -4,15 +4,15 @@ import java.math.BigDecimal;
import java.text.DecimalFormat;
/**
- * q钱相关工具类
+ * 钱相关工具类
*
* @ClassName: MoneyUtils
* @Description:
* @author: maxf
* @date: 2017年12月1日 下午5:33:57
*/
-public class MoneyUtils {
- private MoneyUtils() {
+public class MoneyUtil {
+ private MoneyUtil() {
}
/**
@@ -34,6 +34,25 @@ public class MoneyUtils {
return str;
}
+ /**
+ * 分转元
+ *
+ * @param num
+ * @return String
+ * @Title: formatPrice
+ * @Description:分转元
+ * @throw
+ */
+ public static String toYuan(Long num) {
+ if (num == null) {
+ return "0.00";
+ }
+ DecimalFormat df = new DecimalFormat("0.00");
+ BigDecimal bigDecimal = new BigDecimal(num).divide(new BigDecimal(100));
+ String str = df.format(bigDecimal);
+ return str;
+ }
+
/**
* 元转分
*
@@ -52,4 +71,23 @@ public class MoneyUtils {
String str = df.format(bigDecimal);
return Integer.parseInt(str);
}
+
+ /**
+ * 元转分
+ *
+ * @param num
+ * @return int
+ * @Title: formatPrice
+ * @Description: 元转分
+ * @throw
+ */
+ public static Long toFen4Long(String num) {
+ if (num == null) {
+ return 0L;
+ }
+ DecimalFormat df = new DecimalFormat("#0");
+ BigDecimal bigDecimal = new BigDecimal(num).multiply(new BigDecimal(100));
+ String str = df.format(bigDecimal);
+ return Long.parseLong(str);
+ }
}
diff --git a/src/main/java/com/yexuejc/base/util/ObjUtil.java b/src/main/java/com/yexuejc/base/util/ObjUtil.java
new file mode 100644
index 0000000..c2f3bee
--- /dev/null
+++ b/src/main/java/com/yexuejc/base/util/ObjUtil.java
@@ -0,0 +1,296 @@
+package com.yexuejc.base.util;
+
+import java.io.*;
+import java.lang.reflect.Field;
+import java.util.*;
+
+/**
+ * 对象工具:对类的操作
+ *
+ * @author maxf
+ * @version 1.0
+ * @ClassName ObjUtil
+ * @Description
+ * @date 2018/12/28 15:54
+ */
+public class ObjUtil {
+ private ObjUtil() {
+ }
+
+ /**
+ * 把对象按照{@link ToUeProperty}注解转换
+ * 字段值为空不输出
+ *
+ * @param obj 要转换的对象
+ * @return 转换后的值以Map返回
+ */
+ public static Map getToUeMap(Object obj) {
+ return getUnderlineMap(obj, true, false);
+ }
+
+ /**
+ * 把对象按照注解转换
+ * 字段值为空不输出
+ *
+ * @param obj 要转换的对象
+ * @return 转换后的值toJson以String返回
+ */
+ public static String getToUeStr(Object obj) {
+ return JsonUtil.obj2Json(getUnderlineMap(obj, true, false));
+ }
+
+ /**
+ * 把对象驼峰字段转换成下划线输出,支持继承和字段类型为对象
+ * 字段值为空不输出
+ *
+ * @param obj 要转换的对象
+ * @return 转换后的值以Map返回
+ */
+ public static Map getUnderlineMap(Object obj) {
+ return getUnderlineMap(obj, true, false);
+ }
+
+ /**
+ * 把对象驼峰字段转换成下划线输出,支持继承和字段类型为对象
+ * 字段值为空不输出
+ *
+ * @param obj 要转换的对象
+ * @return 转换后的值toJson以String返回
+ */
+ public static String getUnderlineStr(Object obj) {
+ return JsonUtil.obj2Json(getUnderlineMap(obj, true, false));
+ }
+
+ /**
+ * 把对象字段按{@link ToUeProperty}注解规则转换输出,支持继承和字段类型为对象
+ *
+ * @param obj 要转换的对象
+ * @param isAnnotationAll 是否全部依赖注解转换。全部依赖注解转换:true 只有字段上有注解的才转换,没有注解的默认不转换;false 有注解的依照注解转换,没有的全传下划线
+ * @param putNull 是否映射null
+ * @return 转换后的值toJson以String返回
+ */
+ public static String getUnderlineStr(Object obj, boolean isAnnotationAll, boolean putNull) {
+ return JsonUtil.obj2Json(getUnderlineMap(obj, isAnnotationAll, putNull));
+ }
+
+ /**
+ * 把对象字段按{@link ToUeProperty}注解规则转换成输出,支持继承和字段类型为对象
+ * 主要功能:解决输出时驼峰-下划线的转换
+ *
+ * @param obj 要转换的对象
+ * @param isAnnotationAll 是否全部依赖注解转换。全部依赖注解转换:true 只有字段上有注解的才转换,没有注解的默认不转换;false 有注解的依照注解转换,没有的全传下划线
+ * @param putNull 是否映射null
+ * @return
+ */
+ public static Map getUnderlineMap(Object obj, boolean isAnnotationAll, boolean putNull) {
+ Class> bindClass = obj.getClass();
+ Map objMap = new HashMap<>(0);
+ /*
+ * 得到类中的所有属性集合
+ */
+ try {
+ List fieldList = new ArrayList<>();
+ //当父类为null的时候说明到达了最上层的父类(Object类).
+ while (bindClass != null) {
+ fieldList.addAll(Arrays.asList(bindClass.getDeclaredFields()));
+ //得到父类,然后赋给自己
+ bindClass = bindClass.getSuperclass();
+ }
+ for (Field f : fieldList) {
+ //排除序列化
+ if ("serialVersionUID".equals(f.getName())) {
+ continue;
+ }
+ //设置些属性是可以访问的
+ f.setAccessible(true);
+ String fName = f.getName();
+ if (!isAnnotationAll) {
+ fName = StrUtil.camelToUnderline(f.getName());
+ }
+ boolean annotationPresent = f.isAnnotationPresent(ToUeProperty.class);
+ if (annotationPresent) {
+ ToUeProperty annotation = f.getAnnotation(ToUeProperty.class);
+ String value = annotation.value();
+ if (StrUtil.isNotEmpty(value)) {
+ fName = value;
+ }
+ }
+ Object o = f.get(obj);
+ if (null == o || o.getClass().isPrimitive() || o instanceof String || o instanceof Enum) {
+ if (null == o && !putNull) {
+ continue;
+ }
+ objMap.put(fName, f.get(obj));
+ } else if (o instanceof List) {
+ List list = (List) o;
+ List bodyList = new ArrayList();
+ list.forEach(it -> {
+ if (null != it) {
+ Map underlineMap = getUnderlineMap(it, isAnnotationAll, putNull);
+ bodyList.add(underlineMap);
+ }
+ });
+ if (bodyList.size() > 0) {
+ objMap.put(fName, bodyList);
+ }
+ } else {
+ Map underlineMap = getUnderlineMap(o, isAnnotationAll, putNull);
+ objMap.put(fName, underlineMap);
+ }
+ }
+ } catch (IllegalAccessException e) {
+ System.out.println(e.getMessage());
+ }
+ return objMap;
+ }
+
+
+ public static void main(String[] args) {
+ B a = new B();
+ a.nameFirst = "张三";
+ a.ageInt = "5165458";
+ a.testAss = "asdasdsad";
+ a.setaM1("method1");
+ a.setbM1("b1Mthod1");
+ a.protectedStr = "protectedStr";
+ C c = new C();
+ c.ageInt = "test";
+ a.c = c;
+ a.list = new ArrayList<>();
+ a.list.add(c);
+ Map underlineMap = getUnderlineMap(a, false, false);
+ System.out.println(JsonUtil.obj2Json(underlineMap));
+ }
+
+ static class A implements Serializable {
+ private static final long serialVersionUID = -8462118058721865488L;
+ public String nameFirst;
+ public String ageInt;
+ private String aM1;
+ @ToUeProperty("p_str")
+ protected String protectedStr;
+
+ public String getaM1() {
+ return aM1;
+ }
+
+ public A setaM1(String aM1) {
+ this.aM1 = aM1;
+ return this;
+ }
+ }
+
+ static class B extends A {
+ private static final long serialVersionUID = -8462118058721865488L;
+ public String testAss;
+ private String bM1;
+ private C c;
+ List list;
+
+ public String getbM1() {
+ return bM1;
+ }
+
+ public B setbM1(String bM1) {
+ this.bM1 = bM1;
+ return this;
+ }
+ }
+
+ static class C extends A {
+ private static final long serialVersionUID = -8462118058721865488L;
+ public String testAss;
+ private String bM1;
+
+ public String getbM1() {
+ return bM1;
+ }
+
+ public C setbM1(String bM1) {
+ this.bM1 = bM1;
+ return this;
+ }
+ }
+
+
+ /**
+ * 深度克隆对象
+ *
+ * 将该对象序列化成流,因为写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝
+ *
+ * 注:克隆对象必须序列化
+ *
+ * @param t
+ * @param
+ * @return
+ */
+ public static T depthClone(T t) {
+ T outer = null;
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ oos.writeObject(t);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+ ObjectInputStream ois = new ObjectInputStream(bais);
+ outer = (T) ois.readObject();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ }
+ return outer;
+ }
+
+// public static void main(String[] args) {
+//// test1();
+//// test2();
+//
+// }
+//
+// private static void test2() {
+// B t = new B();
+// t.sex = "男";
+// t.age = 18;
+// A test = new A();
+// test.name = "张三";
+// t.test = test;
+// B b = depthClone(t);
+// System.out.println(JsonUtil.obj2Json(b));
+// }
+//
+// static class A implements Serializable {
+// private static final long serialVersionUID = -8462118058721865488L;
+// public String name;
+// }
+//
+// static class B implements Serializable {
+// private static final long serialVersionUID = 3297717505428005316L;
+// public int age;
+// public String sex;
+// public A test;
+// }
+//
+// static class C implements Serializable {
+// private static final long serialVersionUID = 3297717505428005316L;
+// public int age;
+// public String sex;
+// public A test;
+// }
+//
+// private static void test1() {
+// ApiVO apiVO = new ApiVO(ApiVO.STATUS.S);
+// apiVO.setMsg("asdsadsad");
+// apiVO.setObject1("sadsadsad");
+//
+// Resps obj = new Resps<>();
+// obj.setSucc("安达圣斗士", "ok");
+// System.out.println(obj);
+// apiVO.setObject2(obj);
+// ApiVO apiVO1 = depthClone(apiVO);
+// System.out.println(apiVO == apiVO1);
+// System.out.println(JsonUtil.obj2Json(apiVO1));
+// System.out.println(JsonUtil.obj2Json(apiVO1.getObject1(String.class)));
+// System.out.println(JsonUtil.obj2Json(apiVO1.getObject2(Resps.class)));
+// System.out.println(apiVO1.getObject2(Resps.class) == obj);
+// }
+}
diff --git a/src/main/java/com/yexuejc/base/util/RegexUtils.java b/src/main/java/com/yexuejc/base/util/RegexUtil.java
similarity index 97%
rename from src/main/java/com/yexuejc/base/util/RegexUtils.java
rename to src/main/java/com/yexuejc/base/util/RegexUtil.java
index 137e1b8..5c7daa6 100644
--- a/src/main/java/com/yexuejc/base/util/RegexUtils.java
+++ b/src/main/java/com/yexuejc/base/util/RegexUtil.java
@@ -10,8 +10,8 @@ import java.util.regex.Pattern;
* @expl
* @time 2017年11月9日 上午11:01:24
*/
-public class RegexUtils {
- private RegexUtils() {
+public class RegexUtil {
+ private RegexUtil() {
}
/**
diff --git a/src/main/java/com/yexuejc/base/util/StrUtil.java b/src/main/java/com/yexuejc/base/util/StrUtil.java
index ab22cfe..55c07c2 100644
--- a/src/main/java/com/yexuejc/base/util/StrUtil.java
+++ b/src/main/java/com/yexuejc/base/util/StrUtil.java
@@ -16,6 +16,9 @@ import java.util.regex.Pattern;
* @date: 2018/5/12 19:13
*/
public final class StrUtil {
+ private StrUtil() {
+ }
+
public static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
@@ -123,7 +126,7 @@ public final class StrUtil {
return null;
}
- Map map = new HashMap();
+ Map map = new HashMap(16);
String[] kv = null;
for (String entry : entrys) {
if (isEmpty(entry)) {
@@ -148,7 +151,7 @@ public final class StrUtil {
* @param buf 初始字节数组
* @return 转换后字符串
*/
- public static String toHex(byte buf[]) {
+ public static String toHex(byte[] buf) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
@@ -178,7 +181,7 @@ public final class StrUtil {
return null;
}
md.update(str.getBytes());
- byte tmp[] = md.digest();
+ byte[] tmp = md.digest();
return toHex(tmp);
}
@@ -212,7 +215,7 @@ public final class StrUtil {
return null;
}
messageDigest.update(str.getBytes());
- byte tmp[] = messageDigest.digest();
+ byte[] tmp = messageDigest.digest();
return toHex(tmp);
}
@@ -238,8 +241,9 @@ public final class StrUtil {
* @param str
* @return
*/
+ private static Pattern pattern = Pattern.compile("[0-9]*");
+
public static boolean isNumeric(String str) {
- Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
@@ -259,16 +263,17 @@ public final class StrUtil {
}
StringBuilder coded = new StringBuilder();
+ Random random = new Random();
for (int i = 0; i < 13; i++) {
- coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
+ coded.append(HEX_CHAR[random.nextInt(16)]);
}
coded.append(id.substring(0, 11));
for (int i = 0; i < 7; i++) {
- coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
+ coded.append(HEX_CHAR[random.nextInt(16)]);
}
coded.append(id.substring(11));
for (int i = 0; i < 12; i++) {
- coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
+ coded.append(HEX_CHAR[random.nextInt(16)]);
}
return coded.toString();
@@ -335,7 +340,7 @@ public final class StrUtil {
* @return
*/
public static Map mapSort(Map sortedParams) {
- Map map = new HashMap<>();
+ Map map = new HashMap<>(16);
List keys = new ArrayList<>(sortedParams.keySet());
Collections.sort(keys);
int index = 0;
@@ -361,4 +366,57 @@ public final class StrUtil {
}
return msg;
}
+
+ /**
+ * 下划线字符
+ */
+ public static final char UNDERLINE = '_';
+
+ /**
+ * 字符串下划线转驼峰格式
+ *
+ * @param param 需要转换的字符串
+ * @return 转换好的字符串
+ */
+ public static String underlineToCamel(String param) {
+ if (isEmpty(param)) {
+ return "";
+ }
+ String temp = param.toLowerCase();
+ int len = temp.length();
+ StringBuilder sb = new StringBuilder(len);
+ for (int i = 0; i < len; i++) {
+ char c = temp.charAt(i);
+ if (c == UNDERLINE) {
+ if (++i < len) {
+ sb.append(Character.toUpperCase(temp.charAt(i)));
+ }
+ } else {
+ sb.append(c);
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
+ * 字符串驼峰转下划线格式
+ *
+ * @param param 需要转换的字符串
+ * @return 转换好的字符串
+ */
+ public static String camelToUnderline(String param) {
+ if (isEmpty(param)) {
+ return "";
+ }
+ int len = param.length();
+ StringBuilder sb = new StringBuilder(len);
+ for (int i = 0; i < len; i++) {
+ char c = param.charAt(i);
+ if (Character.isUpperCase(c) && i > 0) {
+ sb.append(UNDERLINE);
+ }
+ sb.append(Character.toLowerCase(c));
+ }
+ return sb.toString();
+ }
}
diff --git a/src/main/java/com/yexuejc/base/util/SysUtil.java b/src/main/java/com/yexuejc/base/util/SysUtil.java
new file mode 100644
index 0000000..4b0758f
--- /dev/null
+++ b/src/main/java/com/yexuejc/base/util/SysUtil.java
@@ -0,0 +1,82 @@
+package com.yexuejc.base.util;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+
+import java.net.URL;
+import java.util.concurrent.*;
+
+/**
+ * 系统工具类
+ *
+ * @PackageName: com.yexuejc.util.base
+ * @Description:
+ * @author: maxf
+ * @date: 2017/12/28 16:12
+ */
+public class SysUtil {
+ private static final String PROJECT_ROOT_PATH = "java.io.tmpdir";
+
+ private SysUtil() {
+ }
+
+ /**
+ * 获取临时缓存路径
+ *
+ * @return String
+ */
+ public static String getCachePath() {
+ return System.getProperty(PROJECT_ROOT_PATH);
+ }
+
+ /**
+ * 从根路径获取文件URL
+ *
+ * @param clazz
+ * @return
+ */
+ public static URL getRootPath(Class clazz, String filePath) {
+ return clazz.getClass().getResource(StrUtil.setStr(filePath, "/"));
+ }
+
+ /**
+ * 开启线程执行
+ * 异步处理代码
+ *
+ * @param threadRun
+ */
+ public static void threadRun(ThreadRun threadRun) {
+ threadRun(null, threadRun);
+ }
+
+ /**
+ * 异步处理代码
+ *
+ * @param poolName 开启线程名称
+ * @param threadRun
+ */
+ public static void threadRun(String poolName, ThreadRun threadRun) {
+ if (StrUtil.isEmpty(poolName)) {
+ poolName = "java-pool-%d";
+ }
+ ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
+ .setNameFormat(poolName).build();
+ ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,
+ 0L, TimeUnit.MILLISECONDS,
+ new LinkedBlockingQueue(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
+
+ singleThreadPool.execute(() -> {
+ threadRun.execute();
+ });
+ singleThreadPool.shutdown();
+ }
+
+ /**
+ * 异步执行接口
+ */
+ public interface ThreadRun {
+ /**
+ * 执行代码块
+ */
+ void execute();
+ }
+}
diff --git a/src/main/java/com/yexuejc/base/util/SysUtils.java b/src/main/java/com/yexuejc/base/util/SysUtils.java
deleted file mode 100644
index b992c2c..0000000
--- a/src/main/java/com/yexuejc/base/util/SysUtils.java
+++ /dev/null
@@ -1,38 +0,0 @@
-package com.yexuejc.base.util;
-
-import java.net.URL;
-
-/**
- * 系统工具类
- *
- * @PackageName: com.yexuejc.util.base
- * @Description:
- * @author: maxf
- * @date: 2017/12/28 16:12
- */
-public class SysUtils {
- private static final String PROJECT_ROOT_PATH = "java.io.tmpdir";
-
- private SysUtils() {
- }
-
- /**
- * 获取临时缓存路径
- *
- * @return String
- */
- public static String getCachePath() {
- return System.getProperty(PROJECT_ROOT_PATH);
- }
-
- /**
- * 从根路径获取文件URL
- *
- * @param clazz
- * @return
- */
- public static URL getRootPath(Class clazz, String filePath) {
- return clazz.getClass().getResource(StrUtil.setStr(filePath, "/"));
- }
-
-}
diff --git a/src/main/java/com/yexuejc/base/util/ThreeDES.java b/src/main/java/com/yexuejc/base/util/ThreeDES.java
new file mode 100644
index 0000000..ba8e630
--- /dev/null
+++ b/src/main/java/com/yexuejc/base/util/ThreeDES.java
@@ -0,0 +1,103 @@
+package com.yexuejc.base.util;
+
+import org.apache.commons.codec.binary.Base64;
+
+import javax.crypto.Cipher;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.DESedeKeySpec;
+import javax.crypto.spec.IvParameterSpec;
+import java.io.UnsupportedEncodingException;
+import java.security.InvalidKeyException;
+import java.security.Key;
+
+
+/**
+ * 3DES加解密
+ *
+ * @author maxf
+ * @ClassName ThreeDES
+ * @Description
+ * @date 2018/9/3 17:09
+ */
+public class ThreeDES {
+ private ThreeDES() {
+ }
+
+ public static String IV = "1234567-";
+ public static String ENCODING = "utf-8";
+
+ /**
+ * DESCBC加密
+ *
+ * @param src 数据源
+ * @param key 密钥
+ * @return 返回加密后的数据
+ * @throws Exception
+ */
+ public static String encryptDESCBC(final String src, final String key) throws Exception {
+ if (key.length() < 24) {
+ throw new InvalidKeyException("key的length不得小于24");
+ }
+ Key deskey = null;
+ DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());
+ SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
+ deskey = keyfactory.generateSecret(spec);
+
+ Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
+ IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
+ cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
+ byte[] encryptData = cipher.doFinal(src.getBytes(ENCODING));
+ return Base64.encodeBase64URLSafeString(encryptData);
+ }
+
+ /**
+ * DESCBC解密
+ *
+ * @param src 数据源
+ * @param key 密钥
+ * @return 返回解密后的原始数据
+ * @throws Exception
+ */
+ public static String decryptDESCBC(final String src, final String key) throws Exception {
+ if (key.length() < 24) {
+ throw new InvalidKeyException("key的length不得小于24");
+ }
+ Key deskey = null;
+ DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());
+ SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
+ deskey = keyfactory.generateSecret(spec);
+ Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
+ IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
+ cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
+
+ byte[] decryptData = cipher.doFinal(Base64.decodeBase64(src));
+
+ return new String(decryptData, ENCODING);
+ }
+
+ /**
+ * 填充,不是8的倍数会填充成8的倍数
+ *
+ * @param str
+ * @return
+ */
+ public static String padding(String str) {
+ byte[] oldByteArray;
+ try {
+ oldByteArray = str.getBytes("UTF8");
+ int numberToPad = 8 - oldByteArray.length % 8;
+ byte[] newByteArray = new byte[oldByteArray.length + numberToPad];
+ System.arraycopy(oldByteArray, 0, newByteArray, 0,
+ oldByteArray.length);
+ for (int i = oldByteArray.length; i < newByteArray.length; ++i) {
+ newByteArray[i] = 0;
+ }
+ return new String(newByteArray, "UTF8");
+ } catch (UnsupportedEncodingException e) {
+ System.out.println("Crypter.padding UnsupportedEncodingException");
+ }
+ return null;
+ }
+
+}
+
diff --git a/src/main/java/com/yexuejc/base/util/ToUeProperty.java b/src/main/java/com/yexuejc/base/util/ToUeProperty.java
new file mode 100644
index 0000000..4398c63
--- /dev/null
+++ b/src/main/java/com/yexuejc/base/util/ToUeProperty.java
@@ -0,0 +1,14 @@
+package com.yexuejc.base.util;
+
+import java.lang.annotation.*;
+
+@Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface ToUeProperty {
+ /**
+ * 字段名,默认该字段转下划线
+ * @return
+ */
+ String value() default "";
+}
\ No newline at end of file