From c2bb2ad67a558b00b6aec9806eae0a58d026f7fd Mon Sep 17 00:00:00 2001 From: CliveYuan Date: Tue, 15 Jun 2021 16:21:23 +0800 Subject: [PATCH 1/4] slimming --- README.md | 5 +- common/pom.xml | 76 +++++ .../cn/cliveyuan/tools/common}/AESTools.java | 8 +- .../cliveyuan/tools/common}/ArrayTools.java | 2 +- .../cliveyuan/tools/common/AssertTools.java | 60 ++-- .../cn/cliveyuan/tools/common}/CsvTools.java | 53 ++-- .../tools/common}/DateTimeTools.java | 39 +-- .../cliveyuan/tools/common}/EmailTools.java | 265 +++++------------- .../cn/cliveyuan/tools/common}/FileTools.java | 6 +- .../tools/common}/GenericTypeTools.java | 3 +- .../cliveyuan/tools/common}/IDCardTools.java | 2 +- .../cn/cliveyuan/tools/common}/IdTools.java | 7 +- .../cn/cliveyuan/tools/common}/MD5Tools.java | 6 +- .../cn/cliveyuan/tools/common/MapTools.java | 28 ++ .../cn/cliveyuan/tools/common}/RSATools.java | 44 ++- .../cliveyuan/tools/common}/StringTools.java | 107 +++++-- .../cliveyuan/tools/common}/TableUtils.java | 93 +----- .../cliveyuan/tools/common}/ThreadTools.java | 2 +- .../tools/common}/ValidateTools.java | 64 ++++- .../cn/cliveyuan/tools/common}/ZipTools.java | 4 +- .../tools/common/bean/EmailParam.java | 72 +++++ .../tools/common/bean/ValidationResult.java | 64 +++++ .../tools/common/bean}/csv/CsvReader.java | 2 +- .../tools/common/bean}/csv/CsvWriter.java | 8 +- .../common}/bean/rsa/GenerateKeyPairReq.java | 2 +- .../tools/common}/bean/rsa/RSAKeyPair.java | 2 +- .../tools/common}/bean/rsa/RSARequest.java | 2 +- .../tools/common}/bean/rsa/RSASignReq.java | 2 +- .../common}/bean/rsa/RSAVerifySignReq.java | 2 +- .../common}/enums/CaptchaGenerateType.java | 2 +- .../tools/common}/enums/CaptchaType.java | 2 +- .../tools/common}/enums/DateTimeField.java | 2 +- .../tools/common}/enums/DateTimeFormat.java | 2 +- .../common}/exception/BaseException.java | 2 +- .../common}/exception/EmailException.java | 2 +- .../src}/main/resources/fastjson.properties | 0 .../tools/common}/test/AESToolsTest.java | 4 +- .../tools/common}/test/CsvToolsTest.java | 18 +- .../tools/common}/test/DateTimeToolsTest.java | 8 +- .../tools/common/test/EmailToolsTest.java | 45 +++ .../tools/common}/test/FileToolsTest.java | 14 +- .../tools/common}/test/IdToolsTest.java | 4 +- .../tools/common}/test/StringToolsTest.java | 29 +- .../tools/common}/test/ThreadToolsTest.java | 4 +- .../tools/common}/test/ValidateToolsTest.java | 12 +- .../tools/common}/test/ZipToolsTest.java | 4 +- .../tools/common/test/bean}/Person.java | 2 +- doc/feature.md | 2 +- httpclient/pom.xml | 26 ++ .../tools/httpclient/HttpClientSingleton.java | 53 ++++ .../tools/httpclient/HttpClientTools.java | 162 +++++++++++ .../httpclient/bean/HttpClientRequest.java | 144 ++++++++++ .../httpclient/bean/HttpClientResponse.java | 38 +++ .../bean/RequestContextTypeEnum.java | 19 ++ .../httpclient/bean/RequestMethodEnum.java | 12 + .../tools/httpclient/client/HttpClient.java | 21 ++ .../httpclient/client/OkHttpHttpClient.java | 187 ++++++++++++ .../cliveyuan/tools/httpclient/ssl/SSL.java | 88 ++++++ .../tools/httpclient/ssl/TrustAnySSL.java | 57 ++++ .../httpclient/test/HttpClientToolsTest.java | 72 +++++ poi/pom.xml | 29 ++ .../cn/cliveyuan/tools/poi/ExcelTools.java | 104 +++++++ .../tools/poi/bean}/ExcelReader.java | 34 +-- .../cliveyuan/tools/poi/bean}/ExcelType.java | 4 +- .../tools/poi/bean}/ExcelWriter.java | 23 +- .../tools/poi/bean}/SheetContent.java | 2 +- .../cliveyuan/tools/poi/bean}/SheetData.java | 2 +- .../cliveyuan/tools/poi/bean}/SheetInfo.java | 2 +- .../tools/poi}/exception/ExcelException.java | 4 +- .../tools/poi/inner/ExcelInnerHelper.java | 156 ++++++++--- .../tools/poi}/test/ExcelToolsTest.java | 53 +++- .../tools/poi/test/bean}/ExcelData.java | 2 +- .../tools/poi/test/bean}/ExcelIdea.java | 2 +- .../tools/poi/test/bean}/ExcelProfile.java | 2 +- .../tools/poi/test/bean}/ExcelWebsite.java | 2 +- .../cliveyuan/tools/poi/test/bean/Person.java | 190 +++++++++++++ pom.xml | 250 ++++++++--------- .../cliveyuan/tools/CollectionTools.java | 105 ------- .../com/gitee/cliveyuan/tools/MathTools.java | 24 -- .../gitee/cliveyuan/tools/action/Action.java | 11 - .../gitee/cliveyuan/tools/action/Action2.java | 10 - .../gitee/cliveyuan/tools/action/Action3.java | 10 - .../gitee/cliveyuan/tools/action/Action4.java | 10 - .../gitee/cliveyuan/tools/action/Action5.java | 10 - .../tools/bean/excel/ExcelGenerateParam.java | 29 -- .../gitee/cliveyuan/tools/cons/NetCons.java | 40 --- .../tools/exception/CsvException.java | 29 -- .../tools/exception/NetException.java | 40 --- .../tools/http/AbstractHttpRequest.java | 260 ----------------- .../gitee/cliveyuan/tools/http/HtmlTools.java | 47 ---- .../cliveyuan/tools/http/HttpClientTools.java | 265 ------------------ .../gitee/cliveyuan/tools/http/HttpResp.java | 84 ------ .../cliveyuan/tools/http/InnerTools.java | 57 ---- .../cliveyuan/tools/http/JsoupTools.java | 112 -------- .../tools/img/ImageConvertTools.java | 61 ---- .../gitee/cliveyuan/tools/img/ImageTools.java | 116 -------- .../cliveyuan/tools/img/ImgTypeTools.java | 98 ------- .../gitee/cliveyuan/tools/sql/SqlTools.java | 35 --- .../cliveyuan/tools/text/StrFormatter.java | 78 ------ .../cliveyuan/tools/test/EmailToolsTest.java | 39 --- .../cliveyuan/tools/test/HtmlToolsTest.java | 65 ----- .../tools/test/HttpClientToolsTest.java | 106 ------- .../cliveyuan/tools/test/ImageToolsTest.java | 42 --- .../cliveyuan/tools/test/JsoupToolsTest.java | 87 ------ .../cliveyuan/tools/test/MathToolsTest.java | 24 -- .../com/gitee/cliveyuan/tools/test/Test2.java | 72 ----- .../gitee/cliveyuan/tools/test/TestCons.java | 11 - .../gitee/cliveyuan/tools/test/XmlyTest.java | 44 --- .../tools/test/fastjson/American.java | 12 - .../tools/test/fastjson/Chinese.java | 13 - .../tools/test/fastjson/FastjsonTest.java | 42 --- .../cliveyuan/tools/test/fastjson/Person.java | 21 -- src/test/resources/git.jpg | Bin 17106 -> 0 bytes src/test/resources/log4j.properties | 12 - src/test/resources/read_test.txt | 10 - src/test/resources/watermark.png | Bin 11169 -> 0 bytes web/pom.xml | 31 ++ .../cn}/cliveyuan/tools/web/CaptchaTools.java | 65 +++-- .../cliveyuan/tools/web/FileUploadTools.java | 28 +- .../java/cn}/cliveyuan/tools/web/IpTools.java | 3 +- .../cliveyuan/tools/web}/VerifyCodeTools.java | 13 +- .../cn/cliveyuan/tools/web/bean/Captcha.java | 4 +- .../tools/web/bean}/CaptchaCodeResult.java | 2 +- .../tools/web/bean/CaptchaGenerateType.java | 18 ++ .../tools/web/bean}/CaptchaRequest.java | 4 +- .../cliveyuan/tools/web/bean/CaptchaType.java | 20 ++ .../tools/web}/bean/CaptchaValidate.java | 6 +- .../tools/web}/bean/FileUploadRequest.java | 2 +- .../tools/web}/bean/FileUploadResponse.java | 2 +- .../web}/exception/CaptchaException.java | 2 +- .../web}/exception/FileUploadException.java | 4 +- 131 files changed, 2362 insertions(+), 2901 deletions(-) create mode 100644 common/pom.xml rename {src/main/java/com/gitee/cliveyuan/tools/codec => common/src/main/java/cn/cliveyuan/tools/common}/AESTools.java (89%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/ArrayTools.java (84%) rename src/main/java/com/gitee/cliveyuan/tools/Assert.java => common/src/main/java/cn/cliveyuan/tools/common/AssertTools.java (90%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/CsvTools.java (72%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/DateTimeTools.java (84%) rename {src/main/java/com/gitee/cliveyuan/tools/mail => common/src/main/java/cn/cliveyuan/tools/common}/EmailTools.java (42%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/FileTools.java (91%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/GenericTypeTools.java (94%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/IDCardTools.java (99%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/IdTools.java (79%) rename {src/main/java/com/gitee/cliveyuan/tools/codec => common/src/main/java/cn/cliveyuan/tools/common}/MD5Tools.java (78%) create mode 100644 common/src/main/java/cn/cliveyuan/tools/common/MapTools.java rename {src/main/java/com/gitee/cliveyuan/tools/codec => common/src/main/java/cn/cliveyuan/tools/common}/RSATools.java (88%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/StringTools.java (69%) rename {src/main/java/com/gitee/cliveyuan/tools/data => common/src/main/java/cn/cliveyuan/tools/common}/TableUtils.java (44%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/ThreadTools.java (95%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/ValidateTools.java (67%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/ZipTools.java (94%) create mode 100644 common/src/main/java/cn/cliveyuan/tools/common/bean/EmailParam.java create mode 100644 common/src/main/java/cn/cliveyuan/tools/common/bean/ValidationResult.java rename {src/main/java/com/gitee/cliveyuan/tools/data => common/src/main/java/cn/cliveyuan/tools/common/bean}/csv/CsvReader.java (93%) rename {src/main/java/com/gitee/cliveyuan/tools/data => common/src/main/java/cn/cliveyuan/tools/common/bean}/csv/CsvWriter.java (79%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/bean/rsa/GenerateKeyPairReq.java (88%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/bean/rsa/RSAKeyPair.java (84%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/bean/rsa/RSARequest.java (92%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/bean/rsa/RSASignReq.java (94%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/bean/rsa/RSAVerifySignReq.java (94%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/enums/CaptchaGenerateType.java (80%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/enums/CaptchaType.java (82%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/enums/DateTimeField.java (83%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/enums/DateTimeFormat.java (94%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/exception/BaseException.java (91%) rename {src/main/java/com/gitee/cliveyuan/tools => common/src/main/java/cn/cliveyuan/tools/common}/exception/EmailException.java (94%) rename {src => common/src}/main/resources/fastjson.properties (100%) rename {src/test/java/com/gitee/cliveyuan/tools => common/src/test/java/cn/cliveyuan/tools/common}/test/AESToolsTest.java (88%) rename {src/test/java/com/gitee/cliveyuan/tools => common/src/test/java/cn/cliveyuan/tools/common}/test/CsvToolsTest.java (79%) rename {src/test/java/com/gitee/cliveyuan/tools => common/src/test/java/cn/cliveyuan/tools/common}/test/DateTimeToolsTest.java (85%) create mode 100644 common/src/test/java/cn/cliveyuan/tools/common/test/EmailToolsTest.java rename {src/test/java/com/gitee/cliveyuan/tools => common/src/test/java/cn/cliveyuan/tools/common}/test/FileToolsTest.java (53%) rename {src/test/java/com/gitee/cliveyuan/tools => common/src/test/java/cn/cliveyuan/tools/common}/test/IdToolsTest.java (76%) rename {src/test/java/com/gitee/cliveyuan/tools => common/src/test/java/cn/cliveyuan/tools/common}/test/StringToolsTest.java (76%) rename {src/test/java/com/gitee/cliveyuan/tools => common/src/test/java/cn/cliveyuan/tools/common}/test/ThreadToolsTest.java (82%) rename {src/test/java/com/gitee/cliveyuan/tools => common/src/test/java/cn/cliveyuan/tools/common}/test/ValidateToolsTest.java (95%) rename {src/test/java/com/gitee/cliveyuan/tools => common/src/test/java/cn/cliveyuan/tools/common}/test/ZipToolsTest.java (90%) rename {src/test/java/com/gitee/cliveyuan/tools/test/bean/excel => common/src/test/java/cn/cliveyuan/tools/common/test/bean}/Person.java (98%) create mode 100644 httpclient/pom.xml create mode 100644 httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientSingleton.java create mode 100644 httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientTools.java create mode 100644 httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientRequest.java create mode 100644 httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientResponse.java create mode 100644 httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/RequestContextTypeEnum.java create mode 100644 httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/RequestMethodEnum.java create mode 100644 httpclient/src/main/java/cn/cliveyuan/tools/httpclient/client/HttpClient.java create mode 100644 httpclient/src/main/java/cn/cliveyuan/tools/httpclient/client/OkHttpHttpClient.java create mode 100644 httpclient/src/main/java/cn/cliveyuan/tools/httpclient/ssl/SSL.java create mode 100644 httpclient/src/main/java/cn/cliveyuan/tools/httpclient/ssl/TrustAnySSL.java create mode 100644 httpclient/src/test/java/cn/cliveyuan/tools/httpclient/test/HttpClientToolsTest.java create mode 100644 poi/pom.xml create mode 100644 poi/src/main/java/cn/cliveyuan/tools/poi/ExcelTools.java rename {src/main/java/com/gitee/cliveyuan/tools/data/excel => poi/src/main/java/cn/cliveyuan/tools/poi/bean}/ExcelReader.java (39%) rename {src/main/java/com/gitee/cliveyuan/tools/enums => poi/src/main/java/cn/cliveyuan/tools/poi/bean}/ExcelType.java (63%) rename {src/main/java/com/gitee/cliveyuan/tools/data/excel => poi/src/main/java/cn/cliveyuan/tools/poi/bean}/ExcelWriter.java (59%) rename {src/main/java/com/gitee/cliveyuan/tools/bean/excel => poi/src/main/java/cn/cliveyuan/tools/poi/bean}/SheetContent.java (90%) rename {src/main/java/com/gitee/cliveyuan/tools/bean/excel => poi/src/main/java/cn/cliveyuan/tools/poi/bean}/SheetData.java (89%) rename {src/main/java/com/gitee/cliveyuan/tools/bean/excel => poi/src/main/java/cn/cliveyuan/tools/poi/bean}/SheetInfo.java (88%) rename {src/main/java/com/gitee/cliveyuan/tools => poi/src/main/java/cn/cliveyuan/tools/poi}/exception/ExcelException.java (91%) rename src/main/java/com/gitee/cliveyuan/tools/ExcelTools.java => poi/src/main/java/cn/cliveyuan/tools/poi/inner/ExcelInnerHelper.java (57%) rename {src/test/java/com/gitee/cliveyuan/tools => poi/src/test/java/cn/cliveyuan/tools/poi}/test/ExcelToolsTest.java (56%) rename {src/test/java/com/gitee/cliveyuan/tools/test/bean/excel => poi/src/test/java/cn/cliveyuan/tools/poi/test/bean}/ExcelData.java (98%) rename {src/test/java/com/gitee/cliveyuan/tools/test/bean/excel => poi/src/test/java/cn/cliveyuan/tools/poi/test/bean}/ExcelIdea.java (84%) rename {src/test/java/com/gitee/cliveyuan/tools/test/bean/excel => poi/src/test/java/cn/cliveyuan/tools/poi/test/bean}/ExcelProfile.java (86%) rename {src/test/java/com/gitee/cliveyuan/tools/test/bean/excel => poi/src/test/java/cn/cliveyuan/tools/poi/test/bean}/ExcelWebsite.java (85%) create mode 100644 poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/Person.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/CollectionTools.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/MathTools.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/action/Action.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/action/Action2.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/action/Action3.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/action/Action4.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/action/Action5.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/bean/excel/ExcelGenerateParam.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/cons/NetCons.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/exception/CsvException.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/exception/NetException.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/http/AbstractHttpRequest.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/http/HtmlTools.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/http/HttpClientTools.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/http/HttpResp.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/http/InnerTools.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/http/JsoupTools.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/img/ImageConvertTools.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/img/ImageTools.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/img/ImgTypeTools.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/sql/SqlTools.java delete mode 100644 src/main/java/com/gitee/cliveyuan/tools/text/StrFormatter.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/EmailToolsTest.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/HtmlToolsTest.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/HttpClientToolsTest.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/ImageToolsTest.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/JsoupToolsTest.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/MathToolsTest.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/Test2.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/TestCons.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/XmlyTest.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/fastjson/American.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/fastjson/Chinese.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/fastjson/FastjsonTest.java delete mode 100644 src/test/java/com/gitee/cliveyuan/tools/test/fastjson/Person.java delete mode 100644 src/test/resources/git.jpg delete mode 100755 src/test/resources/log4j.properties delete mode 100644 src/test/resources/read_test.txt delete mode 100644 src/test/resources/watermark.png create mode 100644 web/pom.xml rename {src/main/java/com/gitee => web/src/main/java/cn}/cliveyuan/tools/web/CaptchaTools.java (82%) rename {src/main/java/com/gitee => web/src/main/java/cn}/cliveyuan/tools/web/FileUploadTools.java (82%) rename {src/main/java/com/gitee => web/src/main/java/cn}/cliveyuan/tools/web/IpTools.java (96%) rename {src/main/java/com/gitee/cliveyuan/tools => web/src/main/java/cn/cliveyuan/tools/web}/VerifyCodeTools.java (97%) rename src/main/java/com/gitee/cliveyuan/tools/bean/captcha/CaptchaV2.java => web/src/main/java/cn/cliveyuan/tools/web/bean/Captcha.java (78%) rename {src/main/java/com/gitee/cliveyuan/tools/bean/captcha => web/src/main/java/cn/cliveyuan/tools/web/bean}/CaptchaCodeResult.java (84%) create mode 100644 web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaGenerateType.java rename {src/main/java/com/gitee/cliveyuan/tools/bean/captcha => web/src/main/java/cn/cliveyuan/tools/web/bean}/CaptchaRequest.java (91%) create mode 100644 web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaType.java rename {src/main/java/com/gitee/cliveyuan/tools => web/src/main/java/cn/cliveyuan/tools/web}/bean/CaptchaValidate.java (83%) rename {src/main/java/com/gitee/cliveyuan/tools => web/src/main/java/cn/cliveyuan/tools/web}/bean/FileUploadRequest.java (95%) rename {src/main/java/com/gitee/cliveyuan/tools => web/src/main/java/cn/cliveyuan/tools/web}/bean/FileUploadResponse.java (93%) rename {src/main/java/com/gitee/cliveyuan/tools => web/src/main/java/cn/cliveyuan/tools/web}/exception/CaptchaException.java (96%) rename {src/main/java/com/gitee/cliveyuan/tools => web/src/main/java/cn/cliveyuan/tools/web}/exception/FileUploadException.java (90%) diff --git a/README.md b/README.md index 4839fe8..4502a1a 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Some commonly used methods in JAVA com.gitee.opensource4clive java-tools - 3.1.0 + 4.0.0 ``` ## Feature List @@ -19,6 +19,9 @@ Some commonly used methods in JAVA ## Release Logs +### v4.0.0 2021-06-09 +* slim tools: split to common, httpclient, poi, web + ### v3.1.0 2020-06-16 * add tools: SqlTools,ArrayTools * add methods: format,substring,trim in StringTools diff --git a/common/pom.xml b/common/pom.xml new file mode 100644 index 0000000..aa0b173 --- /dev/null +++ b/common/pom.xml @@ -0,0 +1,76 @@ + + + + java-tools + com.gitee.opensource4clive + 4.0.0 + + 4.0.0 + + java-tools-common + + + + org.slf4j + slf4j-api + + + com.alibaba + fastjson + + + org.projectlombok + lombok + + + org.apache.commons + commons-collections4 + + + commons-io + commons-io + + + org.apache.commons + commons-lang3 + + + commons-codec + commons-codec + + + org.hibernate.validator + hibernate-validator + + + org.glassfish + jakarta.el + compile + + + net.lingala.zip4j + zip4j + + + javax.mail + javax.mail-api + + + com.sun.mail + javax.mail + ${email.version} + + + org.hibernate.validator + hibernate-validator + + + org.glassfish + jakarta.el + compile + + + + diff --git a/src/main/java/com/gitee/cliveyuan/tools/codec/AESTools.java b/common/src/main/java/cn/cliveyuan/tools/common/AESTools.java similarity index 89% rename from src/main/java/com/gitee/cliveyuan/tools/codec/AESTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/AESTools.java index 06604c8..b26abdd 100755 --- a/src/main/java/com/gitee/cliveyuan/tools/codec/AESTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/AESTools.java @@ -1,7 +1,5 @@ -package com.gitee.cliveyuan.tools.codec; +package cn.cliveyuan.tools.common; -import com.gitee.cliveyuan.tools.Assert; -import com.gitee.cliveyuan.tools.StringTools; import org.apache.commons.codec.binary.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,7 +76,7 @@ public class AESTools { } private static void validate(String aesKey) { - Assert.notEmpty(aesKey, "AES key is required"); - Assert.isTrue(aesKey.length() == 16, "the length of AES key must be 16"); + AssertTools.notEmpty(aesKey, "AES key is required"); + AssertTools.isTrue(aesKey.length() == 16, "the length of AES key must be 16"); } } diff --git a/src/main/java/com/gitee/cliveyuan/tools/ArrayTools.java b/common/src/main/java/cn/cliveyuan/tools/common/ArrayTools.java similarity index 84% rename from src/main/java/com/gitee/cliveyuan/tools/ArrayTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/ArrayTools.java index cc42dc8..d55a2cc 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/ArrayTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/ArrayTools.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; import org.apache.commons.lang3.ArrayUtils; diff --git a/src/main/java/com/gitee/cliveyuan/tools/Assert.java b/common/src/main/java/cn/cliveyuan/tools/common/AssertTools.java similarity index 90% rename from src/main/java/com/gitee/cliveyuan/tools/Assert.java rename to common/src/main/java/cn/cliveyuan/tools/common/AssertTools.java index 0b60831..f8f8f56 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/Assert.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/AssertTools.java @@ -2,7 +2,7 @@ * reference from spring framework */ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; import java.util.Collection; import java.util.Map; @@ -29,8 +29,8 @@ import java.util.Map; * For example: * *
- * Assert.notNull(clazz, "The class must not be null");
- * Assert.isTrue(i < 0, "The value must be greater than zero");
+ * AssertTools.notNull(clazz, "The class must not be null");
+ * AssertTools.isTrue(i < 0, "The value must be greater than zero");
  * 
*
* Mainly for internal use within the framework; consider Apache's Commons Lang @@ -43,14 +43,14 @@ import java.util.Map; * @author Clive Yuan * @since 1.1.2 */ -public abstract class Assert { +public class AssertTools { /** * Assert a boolean expression, throwing {@code IllegalArgumentException} if * the test result is {@code false}. * *
-     * Assert.isTrue(i > 0, "The value must be greater than zero");
+     * AssertTools.isTrue(i > 0, "The value must be greater than zero");
      * 
* * @param expression a boolean expression @@ -68,7 +68,7 @@ public abstract class Assert { * the test result is {@code false}. * *
-     * Assert.isTrue(i > 0);
+     * AssertTools.isTrue(i > 0);
      * 
* * @param expression a boolean expression @@ -82,7 +82,7 @@ public abstract class Assert { * Assert that an object is {@code null} . * *
-     * Assert.isNull(value, "The value must be null");
+     * AssertTools.isNull(value, "The value must be null");
      * 
* * @param object the object to check @@ -99,7 +99,7 @@ public abstract class Assert { * Assert that an object is {@code null} . * *
-     * Assert.isNull(value);
+     * AssertTools.isNull(value);
      * 
* * @param object the object to check @@ -113,7 +113,7 @@ public abstract class Assert { * Assert that an object is not {@code null} . * *
-     * Assert.notNull(clazz, "The class must not be null");
+     * AssertTools.notNull(clazz, "The class must not be null");
      * 
* * @param object the object to check @@ -130,7 +130,7 @@ public abstract class Assert { * Assert that an object is not {@code null} . * *
-     * Assert.notNull(clazz);
+     * AssertTools.notNull(clazz);
      * 
* * @param object the object to check @@ -145,7 +145,7 @@ public abstract class Assert { * {@code null} and not the empty String. * *
-     * Assert.hasLength(name, "Name must not be empty");
+     * AssertTools.hasLength(name, "Name must not be empty");
      * 
* * @param text the String to check @@ -163,7 +163,7 @@ public abstract class Assert { * {@code null} and not the empty String. * *
-     * Assert.hasLength(name);
+     * AssertTools.hasLength(name);
      * 
* * @param text the String to check @@ -178,7 +178,7 @@ public abstract class Assert { * be {@code null} and must contain at least one non-whitespace character. * *
-     * Assert.hasText(name, "'name' must not be empty");
+     * AssertTools.hasText(name, "'name' must not be empty");
      * 
* * @param text the String to check @@ -196,7 +196,7 @@ public abstract class Assert { * be {@code null} and must contain at least one non-whitespace character. * *
-     * Assert.hasText(name, "'name' must not be empty");
+     * AssertTools.hasText(name, "'name' must not be empty");
      * 
* * @param text the String to check @@ -210,7 +210,7 @@ public abstract class Assert { * Assert that the given text does not contain the given substring. * *
-     * Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");
+     * AssertTools.doesNotContain(name, "rod", "Name must not contain 'rod'");
      * 
* * @param textToSearch the text to search @@ -228,7 +228,7 @@ public abstract class Assert { * Assert that the given text does not contain the given substring. * *
-     * Assert.doesNotContain(name, "rod");
+     * AssertTools.doesNotContain(name, "rod");
      * 
* * @param textToSearch the text to search @@ -244,7 +244,7 @@ public abstract class Assert { * and must have at least one element. * *
-     * Assert.notEmpty(array, "The array must have elements");
+     * AssertTools.notEmpty(array, "The array must have elements");
      * 
* * @param array the array to check @@ -262,7 +262,7 @@ public abstract class Assert { * and must have at least one element. * *
-     * Assert.notEmpty(array);
+     * AssertTools.notEmpty(array);
      * 
* * @param array the array to check @@ -277,7 +277,7 @@ public abstract class Assert { * array is empty! * *
-     * Assert.noNullElements(array, "The array must have non-null elements");
+     * AssertTools.noNullElements(array, "The array must have non-null elements");
      * 
* * @param array the array to check @@ -299,7 +299,7 @@ public abstract class Assert { * array is empty! * *
-     * Assert.noNullElements(array);
+     * AssertTools.noNullElements(array);
      * 
* * @param array the array to check @@ -314,7 +314,7 @@ public abstract class Assert { * {@code null} and must have at least one element. * *
-     * Assert.notEmpty(collection, "Collection must have elements");
+     * AssertTools.notEmpty(collection, "Collection must have elements");
      * 
* * @param collection the collection to check @@ -332,7 +332,7 @@ public abstract class Assert { * {@code null} and must have at least one element. * *
-     * Assert.notEmpty(collection, "Collection must have elements");
+     * AssertTools.notEmpty(collection, "Collection must have elements");
      * 
* * @param collection the collection to check @@ -348,7 +348,7 @@ public abstract class Assert { * must have at least one entry. * *
-     * Assert.notEmpty(map, "Map must have entries");
+     * AssertTools.notEmpty(map, "Map must have entries");
      * 
* * @param map the map to check @@ -366,7 +366,7 @@ public abstract class Assert { * must have at least one entry. * *
-     * Assert.notEmpty(map);
+     * AssertTools.notEmpty(map);
      * 
* * @param map the map to check @@ -380,7 +380,7 @@ public abstract class Assert { * Assert that the provided object is an instance of the provided class. * *
-     * Assert.instanceOf(Foo.class, foo);
+     * AssertTools.instanceOf(Foo.class, foo);
      * 
* * @param clazz the required class @@ -396,7 +396,7 @@ public abstract class Assert { * Assert that the provided object is an instance of the provided class. * *
-     * Assert.instanceOf(Foo.class, foo);
+     * AssertTools.instanceOf(Foo.class, foo);
      * 
* * @param type the type to check against @@ -421,7 +421,7 @@ public abstract class Assert { * Assert that {@code superType.isAssignableFrom(subType)} is {@code true}. * *
-     * Assert.isAssignable(Number.class, myClass);
+     * AssertTools.isAssignable(Number.class, myClass);
      * 
* * @param superType the super type to check @@ -436,7 +436,7 @@ public abstract class Assert { * Assert that {@code superType.isAssignableFrom(subType)} is {@code true}. * *
-     * Assert.isAssignable(Number.class, myClass);
+     * AssertTools.isAssignable(Number.class, myClass);
      * 
* * @param superType the super type to check against @@ -460,7 +460,7 @@ public abstract class Assert { * IllegalArgumentException on an assertion failure. * *
-     * Assert.state(id == null, "The id property must not already be initialized");
+     * AssertTools.state(id == null, "The id property must not already be initialized");
      * 
* * @param expression a boolean expression @@ -481,7 +481,7 @@ public abstract class Assert { * {@link IllegalArgumentException} on an assertion failure. * *
-     * Assert.state(id == null);
+     * AssertTools.state(id == null);
      * 
* * @param expression a boolean expression diff --git a/src/main/java/com/gitee/cliveyuan/tools/CsvTools.java b/common/src/main/java/cn/cliveyuan/tools/common/CsvTools.java similarity index 72% rename from src/main/java/com/gitee/cliveyuan/tools/CsvTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/CsvTools.java index 7fce14a..b0e585e 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/CsvTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/CsvTools.java @@ -1,10 +1,7 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; -import com.gitee.cliveyuan.tools.data.TableUtils; -import com.gitee.cliveyuan.tools.data.csv.CsvReader; -import com.gitee.cliveyuan.tools.data.csv.CsvWriter; -import com.gitee.cliveyuan.tools.exception.CsvException; -import com.google.common.collect.Lists; +import cn.cliveyuan.tools.common.bean.csv.CsvReader; +import cn.cliveyuan.tools.common.bean.csv.CsvWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,11 +9,12 @@ import java.beans.PropertyDescriptor; import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; +import java.util.UUID; -import static com.gitee.cliveyuan.tools.data.TableUtils.SDF; /** * @author clive @@ -30,7 +28,6 @@ public class CsvTools { private final static String CSV = "csv"; private static final String EXTENSION = "." + CSV; - /** * 读取CSV文件 * @@ -39,9 +36,9 @@ public class CsvTools { * @return 数据列表 */ public static List read(CsvReader csvReader) { - Assert.notNull(csvReader, "csvReader is required"); - Assert.notNull(csvReader.getFilePath(), "filePath is required"); - Assert.notNull(csvReader.getClazz(), "clazz is required"); + AssertTools.notNull(csvReader, "csvReader is required"); + AssertTools.notNull(csvReader.getFilePath(), "filePath is required"); + AssertTools.notNull(csvReader.getClazz(), "clazz is required"); List data = CsvTools.readRawCsv(csvReader.getFilePath(), csvReader.getEncoding()); return TableUtils.dataToObject(csvReader.getSkipRowNo(), data, csvReader.getClazz()); } @@ -54,9 +51,9 @@ public class CsvTools { * @return CSV文件 */ public static File write(CsvWriter csvWriter) { - Assert.notEmpty(csvWriter.getHeaders(), "headers can't be empty"); - Assert.notEmpty(csvWriter.getData(), "data can't be empty"); - Assert.notNull(csvWriter.getFilePath(), "file path can't be empty"); + AssertTools.notEmpty(csvWriter.getHeaders(), "headers can't be empty"); + AssertTools.notEmpty(csvWriter.getData(), "data can't be empty"); + AssertTools.notNull(csvWriter.getPathname(), "file path can't be empty"); return doWrite(csvWriter); } @@ -65,23 +62,21 @@ public class CsvTools { * * @param absoluteFilePath 文件绝对路径 */ - public static List readRawCsv(String absoluteFilePath, String encoding) throws CsvException { - Assert.notBlank(absoluteFilePath, "csv file path is empty"); + public static List readRawCsv(String absoluteFilePath, String encoding) { + AssertTools.notBlank(absoluteFilePath, "csv file path is empty"); File file = new File(absoluteFilePath); - Assert.isTrue(file.exists(), "csv file not exists"); - List list = Lists.newArrayList(); + AssertTools.isTrue(file.exists(), "csv file not exists"); + List list = new ArrayList(); try { String extension = FileTools.getExtension(absoluteFilePath); if (!CSV.equalsIgnoreCase(extension)) { - throw CsvException.notSupport(extension); + throw new RuntimeException("not support this file: " + extension); } List lines = FileTools.readFileToStringList(file, encoding); lines.forEach(line -> list.add(line.split(","))); - } catch (CsvException e) { - throw e; } catch (Exception e) { logger.error("readCsv ", e); - throw CsvException.failToParseCsv(); + throw new RuntimeException(e); } return list; } @@ -89,17 +84,21 @@ public class CsvTools { //region private methods private static File doWrite(CsvWriter csvWriter) { - String filePath = csvWriter.getFilePath(); + String filePath = csvWriter.getPathname(); String[] headers = csvWriter.getHeaders(); Collection data = csvWriter.getData(); File file = new File(filePath); if (!file.exists()) { boolean result = file.mkdirs(); if (!result) { - throw CsvException.failToMkdirs(); + throw new RuntimeException("Fail to execute mkdirs"); } } - String fileName = System.currentTimeMillis() + EXTENSION; + String name = csvWriter.getFileName(); + if (StringTools.isBlank(name)) { + name = UUID.randomUUID().toString(); + } + String fileName = name + EXTENSION; if (!filePath.endsWith(File.separator)) { filePath = filePath + File.separator; } @@ -126,7 +125,7 @@ public class CsvTools { String value = StringTools.EMPTY; if (invoke != null) { if (invoke instanceof Date) { - value = SDF.format((Date) invoke); + value = TableUtils.SDF.format((Date) invoke); } else { value = invoke.toString(); } @@ -141,7 +140,7 @@ public class CsvTools { return file; } catch (Exception e) { logger.error("excel generate exception", e); - throw CsvException.failToGenerateCsv(); + throw new RuntimeException(e); } } //endregion diff --git a/src/main/java/com/gitee/cliveyuan/tools/DateTimeTools.java b/common/src/main/java/cn/cliveyuan/tools/common/DateTimeTools.java similarity index 84% rename from src/main/java/com/gitee/cliveyuan/tools/DateTimeTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/DateTimeTools.java index 1280b56..0e81403 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/DateTimeTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/DateTimeTools.java @@ -1,20 +1,20 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; -import com.gitee.cliveyuan.tools.enums.DateTimeField; -import com.gitee.cliveyuan.tools.enums.DateTimeFormat; +import cn.cliveyuan.tools.common.enums.DateTimeField; +import cn.cliveyuan.tools.common.enums.DateTimeFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.time.*; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.Date; import java.util.Objects; /** * 日期时间工具 * - * @author: clive - * Created on 2018/07/23 - * @since: 1.0 + * @author clive */ public class DateTimeTools { @@ -67,8 +67,8 @@ public class DateTimeTools { * @param date2 */ public static boolean before(Date date1, Date date2) { - Assert.notNull(date1, "date1 can't be null"); - Assert.notNull(date2, "date2 can't be null"); + AssertTools.notNull(date1, "date1 can't be null"); + AssertTools.notNull(date2, "date2 can't be null"); return date1.before(date2); } @@ -79,8 +79,8 @@ public class DateTimeTools { * @param date2 */ public static boolean after(Date date1, Date date2) { - Assert.notNull(date1, "date1 can't be null"); - Assert.notNull(date2, "date2 can't be null"); + AssertTools.notNull(date1, "date1 can't be null"); + AssertTools.notNull(date2, "date2 can't be null"); return date1.after(date2); } @@ -90,7 +90,7 @@ public class DateTimeTools { * @param date */ public static boolean beforeNow(Date date) { - Assert.notNull(date, "date can't be null"); + AssertTools.notNull(date, "date can't be null"); return before(date, now()); } @@ -100,7 +100,7 @@ public class DateTimeTools { * @param date */ public static boolean afterNow(Date date) { - Assert.notNull(date, "date can't be null"); + AssertTools.notNull(date, "date can't be null"); return after(date, now()); } @@ -130,8 +130,8 @@ public class DateTimeTools { * @param format */ public static String format(Date date, String format) { - Assert.notNull(date, "date can't be null"); - Assert.notEmpty(format, "format can't be empty"); + AssertTools.notNull(date, "date can't be null"); + AssertTools.notEmpty(format, "format can't be empty"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); return simpleDateFormat.format(date); } @@ -145,7 +145,7 @@ public class DateTimeTools { * @throws ParseException */ public static Date parse(String dateStr, String format) throws ParseException { - Assert.notEmpty(dateStr, "dateStr can't be empty"); + AssertTools.notEmpty(dateStr, "dateStr can't be empty"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); return simpleDateFormat.parse(dateStr); } @@ -216,6 +216,13 @@ public class DateTimeTools { return System.currentTimeMillis(); } + /** + * 当前时间的秒 + */ + public static int nowSeconds() { + return (int) (System.currentTimeMillis() / 1000L); + } + /** * 计算耗时 单位 毫秒 * diff --git a/src/main/java/com/gitee/cliveyuan/tools/mail/EmailTools.java b/common/src/main/java/cn/cliveyuan/tools/common/EmailTools.java similarity index 42% rename from src/main/java/com/gitee/cliveyuan/tools/mail/EmailTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/EmailTools.java index f4b8a9e..3255fe2 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/mail/EmailTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/EmailTools.java @@ -1,24 +1,35 @@ -package com.gitee.cliveyuan.tools.mail; +package cn.cliveyuan.tools.common; -import com.gitee.cliveyuan.tools.Assert; -import com.gitee.cliveyuan.tools.CollectionTools; -import com.gitee.cliveyuan.tools.DateTimeTools; -import com.gitee.cliveyuan.tools.StringTools; -import com.gitee.cliveyuan.tools.exception.EmailException; -import com.google.common.collect.Lists; +import cn.cliveyuan.tools.common.bean.EmailParam; +import cn.cliveyuan.tools.common.exception.EmailException; import com.sun.mail.util.MailConnectException; +import com.sun.net.ssl.internal.ssl.Provider; +import org.apache.commons.collections4.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; -import javax.mail.*; -import javax.mail.internet.*; +import javax.mail.AuthenticationFailedException; +import javax.mail.Authenticator; +import javax.mail.BodyPart; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Multipart; +import javax.mail.PasswordAuthentication; +import javax.mail.SendFailedException; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import javax.mail.internet.MimeUtility; import java.io.File; import java.io.UnsupportedEncodingException; import java.security.Security; -import java.util.List; +import java.util.Objects; import java.util.Properties; /** @@ -26,201 +37,61 @@ import java.util.Properties; *

* Created by clive at 2018/07/23. * Rebuilt by clive at 2018/11/26. + * Rebuilt by clive at 2021/06/09. * - * @since 2.0.0 + * @since 4.0.0 */ public class EmailTools { private static final Logger logger = LoggerFactory.getLogger(EmailTools.class); + private static final String CHARSET_UTF_8 = "text/html;charset=utf-8"; private EmailTools() { } - private String host; - private String account; - private String password; - private List recipients = Lists.newArrayList(); - private List carbonCopies = Lists.newArrayList(); - private String subject; - private String content; - private List attachments = Lists.newArrayList(); - private boolean isSSL; - - - // region public methods - - /** - * 构建EmailTools - * - */ - public static EmailTools build() { - return new EmailTools(); - } - /** * 发送邮件 * - * @throws EmailException + * @param param 参数 */ - public void send() throws EmailException { - - //验证 - validate(); - - //判断发送类型 - if (isSSL) - ssl(); - else - normal(); - } - + public static void send(EmailParam param) { - /** - * 发送邮件主机 - * 如: smtp.qq.com; smtp.126.com; - * - * @param host - */ - public EmailTools host(String host) { - this.host = host; - return this; - } + // 验证 + ValidateTools.assertValidateEntity(param); - /** - * 发件人邮箱账号 - * (xxx@qq.com;xxx@126.com) - * - * @param account - */ - public EmailTools account(String account) { - this.account = account; - return this; - } - - /** - * 发件人邮箱密码 - * - * @param password - */ - public EmailTools password(String password) { - this.password = password; - return this; - } - - /** - * 收件人邮箱列表 - * - * @param recipients - */ - public EmailTools recipients(List recipients) { - this.recipients = recipients; - return this; - } - - /** - * 添加收件人邮箱地址 - * - * @param recipient - */ - public EmailTools addRecipient(String recipient) { - this.recipients.add(recipient); - return this; - } - - /** - * 抄送人邮箱地址 - * - * @param carbonCopies - */ - public EmailTools carbonCopies(List carbonCopies) { - this.carbonCopies = carbonCopies; - return this; - } - - /** - * 添加抄送人邮箱地址 - * - * @param carbonCopie - */ - public EmailTools addCarbonCopie(String carbonCopie) { - this.carbonCopies.add(carbonCopie); - return this; - } - - /** - * 邮箱主题 - * - * @param subject - */ - public EmailTools subject(String subject) { - this.subject = subject; - return this; - } - - /** - * 邮件内容 - * - * @param content - */ - public EmailTools content(String content) { - this.content = content; - return this; - } - - /** - * 附件文件列表 - * - * @param attachments - */ - public EmailTools attachments(List attachments) { - this.attachments = attachments; - return this; - } - - /** - * 添加附件文件 - * - * @param attachment - */ - public EmailTools addAttachment(File attachment) { - this.attachments.add(attachment); - return this; - } - - /** - * 通过SSL发送 - * - * @param SSL - */ - public EmailTools isSSL(boolean SSL) { - isSSL = SSL; - return this; + // 判断发送类型 + if (param.isSSL()) { + ssl(param); + } else { + normal(param); + } } - // endregion public methods - // region private methods - private void normal() throws EmailException { + private static void normal(EmailParam param) { try { Properties props = new Properties(); // 发信的主机 - props.put("mail.smtp.host", host); + props.put("mail.smtp.host", param.getHost()); props.put("mail.smtp.auth", "true"); + if (Objects.nonNull(param.getPort())) { + props.setProperty("mail.smtp.port", param.getPort().toString()); + } Session session = Session.getInstance(props); // 调试开关 session.setDebug(false); MimeMessage message = new MimeMessage(session); // 给消息对象设置发件人/收件人/主题/发信时间 - InternetAddress from = new InternetAddress(account); + InternetAddress from = new InternetAddress(param.getAccount()); message.setFrom(from); - config(message); + config(message, param); message.saveChanges(); try (Transport transport = session.getTransport("smtp")) { - transport.connect(host, account, password); + transport.connect(param.getHost(), param.getAccount(), param.getPassword()); transport.sendMessage(message, message.getAllRecipients()); } } catch (MailConnectException e) { @@ -235,17 +106,20 @@ public class EmailTools { } } - private void ssl() throws EmailException { + private static void ssl(EmailParam param) { try { // 设置SSL连接、邮件环境 - Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); - final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; - Properties props = System.getProperties(); - props.setProperty("mail.smtp.host", host); - props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); + Security.addProvider(new Provider()); + Properties props = new Properties(); + props.setProperty("mail.smtp.host", param.getHost()); + String port = "465"; + if (Objects.nonNull(param.getPort())) { + port = Integer.toString(param.getPort()); + } + props.setProperty("mail.smtp.port", port); + props.setProperty("mail.smtp.socketFactory.port", port); + props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.socketFactory.fallback", "false"); - props.setProperty("mail.smtp.port", "465"); - props.setProperty("mail.smtp.socketFactory.port", "465"); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.ssl.enable", "true"); // 建立邮件会话 @@ -253,13 +127,13 @@ public class EmailTools { // 身份认证 @Override protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(account, password); + return new PasswordAuthentication(param.getAccount(), param.getPassword()); } }); - session.setDebug(false); + session.setDebug(param.isDebug()); MimeMessage message = new MimeMessage(session); - message.setFrom(new InternetAddress(account)); - config(message); + message.setFrom(new InternetAddress(param.getAccount())); + config(message, param); message.saveChanges(); Transport.send(message); } catch (MailConnectException e) { @@ -274,18 +148,9 @@ public class EmailTools { } } - private void validate() { - Assert.notEmpty(host, "host can't be empty"); - Assert.notEmpty(account, "account can't be empty"); - Assert.notEmpty(password, "password can't be empty"); - Assert.notEmpty(recipients, "recipients can't be empty"); - Assert.notEmpty(subject, "subject can't be empty"); - Assert.notEmpty(content, "content can't be empty"); - } - - private void config(MimeMessage message) throws MessagingException, UnsupportedEncodingException { + private static void config(MimeMessage message, EmailParam param) throws MessagingException, UnsupportedEncodingException { // 收件人 - for (String recipient : recipients) { + for (String recipient : param.getRecipients()) { if (StringTools.isNotBlank(recipient)) { InternetAddress to = new InternetAddress(recipient); message.addRecipient(Message.RecipientType.TO, to); @@ -293,24 +158,24 @@ public class EmailTools { } // 抄送 - if (CollectionTools.isNotEmpty(carbonCopies)) { - for (String carbonCopy : carbonCopies) { + if (CollectionUtils.isNotEmpty(param.getCarbonCopies())) { + for (String carbonCopy : param.getCarbonCopies()) { if (StringTools.isNotBlank(carbonCopy)) { message.addRecipient(Message.RecipientType.CC, new InternetAddress(carbonCopy)); } } } - message.setSubject(subject); + message.setSubject(param.getSubject()); message.setSentDate(DateTimeTools.now()); BodyPart mbp = new MimeBodyPart(); - mbp.setContent(content, "text/html;charset=utf-8"); + mbp.setContent(param.getContent(), CHARSET_UTF_8); Multipart mm = new MimeMultipart(); mm.addBodyPart(mbp); message.setContent(mm); - if (CollectionTools.isNotEmpty(attachments)) { - for (File attachment : attachments) { + if (CollectionUtils.isNotEmpty(param.getAttachments())) { + for (File attachment : param.getAttachments()) { mbp = new MimeBodyPart(); DataSource source = new FileDataSource(attachment); mbp.setDataHandler(new DataHandler(source)); diff --git a/src/main/java/com/gitee/cliveyuan/tools/FileTools.java b/common/src/main/java/cn/cliveyuan/tools/common/FileTools.java similarity index 91% rename from src/main/java/com/gitee/cliveyuan/tools/FileTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/FileTools.java index 8408d3a..b57ad44 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/FileTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/FileTools.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; import org.apache.commons.io.FileUtils; import org.apache.commons.io.LineIterator; @@ -24,8 +24,8 @@ public class FileTools extends FileUtils { } private static void validate(File file) { - Assert.notNull(file, "file can't be null"); - Assert.isTrue(file.exists(), "file not exists: " + file.getAbsolutePath()); + AssertTools.notNull(file, "file can't be null"); + AssertTools.isTrue(file.exists(), "file not exists: " + file.getAbsolutePath()); } /** diff --git a/src/main/java/com/gitee/cliveyuan/tools/GenericTypeTools.java b/common/src/main/java/cn/cliveyuan/tools/common/GenericTypeTools.java similarity index 94% rename from src/main/java/com/gitee/cliveyuan/tools/GenericTypeTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/GenericTypeTools.java index 1986e3a..4ecc7d2 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/GenericTypeTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/GenericTypeTools.java @@ -1,7 +1,6 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; import lombok.extern.slf4j.Slf4j; -import org.apache.poi.ss.formula.functions.T; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; diff --git a/src/main/java/com/gitee/cliveyuan/tools/IDCardTools.java b/common/src/main/java/cn/cliveyuan/tools/common/IDCardTools.java similarity index 99% rename from src/main/java/com/gitee/cliveyuan/tools/IDCardTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/IDCardTools.java index 1b1f366..fdf4f3a 100755 --- a/src/main/java/com/gitee/cliveyuan/tools/IDCardTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/IDCardTools.java @@ -3,7 +3,7 @@ * Copyright (c) 2017-2018 All Rights Reserved. */ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; import java.util.Calendar; diff --git a/src/main/java/com/gitee/cliveyuan/tools/IdTools.java b/common/src/main/java/cn/cliveyuan/tools/common/IdTools.java similarity index 79% rename from src/main/java/com/gitee/cliveyuan/tools/IdTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/IdTools.java index 2d26238..8e262d4 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/IdTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/IdTools.java @@ -1,6 +1,7 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; -import com.gitee.cliveyuan.tools.enums.DateTimeFormat; +import cn.cliveyuan.tools.common.enums.DateTimeFormat; +import org.apache.commons.lang3.RandomUtils; import java.util.UUID; @@ -39,7 +40,7 @@ public class IdTools { */ public static String genOrderNo() { String dateTime = DateTimeTools.format(DateTimeTools.now(), DateTimeFormat.DATE_TIME_0); - int randomInt = MathTools.randomInt(100000, 999999); + int randomInt = RandomUtils.nextInt(100000, 999999); return dateTime + randomInt; } } diff --git a/src/main/java/com/gitee/cliveyuan/tools/codec/MD5Tools.java b/common/src/main/java/cn/cliveyuan/tools/common/MD5Tools.java similarity index 78% rename from src/main/java/com/gitee/cliveyuan/tools/codec/MD5Tools.java rename to common/src/main/java/cn/cliveyuan/tools/common/MD5Tools.java index f6bfa46..c0524ea 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/codec/MD5Tools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/MD5Tools.java @@ -1,11 +1,7 @@ -package com.gitee.cliveyuan.tools.codec; +package cn.cliveyuan.tools.common; import org.apache.commons.codec.digest.DigestUtils; -import java.math.BigInteger; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - /** * MD5工具 * diff --git a/common/src/main/java/cn/cliveyuan/tools/common/MapTools.java b/common/src/main/java/cn/cliveyuan/tools/common/MapTools.java new file mode 100644 index 0000000..1e92c58 --- /dev/null +++ b/common/src/main/java/cn/cliveyuan/tools/common/MapTools.java @@ -0,0 +1,28 @@ +package cn.cliveyuan.tools.common; + +import java.util.Map; + +/** + * @author Clive Yuan + * @date 2021/06/08 + */ +public class MapTools { + /** + * map是否为空 + * + * @param map map + */ + public static boolean isEmpty(Map map) { + return map == null || map.isEmpty(); + } + + /** + * map是否不为空 + * + * @param map map + */ + public static boolean isNotEmpty(Map map) { + return !isEmpty(map); + } + +} diff --git a/src/main/java/com/gitee/cliveyuan/tools/codec/RSATools.java b/common/src/main/java/cn/cliveyuan/tools/common/RSATools.java similarity index 88% rename from src/main/java/com/gitee/cliveyuan/tools/codec/RSATools.java rename to common/src/main/java/cn/cliveyuan/tools/common/RSATools.java index 046cc67..f6c9f1a 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/codec/RSATools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/RSATools.java @@ -1,14 +1,12 @@ -package com.gitee.cliveyuan.tools.codec; +package cn.cliveyuan.tools.common; -import com.gitee.cliveyuan.tools.Assert; -import com.gitee.cliveyuan.tools.bean.rsa.GenerateKeyPairReq; -import com.gitee.cliveyuan.tools.bean.rsa.RSAKeyPair; -import com.gitee.cliveyuan.tools.bean.rsa.RSARequest; -import com.gitee.cliveyuan.tools.bean.rsa.RSASignReq; -import com.gitee.cliveyuan.tools.bean.rsa.RSAVerifySignReq; +import cn.cliveyuan.tools.common.bean.rsa.GenerateKeyPairReq; +import cn.cliveyuan.tools.common.bean.rsa.RSAKeyPair; +import cn.cliveyuan.tools.common.bean.rsa.RSARequest; +import cn.cliveyuan.tools.common.bean.rsa.RSASignReq; +import cn.cliveyuan.tools.common.bean.rsa.RSAVerifySignReq; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; -import org.bouncycastle.jce.provider.BouncyCastleProvider; import javax.crypto.Cipher; import java.nio.charset.StandardCharsets; @@ -19,7 +17,6 @@ import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; -import java.security.Security; import java.security.Signature; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; @@ -62,7 +59,7 @@ public class RSATools { * @throws NoSuchAlgorithmException */ public static RSAKeyPair generateKeyPair(GenerateKeyPairReq generateKeyPairReq) throws NoSuchAlgorithmException { - Assert.notNull(generateKeyPairReq); + AssertTools.notNull(generateKeyPairReq); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(Objects.nonNull(generateKeyPairReq.getKeyAlgorithm()) ? generateKeyPairReq.getKeyAlgorithm() : DEFAULT_ALGORITHM); keyPairGenerator.initialize(Objects.nonNull(generateKeyPairReq.getKeySize()) ? @@ -99,10 +96,10 @@ public class RSATools { * @return */ public static boolean verifySign(RSAVerifySignReq rsaVerifySignReq) { - Assert.notNull(rsaVerifySignReq); - Assert.notNull(rsaVerifySignReq.getData(), "data is required"); - Assert.notNull(rsaVerifySignReq.getSign(), "sign is required"); - Assert.notNull(rsaVerifySignReq.getPublicKeyString(), "publicKeyString is required"); + AssertTools.notNull(rsaVerifySignReq); + AssertTools.notNull(rsaVerifySignReq.getData(), "data is required"); + AssertTools.notNull(rsaVerifySignReq.getSign(), "sign is required"); + AssertTools.notNull(rsaVerifySignReq.getPublicKeyString(), "publicKeyString is required"); try { KeyFactory keyFactory = KeyFactory.getInstance(Objects.nonNull(rsaVerifySignReq.getKeyAlgorithm()) ? rsaVerifySignReq.getKeyAlgorithm() : DEFAULT_ALGORITHM); @@ -139,9 +136,9 @@ public class RSATools { * @return */ public static String sign(RSASignReq rsaSignReq) { - Assert.notNull(rsaSignReq); - Assert.notNull(rsaSignReq.getData(), "data is required"); - Assert.notNull(rsaSignReq.getPrivateKeyString(), "privateKeyString is required"); + AssertTools.notNull(rsaSignReq); + AssertTools.notNull(rsaSignReq.getData(), "data is required"); + AssertTools.notNull(rsaSignReq.getPrivateKeyString(), "privateKeyString is required"); try { byte[] keyBytes = Base64.decodeBase64(rsaSignReq.getPrivateKeyString().getBytes()); // 构造PKCS8EncodedKeySpec对象 @@ -170,9 +167,9 @@ public class RSATools { * @return */ public static byte[] encrypt(RSARequest rsaRequest) { - Assert.notNull(rsaRequest); - Assert.notNull(rsaRequest.getData(), "data is required"); - Assert.notNull(rsaRequest.getKeyString(), "publicKeyString is required"); + AssertTools.notNull(rsaRequest); + AssertTools.notNull(rsaRequest.getData(), "data is required"); + AssertTools.notNull(rsaRequest.getKeyString(), "publicKeyString is required"); try { // 对公钥解密 byte[] keyBytes = decodeBase64(rsaRequest.getKeyString()); @@ -229,10 +226,9 @@ public class RSATools { * @return */ public static byte[] decrypt(RSARequest rsaRequest) { - Security.addProvider(new BouncyCastleProvider()); - Assert.notNull(rsaRequest); - Assert.notNull(rsaRequest.getData(), "data is required"); - Assert.notNull(rsaRequest.getKeyString(), "privateKeyString is required"); + AssertTools.notNull(rsaRequest); + AssertTools.notNull(rsaRequest.getData(), "data is required"); + AssertTools.notNull(rsaRequest.getKeyString(), "privateKeyString is required"); try { // 对密钥解密 byte[] keyBytes = decodeBase64(rsaRequest.getKeyString()); diff --git a/src/main/java/com/gitee/cliveyuan/tools/StringTools.java b/common/src/main/java/cn/cliveyuan/tools/common/StringTools.java similarity index 69% rename from src/main/java/com/gitee/cliveyuan/tools/StringTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/StringTools.java index 379e873..66316ae 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/StringTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/StringTools.java @@ -1,13 +1,12 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; -import com.gitee.cliveyuan.tools.text.StrFormatter; -import com.google.common.collect.Maps; import org.apache.commons.lang3.StringUtils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.Arrays; +import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.regex.Matcher; @@ -23,6 +22,9 @@ import java.util.regex.Pattern; public class StringTools extends StringUtils { private static final Pattern PATTERN = Pattern.compile("(\\\\u(\\p{XDigit}{4}))"); + public static final String EMPTY_JSON = "{}"; + public static final char C_BACKSLASH = '\\'; + public static final char C_DELIM_START = '{'; private StringTools() { } @@ -111,7 +113,7 @@ public class StringTools extends StringUtils { * @param data */ public static String map2QueryStr(Map data) { - if (CollectionTools.isEmpty(data)) { + if (MapTools.isEmpty(data)) { return null; } StringBuilder sb = new StringBuilder(); @@ -128,7 +130,7 @@ public class StringTools extends StringUtils { } public static Map queryStr2Map(String query) { - Map map = Maps.newHashMap(); + Map map = new HashMap(); if (isBlank(query)) { return map; } @@ -151,7 +153,7 @@ public class StringTools extends StringUtils { * @param data */ public static String map2CookieStr(Map data) { - if (CollectionTools.isEmpty(data)) { + if (MapTools.isEmpty(data)) { return null; } StringBuilder sb = new StringBuilder(); @@ -185,7 +187,7 @@ public class StringTools extends StringUtils { * @param str */ public static Map str2Map(String str) { - Map map = Maps.newHashMap(); + Map map = new HashMap(); if (Objects.isNull(str)) { return map; } @@ -240,9 +242,9 @@ public class StringTools extends StringUtils { */ public static String[] between(final String value, final String start, final String end) { - Assert.notNull(value, "'value' should be not null."); - Assert.notNull(start, "'start' should be not null."); - Assert.notNull(end, "'end' should be not null."); + AssertTools.notNull(value, "'value' should be not null."); + AssertTools.notNull(start, "'start' should be not null."); + AssertTools.notNull(end, "'end' should be not null."); String[] parts = value.split(end); return Arrays.stream(parts) .filter(subPart -> subPart.contains(start)) @@ -259,14 +261,14 @@ public class StringTools extends StringUtils { */ public static String firstBetween(final String value, final String start, final String end) { String[] betweenArray = between(value, start, end); - if (CollectionTools.isNotEmpty(betweenArray)) { + if (ArrayTools.isNotEmpty(betweenArray)) { return betweenArray[0]; } return EMPTY; } /** - * 格式化文本, {} 表示占位符
+ * 格式化字符串
* 此方法只是简单将占位符 {} 按照顺序替换为参数
* 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可
* 例:
@@ -274,15 +276,56 @@ public class StringTools extends StringUtils { * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a
* 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b
* - * @param template 文本模板,被替换的部分用 {} 表示 - * @param params 参数值 - * @return 格式化后的文本 + * @param strPattern 字符串模板 + * @param argArray 参数列表 + * @return 结果 */ - public static String format(String template, Object... params) { - if (ArrayTools.isEmpty(params) || isEmpty(template)) { - return template; + public static String format(final String strPattern, final Object... argArray) { + if (StringTools.isEmpty(strPattern) || ArrayTools.isEmpty(argArray)) { + return strPattern; + } + final int strPatternLength = strPattern.length(); + + // 初始化定义好的长度以获得更好的性能 + StringBuilder sbuf = new StringBuilder(strPatternLength + 50); + + int handledPosition = 0; + int delimIndex;// 占位符所在位置 + for (int argIndex = 0; argIndex < argArray.length; argIndex++) { + delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition); + if (delimIndex == -1) { + if (handledPosition == 0) { + return strPattern; + } else { // 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果 + sbuf.append(strPattern, handledPosition, strPatternLength); + return sbuf.toString(); + } + } else { + if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH) { + if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH) { + // 转义符之前还有一个转义符,占位符依旧有效 + sbuf.append(strPattern, handledPosition, delimIndex - 1); + sbuf.append(argArray[argIndex]); + handledPosition = delimIndex + 2; + } else { + // 占位符被转义 + argIndex--; + sbuf.append(strPattern, handledPosition, delimIndex - 1); + sbuf.append(C_DELIM_START); + handledPosition = delimIndex + 1; + } + } else { + // 正常占位符 + sbuf.append(strPattern, handledPosition, delimIndex); + sbuf.append(argArray[argIndex]); + handledPosition = delimIndex + 2; + } + } } - return StrFormatter.format(template, params); + // 加入最后一个占位符后所有的字符 + sbuf.append(strPattern, handledPosition, strPattern.length()); + + return sbuf.toString(); } /** @@ -379,5 +422,31 @@ public class StringTools extends StringUtils { return str.substring(start, end); } + /** + * 删除字符串中的html标签 + * + * @param htmlStr 字符串 + * @return + */ + public static String delHtmlTag(String htmlStr) { + String regEx_script = "]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 + String regEx_style = "]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 + String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式 + + Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); + Matcher m_script = p_script.matcher(htmlStr); + htmlStr = m_script.replaceAll(""); //过滤script标签 + + Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); + Matcher m_style = p_style.matcher(htmlStr); + htmlStr = m_style.replaceAll(""); //过滤style标签 + + Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); + Matcher m_html = p_html.matcher(htmlStr); + htmlStr = m_html.replaceAll(""); //过滤html标签 + + return htmlStr.trim(); //返回文本字符串 + } + } diff --git a/src/main/java/com/gitee/cliveyuan/tools/data/TableUtils.java b/common/src/main/java/cn/cliveyuan/tools/common/TableUtils.java similarity index 44% rename from src/main/java/com/gitee/cliveyuan/tools/data/TableUtils.java rename to common/src/main/java/cn/cliveyuan/tools/common/TableUtils.java index 4f410ae..61c5b38 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/data/TableUtils.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/TableUtils.java @@ -1,28 +1,15 @@ -package com.gitee.cliveyuan.tools.data; +package cn.cliveyuan.tools.common; -import com.gitee.cliveyuan.tools.Assert; -import com.gitee.cliveyuan.tools.CollectionTools; -import com.gitee.cliveyuan.tools.StringTools; -import com.gitee.cliveyuan.tools.bean.excel.ExcelGenerateParam; -import com.gitee.cliveyuan.tools.bean.excel.SheetContent; -import com.gitee.cliveyuan.tools.enums.ExcelType; -import com.gitee.cliveyuan.tools.exception.ExcelException; import lombok.extern.slf4j.Slf4j; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.beans.PropertyDescriptor; -import java.io.File; -import java.io.FileOutputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.math.BigDecimal; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; /** * Created by Clive on 2019/10/28. @@ -82,76 +69,4 @@ public class TableUtils { } return list; } - - public static File generate(ExcelGenerateParam param) { - String filePath = param.getFilePath(); - ExcelType excelType = param.getExcelType(); - Assert.notBlank(filePath, "file path can't be empty"); - Assert.notNull(excelType, "excel type is required"); - Assert.notEmpty(param.getSheetContentList(), "sheetContentList can't be empty"); - - File file = new File(filePath); - if (!file.exists()) { - boolean result = file.mkdirs(); - if (!result) throw ExcelException.failToMkdirs(); - } - String fileName = System.currentTimeMillis() + "." + excelType.name().toLowerCase(); - if (!filePath.endsWith("/")) filePath = filePath + "/"; - file = new File(filePath + fileName); - try (FileOutputStream fos = new FileOutputStream(file)) { - Workbook wb; - if (excelType.equals(ExcelType.XLS)) wb = new HSSFWorkbook(); - else wb = new XSSFWorkbook(); - - for (SheetContent sheetContent : param.getSheetContentList()) { - String[] headers = sheetContent.getHeaders(); - Collection data = sheetContent.getData(); - Assert.isTrue(CollectionTools.isNotEmpty(headers), "headers can't be empty"); - Assert.isTrue(CollectionTools.isNotEmpty(data), "data can't be empty"); - - Sheet sheet = wb.createSheet(sheetContent.getSheetName()); - Row firstRow = sheet.createRow(0); - //创建表头 - for (int i = 0; i < headers.length; i++) { - Cell cell = firstRow.createCell(i); - cell.setCellValue(headers[i]); - } - - //处理数据 - Iterator it = data.iterator(); - int index = 0; - while (it.hasNext()) { - index++; - Row row = sheet.createRow(index); - Object t = it.next(); - //反射 - Class clazz = t.getClass(); - Field[] fields = clazz.getDeclaredFields(); - for (int i = 0; i < fields.length; i++) { - Field field = fields[i]; - PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz); - Method getMethod = pd.getReadMethod(); - if (getMethod != null) { - Object invoke = getMethod.invoke(t); - String value = StringTools.EMPTY; - if (invoke != null) { - if (invoke instanceof Date) { - value = TableUtils.SDF.format((Date) invoke); - } else value = invoke.toString(); - } - Cell cell = row.createCell(i); - cell.setCellValue(value); - } - - } - } - } - wb.write(fos); - return file; - } catch (Exception e) { - log.error("excel generate exception", e); - throw ExcelException.failToGenerateExcel(); - } - - } } diff --git a/src/main/java/com/gitee/cliveyuan/tools/ThreadTools.java b/common/src/main/java/cn/cliveyuan/tools/common/ThreadTools.java similarity index 95% rename from src/main/java/com/gitee/cliveyuan/tools/ThreadTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/ThreadTools.java index a4bf3be..cce0a25 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/ThreadTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/ThreadTools.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; /** * 线程工具 diff --git a/src/main/java/com/gitee/cliveyuan/tools/ValidateTools.java b/common/src/main/java/cn/cliveyuan/tools/common/ValidateTools.java similarity index 67% rename from src/main/java/com/gitee/cliveyuan/tools/ValidateTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/ValidateTools.java index 030575a..5439a94 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/ValidateTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/ValidateTools.java @@ -1,6 +1,16 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; +import cn.cliveyuan.tools.common.bean.ValidationResult; +import org.apache.commons.collections4.CollectionUtils; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.groups.Default; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.regex.Pattern; /** @@ -15,6 +25,8 @@ public class ValidateTools { private ValidateTools() { } + private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator(); + /** * 正则匹配 * @@ -221,4 +233,54 @@ public class ValidateTools { public static boolean isIdCardNo(String text) { return IDCardTools.isIDCard(text); } + + public static void assertValidateEntity(T obj) { + ValidationResult validationResult = ValidateTools.validateEntity(obj); + AssertTools.isTrue(!validationResult.isHasErrors(), validationResult.getMessage()); + } + /** + * 校验实体,返回实体所有属性的校验结果 + * + * @param obj + * @param + * @return + */ + public static ValidationResult validateEntity(T obj) { + //解析校验结果 + Set> validateSet = VALIDATOR.validate(obj, Default.class); + return buildValidationResult(validateSet); + } + + /** + * 校验指定实体的指定属性是否存在异常 + * + * @param obj + * @param propertyName + * @param + * @return + */ + public static ValidationResult validateProperty(T obj, String propertyName) { + Set> validateSet = VALIDATOR.validateProperty(obj, propertyName, Default.class); + return buildValidationResult(validateSet); + } + + /** + * 将异常结果封装返回 + * + * @param validateSet + * @param + * @return + */ + private static ValidationResult buildValidationResult(Set> validateSet) { + ValidationResult validationResult = new ValidationResult(); + if (CollectionUtils.isNotEmpty(validateSet)) { + validationResult.setHasErrors(true); + Map errorMsgMap = new HashMap<>(); + for (ConstraintViolation constraintViolation : validateSet) { + errorMsgMap.put(constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage()); + } + validationResult.setErrorMsg(errorMsgMap); + } + return validationResult; + } } diff --git a/src/main/java/com/gitee/cliveyuan/tools/ZipTools.java b/common/src/main/java/cn/cliveyuan/tools/common/ZipTools.java similarity index 94% rename from src/main/java/com/gitee/cliveyuan/tools/ZipTools.java rename to common/src/main/java/cn/cliveyuan/tools/common/ZipTools.java index 9f24473..c720117 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/ZipTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/ZipTools.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.common; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; @@ -19,8 +19,6 @@ import java.util.Objects; */ public class ZipTools { - private static final Logger logger = LoggerFactory.getLogger(ZipTools.class); - private ZipTools() { } diff --git a/common/src/main/java/cn/cliveyuan/tools/common/bean/EmailParam.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/EmailParam.java new file mode 100644 index 0000000..79b90cf --- /dev/null +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/EmailParam.java @@ -0,0 +1,72 @@ +package cn.cliveyuan.tools.common.bean; + +import lombok.Builder; +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotEmpty; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Clive Yuan + * @date 2021/06/09 + */ +@Data +@Builder +public class EmailParam { + /** + * 邮件发送主机, 如 smtp.exmail.qq.com + */ + @NotBlank + private String host; + /** + * 发送端口 + */ + private Integer port; + /** + * 账号 + */ + @NotBlank + private String account; + /** + * 密码 + */ + @NotBlank + private String password; + /** + * 收件人 + */ + @Builder.Default + @NotEmpty + private List recipients = new ArrayList<>(); + /** + * 抄送人 + */ + @Builder.Default + private List carbonCopies = new ArrayList<>(); + /** + * 主题 + */ + @NotBlank + private String subject; + /** + * 内容 + */ + @NotBlank + private String content; + /** + * 附件 + */ + @Builder.Default + private List attachments = new ArrayList<>(); + /** + * 通过SSL发送 + */ + private boolean isSSL; + /** + * 调试 + */ + private boolean debug; +} diff --git a/common/src/main/java/cn/cliveyuan/tools/common/bean/ValidationResult.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/ValidationResult.java new file mode 100644 index 0000000..b178a98 --- /dev/null +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/ValidationResult.java @@ -0,0 +1,64 @@ +package cn.cliveyuan.tools.common.bean; + +import org.apache.commons.lang3.StringUtils; + +import java.text.MessageFormat; +import java.util.Map; + +/** + * @author Clive Yuan + * @date 2021/06/09 + */ +public class ValidationResult { + + /** + * 是否有异常 + */ + private boolean hasErrors; + + /** + * 异常消息记录 + */ + private Map errorMsg; + + /** + * 获取异常消息组装 + * + * @return + */ + public String getMessage() { + if (errorMsg == null || errorMsg.isEmpty()) { + return StringUtils.EMPTY; + } + StringBuilder message = new StringBuilder(); + errorMsg.forEach((key, value) -> { + message.append(MessageFormat.format("{0}:{1} \r\n", key, value)); + }); + return message.toString(); + } + + + public boolean isHasErrors() { + return hasErrors; + } + + public void setHasErrors(boolean hasErrors) { + this.hasErrors = hasErrors; + } + + public Map getErrorMsg() { + return errorMsg; + } + + public void setErrorMsg(Map errorMsg) { + this.errorMsg = errorMsg; + } + + @Override + public String toString() { + return "ValidationResult{" + + "hasErrors=" + hasErrors + + ", errorMsg=" + errorMsg + + '}'; + } +} diff --git a/src/main/java/com/gitee/cliveyuan/tools/data/csv/CsvReader.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/csv/CsvReader.java similarity index 93% rename from src/main/java/com/gitee/cliveyuan/tools/data/csv/CsvReader.java rename to common/src/main/java/cn/cliveyuan/tools/common/bean/csv/CsvReader.java index c5e2d1f..62f3be6 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/data/csv/CsvReader.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/csv/CsvReader.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.data.csv; +package cn.cliveyuan.tools.common.bean.csv; import lombok.Builder; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/data/csv/CsvWriter.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/csv/CsvWriter.java similarity index 79% rename from src/main/java/com/gitee/cliveyuan/tools/data/csv/CsvWriter.java rename to common/src/main/java/cn/cliveyuan/tools/common/bean/csv/CsvWriter.java index 8138a09..45a108c 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/data/csv/CsvWriter.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/csv/CsvWriter.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.data.csv; +package cn.cliveyuan.tools.common.bean.csv; import lombok.Builder; import lombok.Data; @@ -25,7 +25,11 @@ public class CsvWriter implements Serializable { /** * 导出的文件路径 */ - private String filePath; + private String pathname; + /** + * 文件名, 不指定则随机生成 + */ + private String fileName; /** * 编码格式: 默认UTF-8 */ diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/rsa/GenerateKeyPairReq.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/GenerateKeyPairReq.java similarity index 88% rename from src/main/java/com/gitee/cliveyuan/tools/bean/rsa/GenerateKeyPairReq.java rename to common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/GenerateKeyPairReq.java index a1aa78f..56b7db1 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/rsa/GenerateKeyPairReq.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/GenerateKeyPairReq.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean.rsa; +package cn.cliveyuan.tools.common.bean.rsa; import lombok.Builder; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSAKeyPair.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSAKeyPair.java similarity index 84% rename from src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSAKeyPair.java rename to common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSAKeyPair.java index 2064ea0..09c79c7 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSAKeyPair.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSAKeyPair.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean.rsa; +package cn.cliveyuan.tools.common.bean.rsa; import lombok.Builder; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSARequest.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSARequest.java similarity index 92% rename from src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSARequest.java rename to common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSARequest.java index 6ef544c..2275104 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSARequest.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSARequest.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean.rsa; +package cn.cliveyuan.tools.common.bean.rsa; import lombok.Builder; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSASignReq.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSASignReq.java similarity index 94% rename from src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSASignReq.java rename to common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSASignReq.java index a5164a0..096042d 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSASignReq.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSASignReq.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean.rsa; +package cn.cliveyuan.tools.common.bean.rsa; import lombok.Builder; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSAVerifySignReq.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSAVerifySignReq.java similarity index 94% rename from src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSAVerifySignReq.java rename to common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSAVerifySignReq.java index b7fb039..95bcad3 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/rsa/RSAVerifySignReq.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSAVerifySignReq.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean.rsa; +package cn.cliveyuan.tools.common.bean.rsa; import lombok.Builder; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/enums/CaptchaGenerateType.java b/common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaGenerateType.java similarity index 80% rename from src/main/java/com/gitee/cliveyuan/tools/enums/CaptchaGenerateType.java rename to common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaGenerateType.java index 1123a53..f21546f 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/enums/CaptchaGenerateType.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaGenerateType.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.enums; +package cn.cliveyuan.tools.common.enums; /** * Created by Clive on 2019/06/16. diff --git a/src/main/java/com/gitee/cliveyuan/tools/enums/CaptchaType.java b/common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaType.java similarity index 82% rename from src/main/java/com/gitee/cliveyuan/tools/enums/CaptchaType.java rename to common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaType.java index da0539e..46f7617 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/enums/CaptchaType.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaType.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.enums; +package cn.cliveyuan.tools.common.enums; /** * 验证码类型 diff --git a/src/main/java/com/gitee/cliveyuan/tools/enums/DateTimeField.java b/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeField.java similarity index 83% rename from src/main/java/com/gitee/cliveyuan/tools/enums/DateTimeField.java rename to common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeField.java index d5c86b6..81e7f51 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/enums/DateTimeField.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeField.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.enums; +package cn.cliveyuan.tools.common.enums; /** * 日期时间字段常量 diff --git a/src/main/java/com/gitee/cliveyuan/tools/enums/DateTimeFormat.java b/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeFormat.java similarity index 94% rename from src/main/java/com/gitee/cliveyuan/tools/enums/DateTimeFormat.java rename to common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeFormat.java index 93f4278..afaceb9 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/enums/DateTimeFormat.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeFormat.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.enums; +package cn.cliveyuan.tools.common.enums; /** * 日期时间格式常量 diff --git a/src/main/java/com/gitee/cliveyuan/tools/exception/BaseException.java b/common/src/main/java/cn/cliveyuan/tools/common/exception/BaseException.java similarity index 91% rename from src/main/java/com/gitee/cliveyuan/tools/exception/BaseException.java rename to common/src/main/java/cn/cliveyuan/tools/common/exception/BaseException.java index a5a5018..b86d25f 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/exception/BaseException.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/exception/BaseException.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.exception; +package cn.cliveyuan.tools.common.exception; /** * 基础异常 diff --git a/src/main/java/com/gitee/cliveyuan/tools/exception/EmailException.java b/common/src/main/java/cn/cliveyuan/tools/common/exception/EmailException.java similarity index 94% rename from src/main/java/com/gitee/cliveyuan/tools/exception/EmailException.java rename to common/src/main/java/cn/cliveyuan/tools/common/exception/EmailException.java index 331aff2..cb1dcf3 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/exception/EmailException.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/exception/EmailException.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.exception; +package cn.cliveyuan.tools.common.exception; /** * 邮件异常 diff --git a/src/main/resources/fastjson.properties b/common/src/main/resources/fastjson.properties similarity index 100% rename from src/main/resources/fastjson.properties rename to common/src/main/resources/fastjson.properties diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/AESToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/AESToolsTest.java similarity index 88% rename from src/test/java/com/gitee/cliveyuan/tools/test/AESToolsTest.java rename to common/src/test/java/cn/cliveyuan/tools/common/test/AESToolsTest.java index 8459a3f..0092cf0 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/AESToolsTest.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/AESToolsTest.java @@ -1,6 +1,6 @@ -package com.gitee.cliveyuan.tools.test; +package cn.cliveyuan.tools.common.test; -import com.gitee.cliveyuan.tools.codec.AESTools; +import cn.cliveyuan.tools.common.AESTools; import org.junit.Assert; import org.junit.Test; diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/CsvToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/CsvToolsTest.java similarity index 79% rename from src/test/java/com/gitee/cliveyuan/tools/test/CsvToolsTest.java rename to common/src/test/java/cn/cliveyuan/tools/common/test/CsvToolsTest.java index d6638e2..823a53a 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/CsvToolsTest.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/CsvToolsTest.java @@ -1,12 +1,12 @@ -package com.gitee.cliveyuan.tools.test; +package cn.cliveyuan.tools.common.test; +import cn.cliveyuan.tools.common.CsvTools; +import cn.cliveyuan.tools.common.DateTimeTools; +import cn.cliveyuan.tools.common.bean.csv.CsvReader; +import cn.cliveyuan.tools.common.bean.csv.CsvWriter; +import cn.cliveyuan.tools.common.test.bean.Person; import com.alibaba.fastjson.JSON; -import com.gitee.cliveyuan.tools.CsvTools; -import com.gitee.cliveyuan.tools.DateTimeTools; -import com.gitee.cliveyuan.tools.data.csv.CsvReader; -import com.gitee.cliveyuan.tools.data.csv.CsvWriter; -import com.gitee.cliveyuan.tools.test.bean.excel.Person; -import com.google.common.collect.Lists; +import java.util.ArrayList; import org.junit.Assert; import org.junit.Test; @@ -23,7 +23,7 @@ public class CsvToolsTest { @Test public void writeAndRead() { String[] headers = {"姓名", "性别", "年龄", "生日", "身高", "体重", "职位", "国籍", "活了多少秒", "是程序员吗", "月薪"}; - List data = Lists.newArrayList(); + List data = new ArrayList(); for (int i = 0; i < 10; i++) { data.add(Person.build() .Name("张三" + i) @@ -44,7 +44,7 @@ public class CsvToolsTest { File generatedFile = CsvTools.write(CsvWriter.builder() .headers(headers) - .filePath(filePath) + .pathname(filePath) .data(data) .build()); Assert.assertNotNull(generatedFile); diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/DateTimeToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/DateTimeToolsTest.java similarity index 85% rename from src/test/java/com/gitee/cliveyuan/tools/test/DateTimeToolsTest.java rename to common/src/test/java/cn/cliveyuan/tools/common/test/DateTimeToolsTest.java index 929199e..f1951dc 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/DateTimeToolsTest.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/DateTimeToolsTest.java @@ -1,8 +1,8 @@ -package com.gitee.cliveyuan.tools.test; +package cn.cliveyuan.tools.common.test; -import com.gitee.cliveyuan.tools.DateTimeTools; -import com.gitee.cliveyuan.tools.enums.DateTimeField; -import com.gitee.cliveyuan.tools.enums.DateTimeFormat; +import cn.cliveyuan.tools.common.DateTimeTools; +import cn.cliveyuan.tools.common.enums.DateTimeField; +import cn.cliveyuan.tools.common.enums.DateTimeFormat; import org.junit.Test; import java.text.ParseException; diff --git a/common/src/test/java/cn/cliveyuan/tools/common/test/EmailToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/EmailToolsTest.java new file mode 100644 index 0000000..5e3e825 --- /dev/null +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/EmailToolsTest.java @@ -0,0 +1,45 @@ +package cn.cliveyuan.tools.common.test; + +import cn.cliveyuan.tools.common.EmailTools; +import cn.cliveyuan.tools.common.bean.EmailParam; + +import java.util.Collections; + +/** + * @author clive + * Created on 2018/07/24 + * @since 1.0 + */ +public class EmailToolsTest { + + // https://gitee.com/cliveyuan/bid/blob/master/src/main/resources/config/bid.properties + + private static final String HOST = "smtp.exmail.qq.com"; + private static final String ACCOUNT = "system@cliveyuan.cn"; + private static final String PASSWORD = "***"; + + // @Test + public void send() { + EmailTools.send(EmailParam.builder() + .host(HOST) + .account(ACCOUNT) + .password(PASSWORD) + .recipients(Collections.singletonList("cliveyuan@foxmail.com")) + .subject("Email Tools Test") + .content("Hello World!") + .build()); + } + + // @Test + public void sendBySSL() { + EmailTools.send(EmailParam.builder() + .host(HOST) + .account(ACCOUNT) + .password(PASSWORD) + .recipients(Collections.singletonList("cliveyuan@foxmail.com")) + .subject("ssl Email Tools Test") + .content("Hello World!") + .isSSL(true) + .build()); + } +} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/FileToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/FileToolsTest.java similarity index 53% rename from src/test/java/com/gitee/cliveyuan/tools/test/FileToolsTest.java rename to common/src/test/java/cn/cliveyuan/tools/common/test/FileToolsTest.java index f1df717..390e649 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/FileToolsTest.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/FileToolsTest.java @@ -1,13 +1,12 @@ -package com.gitee.cliveyuan.tools.test; +package cn.cliveyuan.tools.common.test; -import com.gitee.cliveyuan.tools.FileTools; -import org.apache.commons.io.FileUtils; +import cn.cliveyuan.tools.common.FileTools; import org.junit.Assert; import org.junit.Test; import java.io.File; import java.io.IOException; -import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.List; /** @@ -20,9 +19,12 @@ public class FileToolsTest { @Test public void readFileToStringList() throws IOException { String rootPath = FileToolsTest.class.getResource("/").getPath(); - List lines = FileTools.readFileToStringList(new File(rootPath + "/read_test.txt")); + String pathname = rootPath + "/read_test.txt"; + File file = new File(pathname); + FileTools.writeStringToFile(file, "a\nb\nc", StandardCharsets.UTF_8); + List lines = FileTools.readFileToStringList(file); lines.forEach(System.out::println); System.out.println("size=" + lines.size()); - Assert.assertEquals(lines.size(), 10); + Assert.assertEquals(lines.size(), 3); } } diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/IdToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/IdToolsTest.java similarity index 76% rename from src/test/java/com/gitee/cliveyuan/tools/test/IdToolsTest.java rename to common/src/test/java/cn/cliveyuan/tools/common/test/IdToolsTest.java index 7fad688..82c76b6 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/IdToolsTest.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/IdToolsTest.java @@ -1,6 +1,6 @@ -package com.gitee.cliveyuan.tools.test; +package cn.cliveyuan.tools.common.test; -import com.gitee.cliveyuan.tools.IdTools; +import cn.cliveyuan.tools.common.IdTools; import org.junit.Test; /** diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/StringToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/StringToolsTest.java similarity index 76% rename from src/test/java/com/gitee/cliveyuan/tools/test/StringToolsTest.java rename to common/src/test/java/cn/cliveyuan/tools/common/test/StringToolsTest.java index 0b41f24..2725ff4 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/StringToolsTest.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/StringToolsTest.java @@ -1,11 +1,11 @@ -package com.gitee.cliveyuan.tools.test; +package cn.cliveyuan.tools.common.test; -import com.gitee.cliveyuan.tools.StringTools; -import com.gitee.cliveyuan.tools.test.bean.excel.Person; -import com.google.common.collect.Maps; +import cn.cliveyuan.tools.common.StringTools; +import cn.cliveyuan.tools.common.test.bean.Person; import org.junit.Assert; import org.junit.Test; +import java.util.HashMap; import java.util.Map; /** @@ -49,7 +49,7 @@ public class StringToolsTest { @Test public void between() { String value = "第8页,共10条"; - String[] between = StringTools.between(value, "共" , "条"); + String[] between = StringTools.between(value, "共", "条"); for (String s : between) { System.out.println(s); } @@ -58,16 +58,16 @@ public class StringToolsTest { @Test public void firstBetween() { String value = "第8页,共10条"; - String result = StringTools.firstBetween(value, "第" , "页"); + String result = StringTools.firstBetween(value, "第", "页"); System.out.println(result); } @Test public void map2QueryStr() { - Map map = Maps.newHashMap(); - map.put("a" , "1"); - map.put("b" , "2"); - map.put("c" , "3"); + Map map = new HashMap<>(); + map.put("a", "1"); + map.put("b", "2"); + map.put("c", "3"); System.out.println(StringTools.map2QueryStr(map)); } @@ -92,13 +92,14 @@ public class StringToolsTest { @Test public void substring() { String s = "0123456789"; - Assert.assertEquals("56789", StringTools.substring(s,5)); - Assert.assertEquals("12", StringTools.substring(s,1,3)); + Assert.assertEquals("56789", StringTools.substring(s, 5)); + Assert.assertEquals("12", StringTools.substring(s, 1, 3)); } + @Test public void substringLength() { String s = "0123456789"; - Assert.assertEquals("01234", StringTools.substringLength(s,5)); - Assert.assertEquals("1", StringTools.substringLength(s,1,1)); + Assert.assertEquals("01234", StringTools.substringLength(s, 5)); + Assert.assertEquals("1", StringTools.substringLength(s, 1, 1)); } } diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/ThreadToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/ThreadToolsTest.java similarity index 82% rename from src/test/java/com/gitee/cliveyuan/tools/test/ThreadToolsTest.java rename to common/src/test/java/cn/cliveyuan/tools/common/test/ThreadToolsTest.java index 0b07be2..3af14ac 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/ThreadToolsTest.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/ThreadToolsTest.java @@ -1,6 +1,6 @@ -package com.gitee.cliveyuan.tools.test; +package cn.cliveyuan.tools.common.test; -import com.gitee.cliveyuan.tools.ThreadTools; +import cn.cliveyuan.tools.common.ThreadTools; /** * @author clive diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/ValidateToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/ValidateToolsTest.java similarity index 95% rename from src/test/java/com/gitee/cliveyuan/tools/test/ValidateToolsTest.java rename to common/src/test/java/cn/cliveyuan/tools/common/test/ValidateToolsTest.java index e1fa0dd..d19b035 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/ValidateToolsTest.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/ValidateToolsTest.java @@ -1,11 +1,9 @@ -package com.gitee.cliveyuan.tools.test; +package cn.cliveyuan.tools.common.test; -import com.gitee.cliveyuan.tools.ValidateTools; +import cn.cliveyuan.tools.common.ValidateTools; import org.junit.Assert; import org.junit.Test; -import java.util.regex.Pattern; - /** * @author clive * Created on 2018/07/24 @@ -18,31 +16,37 @@ public class ValidateToolsTest { String str = "坎坎坷坷解决李四在干嘛你哈哈哈"; Assert.assertTrue(ValidateTools.isName(str)); } + @Test public void isFloat() { String str = "-8"; Assert.assertTrue(ValidateTools.isFloat(str)); } + @Test public void isUrl() { String str = "http://www.baidu.com"; Assert.assertTrue(ValidateTools.isUrl(str)); } + @Test public void isMobile() { String str = "13800138000"; Assert.assertTrue(ValidateTools.isMobile(str)); } + @Test public void isEmail() { String str = "cliveyuan@foxmail.com"; Assert.assertTrue(ValidateTools.isEmail(str)); } + @Test public void isBankCardNo() { String str = "6217002710000684874";//网上随便找的 Assert.assertTrue(ValidateTools.isBankCardNo(str)); } + @Test public void isIdCardNo() { //网上随机生成的 diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/ZipToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/ZipToolsTest.java similarity index 90% rename from src/test/java/com/gitee/cliveyuan/tools/test/ZipToolsTest.java rename to common/src/test/java/cn/cliveyuan/tools/common/test/ZipToolsTest.java index 0ea9b42..cde4402 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/ZipToolsTest.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/ZipToolsTest.java @@ -1,6 +1,6 @@ -package com.gitee.cliveyuan.tools.test; +package cn.cliveyuan.tools.common.test; -import com.gitee.cliveyuan.tools.ZipTools; +import cn.cliveyuan.tools.common.ZipTools; import net.lingala.zip4j.exception.ZipException; import org.junit.Test; diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/Person.java b/common/src/test/java/cn/cliveyuan/tools/common/test/bean/Person.java similarity index 98% rename from src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/Person.java rename to common/src/test/java/cn/cliveyuan/tools/common/test/bean/Person.java index c8d112c..1635277 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/Person.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/bean/Person.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.test.bean.excel; +package cn.cliveyuan.tools.common.test.bean; import java.math.BigDecimal; import java.util.Date; diff --git a/doc/feature.md b/doc/feature.md index 6366cdb..a6e6386 100644 --- a/doc/feature.md +++ b/doc/feature.md @@ -1,6 +1,6 @@ ## Feature List * StringTools.java -* Assert.java +* AssertTools.java * DateTimeTools.java * FileTools.java * EmailTools.java diff --git a/httpclient/pom.xml b/httpclient/pom.xml new file mode 100644 index 0000000..0d25f41 --- /dev/null +++ b/httpclient/pom.xml @@ -0,0 +1,26 @@ + + + + java-tools + com.gitee.opensource4clive + 4.0.0 + + 4.0.0 + + java-tools-httpclient + + + + com.gitee.opensource4clive + java-tools-common + + + com.squareup.okhttp3 + okhttp + + + + + diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientSingleton.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientSingleton.java new file mode 100644 index 0000000..e0ec3e3 --- /dev/null +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientSingleton.java @@ -0,0 +1,53 @@ +package cn.cliveyuan.tools.httpclient; + +import cn.cliveyuan.tools.httpclient.ssl.SSL; +import okhttp3.ConnectionPool; +import okhttp3.Dispatcher; +import okhttp3.OkHttpClient; +import okhttp3.internal.Util; + +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * @author Clive Yuan + * @date 2021/06/08 + */ +public class HttpClientSingleton { + + /** + * 获取okHttpClient单例 + * + * @return + */ + public static OkHttpClient getOkHttpClient() { + return OkHttpClientSingletonHolder.OK_HTTP_CLIENT; + } + + + private static class OkHttpClientSingletonHolder { + private static final OkHttpClient OK_HTTP_CLIENT; + + static { + ThreadPoolExecutor executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, + new SynchronousQueue<>(), Util.threadFactory("OkHttp Dispatcher", false)); + Dispatcher dispatcher = new Dispatcher(executorService); + dispatcher.setMaxRequestsPerHost(250); + dispatcher.setMaxRequests(2000); + + OkHttpClient.Builder clientBuilder = new OkHttpClient + .Builder() + .dispatcher(dispatcher) + .followRedirects(false) + .followSslRedirects(false) + .connectionPool(new ConnectionPool(128, 900, TimeUnit.SECONDS)) + .connectTimeout(10, TimeUnit.SECONDS) + .readTimeout(3, TimeUnit.MINUTES) + .writeTimeout(1, TimeUnit.MINUTES) + .sslSocketFactory(SSL.sslSocketFactory, SSL.x509TrustManager) + .hostnameVerifier(SSL.hostnameVerifier); + OK_HTTP_CLIENT = clientBuilder.build(); + } + } +} diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientTools.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientTools.java new file mode 100644 index 0000000..5ad8fcf --- /dev/null +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientTools.java @@ -0,0 +1,162 @@ +package cn.cliveyuan.tools.httpclient; + +import cn.cliveyuan.tools.common.AssertTools; +import cn.cliveyuan.tools.common.StringTools; +import cn.cliveyuan.tools.common.ValidateTools; +import cn.cliveyuan.tools.common.bean.ValidationResult; +import cn.cliveyuan.tools.httpclient.bean.HttpClientRequest; +import cn.cliveyuan.tools.httpclient.bean.HttpClientResponse; +import cn.cliveyuan.tools.httpclient.bean.RequestContextTypeEnum; +import cn.cliveyuan.tools.httpclient.bean.RequestMethodEnum; +import cn.cliveyuan.tools.httpclient.client.HttpClient; +import cn.cliveyuan.tools.httpclient.client.OkHttpHttpClient; + +import java.util.Map; + +/** + * http客户端工具 + * + * @author Clive Yuan + * @date 2021/06/08 + */ +public class HttpClientTools { + + private static final HttpClient HTTP_CLIENT = new OkHttpHttpClient(); + private static final String DEFAULT_HEADER = "Accept: */*\n" + + "Accept-Encoding: deflate\n" + + "Accept-Language: zh-CN,zh;q=0.9\n" + + "Connection: keep-alive\n" + + "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36\n" + + "X-Requested-With: XMLHttpRequest"; + + /** + * 发起get请求 + * + * @param url 请求地址 + * @return 响应内容 + */ + public static String get(String url) { + return request(HttpClientRequest.builder().url(url).build()).getResponse(); + } + + /** + * 发起get请求 + * + * @param url 请求地址 + * @param headers 请求头 + * @return 响应内容 + */ + public static String get(String url, Map headers) { + return request(HttpClientRequest.builder().url(url).headers(headers).build()).getResponse(); + } + + /** + * 发起post-form请求 + * + * @param url 请求地址 + * @param paramMap 参数 + * @return 响应内容 + */ + public static String post(String url, Map paramMap) { + return request(HttpClientRequest.builder() + .url(url) + .method(RequestMethodEnum.POST) + .params(paramMap) + .build()).getResponse(); + } + + /** + * 发起post-form请求 + * + * @param url 请求地址 + * @param paramMap 参数 + * @param headers 请求头 + * @return 响应内容 + */ + public static String post(String url, Map paramMap, Map headers) { + return request(HttpClientRequest.builder() + .url(url) + .method(RequestMethodEnum.POST) + .headers(headers) + .params(paramMap) + .build()).getResponse(); + } + + /** + * 发起post-json请求 + * + * @param url 请求地址 + * @param requestBody 请求体 + * @return 响应内容 + */ + public static String postJson(String url, String requestBody) { + return request(HttpClientRequest.builder() + .url(url) + .method(RequestMethodEnum.POST) + .contextType(RequestContextTypeEnum.JSON) + .requestBody(requestBody) + .build()).getResponse(); + } + + /** + * 发起post-json请求 + * + * @param url 请求地址 + * @param requestBody 请求体 + * @param headers 请求头 + * @return 响应内容 + */ + public static String postJson(String url, String requestBody, Map headers) { + return request(HttpClientRequest.builder() + .url(url) + .method(RequestMethodEnum.POST) + .contextType(RequestContextTypeEnum.JSON) + .headers(headers) + .requestBody(requestBody) + .build()).getResponse(); + } + + /** + * 发送请求 + * + * @param request 入参 + * @return 响应 + */ + public static HttpClientResponse request(HttpClientRequest request) { + handleRequest(request); + try { + return HTTP_CLIENT.request(request); + } catch (Exception e) { + if (request.isIgnoreError()) { + HttpClientResponse response = new HttpClientResponse(); + response.setStatusCode(-1); + response.setSuccessful(false); + response.setExceptionMsg(e.getMessage()); + return response; + } + throw new RuntimeException(e); + } + } + + private static void handleRequest(HttpClientRequest request) { + ValidateTools.assertValidateEntity(request); + if (request.isAddDefaultHeaders()) { + request.getHeaders().putAll(StringTools.str2Map(DEFAULT_HEADER)); + } + if (StringTools.isNotBlank(request.getKvParams())) { + request.getParams().putAll(StringTools.str2Map(request.getKvParams())); + } + if (StringTools.isNotBlank(request.getKvHeaders())) { + request.getHeaders().putAll(StringTools.str2Map(request.getKvHeaders())); + } + if (StringTools.isNotBlank(request.getKvCookies())) { + request.getCookies().putAll(StringTools.str2Map(request.getKvCookies())); + } + if (!request.getCookies().isEmpty()) { + request.getHeaders().put("Cookie", StringTools.map2CookieStr(request.getCookies())); + } + if (request.isAddDefaultReferer()) { + request.getHeaders().put("Referer", request.getUrl()); + } + } +} diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientRequest.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientRequest.java new file mode 100644 index 0000000..f6cd2dd --- /dev/null +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientRequest.java @@ -0,0 +1,144 @@ +package cn.cliveyuan.tools.httpclient.bean; + +import lombok.Builder; +import lombok.Data; +import org.apache.commons.lang3.StringUtils; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Clive Yuan + * @date 2021/06/08 + */ +@Data +@Builder +public class HttpClientRequest { + /** + * 请求地址 + */ + @NotBlank(message = "url can't be blank") + private String url; + /** + * 请求方式 + */ + @Builder.Default + @NotNull(message = "method can't be null") + private RequestMethodEnum method = RequestMethodEnum.GET; + /** + * 内容类型 + */ + @Builder.Default + @NotNull(message = "contextType can't be null") + private RequestContextTypeEnum contextType = RequestContextTypeEnum.FORM; + /** + * 请求体 (当请求内容类型为JSON时有效) + */ + @Builder.Default + private String requestBody = StringUtils.EMPTY; + /** + * 参数 + */ + @Builder.Default + private Map params = new HashMap<>(); + /** + * 通过字符串设置请求入参 + * 格式: + *

+     *     user: clive
+     *     pwd:  123456
+     * 
+ * 每行一对,每对之间用分号分隔对应key-value + * 会去除前后空格 + */ + private String kvParams; + /** + * HEADER + */ + @Builder.Default + private Map headers = new HashMap<>(); + /** + * 通过字符串设置请求Header + */ + private String kvHeaders; + /** + * COOKIE + */ + @Builder.Default + private Map cookies = new HashMap<>(); + /** + * 通过字符串设置请求Cookie + */ + private String kvCookies; + /** + * 连接超时时间 (默认10s) + */ + private Duration connectTimeout; + /** + * 读取超时时间 (默认180s) + */ + private Duration readTimeout; + /** + * 写入超时时间 (默认60s) + */ + private Duration writeTimeout; + /** + * 添加默认Header + */ + private boolean addDefaultHeaders; + /** + * 添加默认Referer (默认为请求地址) + */ + private boolean addDefaultReferer; + /** + * 忽略SSL证书 + */ + private boolean ignoreSslCertificate; + /** + * 字符集 + */ + private Charset charset; + /** + * 忽略错误 (发生错误时不抛出HttpClientException异常) + */ + private boolean ignoreError; + + + /** + * 添加入参 + * + * @param key name + * @param value val + */ + public HttpClientRequest addParam(String key, String value) { + this.params.put(key, value); + return this; + } + + /** + * 添加Header + * + * @param key name + * @param value val + */ + public HttpClientRequest addHeader(String key, String value) { + this.headers.put(key, value); + return this; + } + + /** + * 添加Cookie + * + * @param key name + * @param value val + */ + public HttpClientRequest addCookie(String key, String value) { + this.cookies.put(key, value); + return this; + } +} diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientResponse.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientResponse.java new file mode 100644 index 0000000..999e02e --- /dev/null +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientResponse.java @@ -0,0 +1,38 @@ +package cn.cliveyuan.tools.httpclient.bean; + +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Clive Yuan + * @date 2021/06/08 + */ +@Data +public class HttpClientResponse { + /** + * http 状态码 + */ + private int statusCode; + /** + * 是否为成功状态 + */ + private boolean isSuccessful; + /** + * 响应内容 + */ + private String response; + /** + * 响应Header + */ + private Map headers = new HashMap<>(); + /** + * 响应Cookie + */ + private Map cookies = new HashMap<>(); + /** + * 报错信息 (当抛异常时且ignoreError=true时有值) + */ + private String exceptionMsg; +} diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/RequestContextTypeEnum.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/RequestContextTypeEnum.java new file mode 100644 index 0000000..95f5e2e --- /dev/null +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/RequestContextTypeEnum.java @@ -0,0 +1,19 @@ +package cn.cliveyuan.tools.httpclient.bean; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import okhttp3.MediaType; + +/** + * @author Clive Yuan + * @date 2021/06/08 + */ +@Getter +@AllArgsConstructor +public enum RequestContextTypeEnum { + + FORM(MediaType.get("application/x-www-form-urlencoded;charset=utf-8")), + JSON(MediaType.get("application/json;charset=utf-8")); + + private final MediaType mediaType; +} diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/RequestMethodEnum.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/RequestMethodEnum.java new file mode 100644 index 0000000..4c97287 --- /dev/null +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/RequestMethodEnum.java @@ -0,0 +1,12 @@ +package cn.cliveyuan.tools.httpclient.bean; + +/** + * @author Clive Yuan + * @date 2021/06/08 + */ +public enum RequestMethodEnum { + GET, + POST, + PUT, + DELETE +} diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/client/HttpClient.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/client/HttpClient.java new file mode 100644 index 0000000..1a9587d --- /dev/null +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/client/HttpClient.java @@ -0,0 +1,21 @@ +package cn.cliveyuan.tools.httpclient.client; + +import cn.cliveyuan.tools.httpclient.bean.HttpClientRequest; +import cn.cliveyuan.tools.httpclient.bean.HttpClientResponse; + +/** + * http客户端接口 + * + * @author Clive Yuan + * @date 2021/06/08 + */ +public interface HttpClient { + + /** + * http请求 + * + * @param request 入参 + * @return 响应 + */ + HttpClientResponse request(HttpClientRequest request); +} diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/client/OkHttpHttpClient.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/client/OkHttpHttpClient.java new file mode 100644 index 0000000..d1eaace --- /dev/null +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/client/OkHttpHttpClient.java @@ -0,0 +1,187 @@ +package cn.cliveyuan.tools.httpclient.client; + +import cn.cliveyuan.tools.common.MapTools; +import cn.cliveyuan.tools.common.StringTools; +import cn.cliveyuan.tools.httpclient.HttpClientSingleton; +import cn.cliveyuan.tools.httpclient.bean.HttpClientRequest; +import cn.cliveyuan.tools.httpclient.bean.HttpClientResponse; +import cn.cliveyuan.tools.httpclient.bean.RequestContextTypeEnum; +import cn.cliveyuan.tools.httpclient.bean.RequestMethodEnum; +import cn.cliveyuan.tools.httpclient.ssl.SSL; +import cn.cliveyuan.tools.httpclient.ssl.TrustAnySSL; +import lombok.extern.slf4j.Slf4j; +import okhttp3.Call; +import okhttp3.FormBody; +import okhttp3.Headers; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.ResponseBody; +import okhttp3.internal.Util; +import okio.BufferedSource; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.StringJoiner; +import java.util.concurrent.TimeUnit; + +/** + * okHttp对httpClient的实现 + * + * @author Clive Yuan + * @date 2021/06/08 + */ +@Slf4j +public class OkHttpHttpClient implements HttpClient { + + @Override + public HttpClientResponse request(HttpClientRequest httpClientRequest) { + HttpClientResponse httpClientResponse = new HttpClientResponse(); + httpClientResponse.setResponse(StringTools.EMPTY); + OkHttpClient okHttpClient = buildOkHttpClient(httpClientRequest); + HttpUrl httpUrl = getHttpUrl(httpClientRequest.getMethod(), httpClientRequest.getUrl(), httpClientRequest.getParams()); + RequestBody requestBody = null; + if (!RequestMethodEnum.GET.equals(httpClientRequest.getMethod())) { + requestBody = this.buildRequestBody(httpClientRequest); + } + final Request.Builder requestBuilder = new Request.Builder().method(httpClientRequest.getMethod().name(), requestBody).url(httpUrl); + log.debug("HttpClient request: {}", httpClientRequest); + Request request = buildRequest(requestBuilder, httpClientRequest.getHeaders()); + Call call = okHttpClient.newCall(request); + try (Response response = call.execute()) { + httpClientResponse.setStatusCode(response.code()); + httpClientResponse.setSuccessful(response.isSuccessful()); + ResponseBody responseBody = response.body(); + if (Objects.nonNull(responseBody)) { + httpClientResponse.setResponse(this.handleResponse(httpClientRequest, responseBody)); + } + Headers headers = response.headers(); + httpClientResponse.setHeaders(getResponseHeaders(headers)); + httpClientResponse.setCookies(getResponseCookies(headers)); + } catch (Exception e) { + log.error("request error: httpClientRequest=" + httpClientRequest, e); + throw new RuntimeException(e); + } + log.debug("HttpClient response: {}", httpClientResponse); + return httpClientResponse; + } + + private RequestBody buildRequestBody(HttpClientRequest httpClientRequest) { + if (RequestContextTypeEnum.JSON.equals(httpClientRequest.getContextType())) { + return RequestBody.create(httpClientRequest.getContextType().getMediaType(), httpClientRequest.getRequestBody()); + } + Map params = httpClientRequest.getParams(); + FormBody.Builder formBodyBuilder = new FormBody.Builder(StandardCharsets.UTF_8); + if (MapTools.isNotEmpty(params)) { + params.forEach((key, value) -> { + if (Objects.isNull(key) || Objects.isNull(value)) { + return; + } + formBodyBuilder.add(key, value.toString()); + }); + + } + return formBodyBuilder.build(); + } + + private OkHttpClient buildOkHttpClient(HttpClientRequest httpClientRequest) { + OkHttpClient okHttpClient = HttpClientSingleton.getOkHttpClient(); + if (httpClientRequest.getConnectTimeout() != null + || httpClientRequest.getReadTimeout() != null + || httpClientRequest.getWriteTimeout() != null + || httpClientRequest.isIgnoreSslCertificate()) { + OkHttpClient.Builder builder = okHttpClient.newBuilder() + .connectTimeout(10, TimeUnit.SECONDS) + .readTimeout(3, TimeUnit.MINUTES) + .writeTimeout(1, TimeUnit.MINUTES) + .sslSocketFactory(SSL.sslSocketFactory, SSL.x509TrustManager) + .hostnameVerifier(SSL.hostnameVerifier); + if (Objects.nonNull(httpClientRequest.getConnectTimeout())) { + builder.connectTimeout(httpClientRequest.getConnectTimeout()); + } + if (Objects.nonNull(httpClientRequest.getReadTimeout())) { + builder.readTimeout(httpClientRequest.getReadTimeout()); + } + if (Objects.nonNull(httpClientRequest.getWriteTimeout())) { + builder.writeTimeout(httpClientRequest.getWriteTimeout()); + } + if (httpClientRequest.isIgnoreSslCertificate()) { + builder.sslSocketFactory(TrustAnySSL.sslSocketFactory, TrustAnySSL.x509TrustManager) + .hostnameVerifier(TrustAnySSL.hostnameVerifier); + } + return builder.build(); + } + return okHttpClient; + } + + private String handleResponse(HttpClientRequest httpClientRequest, ResponseBody responseBody) throws IOException { + if (Objects.nonNull(httpClientRequest.getCharset())) { + try (BufferedSource source = responseBody.source()) { + Charset charset = Util.bomAwareCharset(source, httpClientRequest.getCharset()); + return source.readString(charset); + } + } + return responseBody.string(); + } + + private Map getResponseCookies(Headers headers) { + Map cookieMap = new HashMap<>(); + String cookieString = headers.get("Set-Cookie"); + if (Objects.nonNull(cookieString)) { + String[] pairs = cookieString.split(";"); + for (String pair : pairs) { + String[] kvs = pair.split("="); + String key = kvs[0].trim(); + StringJoiner value = new StringJoiner(""); + if (kvs.length >= 2) { + for (int i = 1; i < kvs.length; i++) { + value.add(kvs[i]); + } + } + cookieMap.put(key, value.toString()); + } + } + return cookieMap; + } + + private Map getResponseHeaders(Headers headers) { + Map headerMap = new HashMap<>(); + for (int i = 0; i < headers.size(); i++) { + String name = headers.name(i); + headerMap.put(name, headers.get(name)); + } + return headerMap; + } + + private static Request buildRequest(Request.Builder requestBuilder, Map headers) { + if (!headers.isEmpty()) { + headers.forEach((key, value) -> { + if (key != null && value != null) { + requestBuilder.addHeader(key, value); + } + }); + } + return requestBuilder.build(); + } + + + private static HttpUrl getHttpUrl(RequestMethodEnum method, String url, Map params) { + HttpUrl httpUrl = HttpUrl.parse(url); + if (RequestMethodEnum.GET.equals(method) && httpUrl != null && MapTools.isNotEmpty(params)) { + final HttpUrl.Builder httpUrlBuilder = httpUrl.newBuilder(); + params.forEach((key, value) -> { + if (key != null && value != null) { + httpUrlBuilder.addQueryParameter(key, String.valueOf(value)); + } + }); + return httpUrlBuilder.build(); + } + return httpUrl; + } +} diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/ssl/SSL.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/ssl/SSL.java new file mode 100644 index 0000000..8389910 --- /dev/null +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/ssl/SSL.java @@ -0,0 +1,88 @@ +package cn.cliveyuan.tools.httpclient.ssl; + +import okhttp3.internal.tls.OkHostnameVerifier; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509TrustManager; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.Arrays; + +/** + * @author Clive Yuan + * @date 2021/06/08 + */ +public class SSL { + public static final X509TrustManager x509TrustManager; + public static final HostnameVerifier hostnameVerifier; + public static final SSLSocketFactory sslSocketFactory; + + static { + try { + final X509TrustManager systemDefaultTrustManager = systemDefaultTrustManager(); + // Create a trust manager that does not validate certificate chains + x509TrustManager = new X509TrustManager() { + + X509Certificate[] EMPTY = new X509Certificate[0]; + + @Override + public void checkClientTrusted( + X509Certificate[] chain, + String authType) throws CertificateException { + systemDefaultTrustManager.checkClientTrusted(chain, authType); + } + + @Override + public void checkServerTrusted( + X509Certificate[] chain, + String authType) throws CertificateException { + try { + systemDefaultTrustManager.checkServerTrusted(chain, authType); + } catch (CertificateException e) { + throw e; + } catch (Throwable e) { + throw new CertificateException(e); + } + } + + + @Override + public X509Certificate[] getAcceptedIssuers() { + return systemDefaultTrustManager.getAcceptedIssuers(); + } + }; + + hostnameVerifier = OkHostnameVerifier.INSTANCE::verify; + sslSocketFactory = systemDefaultSslSocketFactory(x509TrustManager); + } catch (Throwable e) { + throw new AssertionError(e); + } + } + + + private static X509TrustManager systemDefaultTrustManager() throws NoSuchAlgorithmException, KeyStoreException { + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( + TrustManagerFactory.getDefaultAlgorithm()); + trustManagerFactory.init((KeyStore) null); + TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); + if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) { + throw new IllegalStateException("Unexpected default trust managers:" + + Arrays.toString(trustManagers)); + } + return (X509TrustManager) trustManagers[0]; + } + + private static SSLSocketFactory systemDefaultSslSocketFactory(X509TrustManager trustManager) throws NoSuchAlgorithmException, KeyManagementException { + SSLContext sslContext = SSLContext.getInstance("TLS"); + sslContext.init(null, new TrustManager[]{trustManager}, null); + return sslContext.getSocketFactory(); + } +} diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/ssl/TrustAnySSL.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/ssl/TrustAnySSL.java new file mode 100644 index 0000000..50b8c18 --- /dev/null +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/ssl/TrustAnySSL.java @@ -0,0 +1,57 @@ +package cn.cliveyuan.tools.httpclient.ssl; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +/** + * @author Clive Yuan + * @date 2021/06/08 + */ +public class TrustAnySSL { + public static final X509TrustManager x509TrustManager; + public static final HostnameVerifier hostnameVerifier; + public static final SSLSocketFactory sslSocketFactory; + + static { + try { + x509TrustManager = new X509TrustManager() { + + @Override + public void checkClientTrusted( + X509Certificate[] chain, + String authType) throws CertificateException { + + } + + @Override + public void checkServerTrusted( + X509Certificate[] chain, + String authType) throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }; + + hostnameVerifier = (s, sslSession) -> true; + sslSocketFactory = systemDefaultSslSocketFactory(x509TrustManager); + } catch (Throwable e) { + throw new AssertionError(e); + } + } + + private static SSLSocketFactory systemDefaultSslSocketFactory(X509TrustManager trustManager) throws NoSuchAlgorithmException, KeyManagementException { + SSLContext sslContext = SSLContext.getInstance("TLS"); + sslContext.init(null, new TrustManager[]{trustManager}, null); + return sslContext.getSocketFactory(); + } +} diff --git a/httpclient/src/test/java/cn/cliveyuan/tools/httpclient/test/HttpClientToolsTest.java b/httpclient/src/test/java/cn/cliveyuan/tools/httpclient/test/HttpClientToolsTest.java new file mode 100644 index 0000000..1583359 --- /dev/null +++ b/httpclient/src/test/java/cn/cliveyuan/tools/httpclient/test/HttpClientToolsTest.java @@ -0,0 +1,72 @@ +package cn.cliveyuan.tools.httpclient.test; + +import cn.cliveyuan.tools.common.AssertTools; +import cn.cliveyuan.tools.httpclient.HttpClientTools; +import cn.cliveyuan.tools.httpclient.bean.HttpClientRequest; +import cn.cliveyuan.tools.httpclient.bean.HttpClientResponse; +import cn.cliveyuan.tools.httpclient.bean.RequestContextTypeEnum; +import cn.cliveyuan.tools.httpclient.bean.RequestMethodEnum; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Clive Yuan + * @date 2021/06/09 + */ +public class HttpClientToolsTest { + + @Test + public void get() { + String response = HttpClientTools.get("https://cliveyuan.cn"); + System.out.println(response); + AssertTools.notNull(response); + } + + @Test + public void post() { + Map map = new HashMap<>(); + map.put("type", "top"); + map.put("key", "APPKEY"); + String response = HttpClientTools.post("http://v.juhe.cn/toutiao/index", map); + System.out.println(response); + AssertTools.notNull(response); + } + + @Test + public void postJson() { + String response = HttpClientTools.postJson("http://localhost:8001/mgr/admin/login", "{\"account\":\"admin\",\"password\":\"123456\",\"captcha\":{\"id\":1,\"value\":\"111111\"}}"); + System.out.println(response); + AssertTools.notNull(response); + } + + @Test + public void request() { + Map params = new HashMap<>(); + params.put("username", "clive"); + Map headers = new HashMap<>(); + headers.put("x-test", "header"); + Map cookies = new HashMap<>(); + cookies.put("pid", "cookie"); + HttpClientResponse response = HttpClientTools.request(HttpClientRequest.builder() + .url("http://localhost:8001/mgr/app-tool/page") + .method(RequestMethodEnum.POST) + .contextType(RequestContextTypeEnum.JSON) + .requestBody("{\"pageParameter\":{\"page\":1,\"limit\":30},\"record\":{\"app\":9}}") + .params(params) + .headers(headers) + .cookies(cookies) + .kvParams("pwd: 11223344") + .kvHeaders("X-Token: 84459edd573f52b7b515d454be708c2e") + .kvCookies("kv_pid: cookie2") + .addDefaultReferer(true) + .addDefaultHeaders(true) + .build()); + AssertTools.isTrue(response.isSuccessful()); + System.out.println("response: " + response.getResponse()); + System.out.println("headers: " + response.getHeaders()); + AssertTools.notNull(response.getResponse()); + } + +} diff --git a/poi/pom.xml b/poi/pom.xml new file mode 100644 index 0000000..48ad8fc --- /dev/null +++ b/poi/pom.xml @@ -0,0 +1,29 @@ + + + + java-tools + com.gitee.opensource4clive + 4.0.0 + + 4.0.0 + + java-tools-poi + + + + com.gitee.opensource4clive + java-tools-common + + + org.apache.poi + poi + + + org.apache.poi + poi-ooxml + + + + diff --git a/poi/src/main/java/cn/cliveyuan/tools/poi/ExcelTools.java b/poi/src/main/java/cn/cliveyuan/tools/poi/ExcelTools.java new file mode 100644 index 0000000..097550d --- /dev/null +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/ExcelTools.java @@ -0,0 +1,104 @@ +package cn.cliveyuan.tools.poi; + + +import cn.cliveyuan.tools.poi.bean.ExcelReader; +import cn.cliveyuan.tools.poi.bean.ExcelWriter; +import cn.cliveyuan.tools.poi.bean.SheetContent; +import cn.cliveyuan.tools.poi.bean.SheetData; +import cn.cliveyuan.tools.poi.bean.SheetInfo; +import cn.cliveyuan.tools.poi.inner.ExcelInnerHelper; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import org.apache.commons.collections4.CollectionUtils; + +import java.io.File; +import java.util.Collections; +import java.util.List; +import java.util.Map; + + +/** + * Excel 工具类 + * + * @author Clive Yuan + * @date 2021-06-09 + */ +public class ExcelTools { + + private ExcelTools() { + } + + /** + * 读取Excel + * + * @param excelReader excelReader + * @return + */ + public static List read(ExcelReader excelReader) { + if (excelReader.isSkipFirstRow()) { + excelReader.setStartRowNo(1); + } + return ExcelInnerHelper.read(excelReader); + } + + /** + * 简单读取Excel, 读取名为'Sheet1'的数据 + * + * @param filePath 文件路径 + * @param clazz 类 + * @param skipFirstRow 是否跳过首行 + * @param 类泛型 + * @return + */ + public static List readSimply(String filePath, Class clazz, boolean skipFirstRow) { + SheetInfo sheetInfo = new SheetInfo("Sheet1", clazz); + List list = read(ExcelReader.builder() + .pathname(filePath) + .sheetInfoList(Collections.singletonList(sheetInfo)) + .skipFirstRow(skipFirstRow) + .build()); + if (CollectionUtils.isEmpty(list)) { + return Collections.emptyList(); + } + SheetData sheetData = list.get(0); + return JSONArray.parseArray(JSON.toJSONString(sheetData.getDataList()), clazz); + } + + /** + * 生成Excel + * + * @param excelWriter excelWriter + * @return + */ + public static File write(ExcelWriter excelWriter) { + return ExcelInnerHelper.write(excelWriter); + } + + /** + * 简单生成单个sheetExcel + * + * @param filePath 文件绝对路径 + * @param headers 抬头 + * @param data 数据 + * @param 数据类泛型 + * @return + */ + public static File writeSimply(String filePath, String[] headers, List data) { + SheetContent sheetContent = new SheetContent<>("Sheet1", headers, data); + return write(ExcelWriter.builder() + .filePath(filePath) + .sheetContentList(Collections.singletonList(sheetContent)) + .build()); + } + + /** + * 读入excel文件,解析后返回 + * key: sheet名, value每行数据 + * + * @param absoluteFilePath 文件绝对路径 + * @param sheetNames 指定的sheet名 + */ + public static Map> readRawExcel(String absoluteFilePath, String... sheetNames) { + return ExcelInnerHelper.readRawExcel(absoluteFilePath, sheetNames); + } +} diff --git a/src/main/java/com/gitee/cliveyuan/tools/data/excel/ExcelReader.java b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelReader.java similarity index 39% rename from src/main/java/com/gitee/cliveyuan/tools/data/excel/ExcelReader.java rename to poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelReader.java index 047aef1..561e76a 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/data/excel/ExcelReader.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelReader.java @@ -1,22 +1,12 @@ -package com.gitee.cliveyuan.tools.data.excel; +package cn.cliveyuan.tools.poi.bean; -import com.gitee.cliveyuan.tools.Assert; -import com.gitee.cliveyuan.tools.ExcelTools; -import com.gitee.cliveyuan.tools.bean.excel.SheetData; -import com.gitee.cliveyuan.tools.bean.excel.SheetInfo; -import com.gitee.cliveyuan.tools.data.TableUtils; -import com.gitee.cliveyuan.tools.exception.ExcelException; -import com.google.common.collect.Lists; +import java.util.ArrayList; import lombok.Builder; import lombok.Data; import lombok.extern.slf4j.Slf4j; import java.io.Serializable; import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.function.Function; -import java.util.stream.Collectors; /** * Created by Clive on 2019/10/28. @@ -26,11 +16,23 @@ import java.util.stream.Collectors; @Slf4j public class ExcelReader implements Serializable { - private String excelPath; + /** + * excel 全文件名 (包括扩展名, 如 /xxx/a/b/test.xlsx) + */ + private String pathname; + /** + * sheet 信息列表 + */ @Builder.Default - private List sheetInfoList = Lists.newArrayList(); - @Builder.Default - private int skipRowNo = 1; + private List sheetInfoList = new ArrayList(); + /** + * 开始行号 + */ + private int startRowNo; + /** + * 跳过首行 + */ + private boolean skipFirstRow; public ExcelReader addSheetInfo(String sheetName, Class clazz) { sheetInfoList.add(new SheetInfo(sheetName, clazz)); diff --git a/src/main/java/com/gitee/cliveyuan/tools/enums/ExcelType.java b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelType.java similarity index 63% rename from src/main/java/com/gitee/cliveyuan/tools/enums/ExcelType.java rename to poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelType.java index 000479e..5ff86c1 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/enums/ExcelType.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelType.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.enums; +package cn.cliveyuan.tools.poi.bean; /** * excel格式 @@ -7,7 +7,7 @@ package com.gitee.cliveyuan.tools.enums; * Created on 2018/07/30 * @since 1.0 */ -public enum ExcelType { +public enum ExcelType { XLS, XLSX, XLSM diff --git a/src/main/java/com/gitee/cliveyuan/tools/data/excel/ExcelWriter.java b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelWriter.java similarity index 59% rename from src/main/java/com/gitee/cliveyuan/tools/data/excel/ExcelWriter.java rename to poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelWriter.java index 1391d36..9df0552 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/data/excel/ExcelWriter.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelWriter.java @@ -1,8 +1,6 @@ -package com.gitee.cliveyuan.tools.data.excel; +package cn.cliveyuan.tools.poi.bean; -import com.gitee.cliveyuan.tools.bean.excel.SheetContent; -import com.gitee.cliveyuan.tools.enums.ExcelType; -import com.google.common.collect.Lists; +import java.util.ArrayList; import lombok.Builder; import lombok.Data; import lombok.extern.slf4j.Slf4j; @@ -19,11 +17,24 @@ import java.util.List; @Slf4j public class ExcelWriter implements Serializable { + /** + * 文件路径 + */ private String filePath; + /** + * 文件名 (不指定则随机生成) + */ + private String fileName; + /** + * 文件类型 + */ @Builder.Default - private ExcelType excelType = ExcelType.XLSX; + private ExcelType fileType = ExcelType.XLSX; + /** + * sheet 内容列表 + */ @Builder.Default - private List sheetContentList = Lists.newArrayList(); + private List sheetContentList = new ArrayList(); public ExcelWriter addSheetContent(String sheetName, String[] headers, Collection data) { this.sheetContentList.add(new SheetContent(sheetName, headers, data)); diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/excel/SheetContent.java b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/SheetContent.java similarity index 90% rename from src/main/java/com/gitee/cliveyuan/tools/bean/excel/SheetContent.java rename to poi/src/main/java/cn/cliveyuan/tools/poi/bean/SheetContent.java index be0e9f2..660f0c8 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/excel/SheetContent.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/SheetContent.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean.excel; +package cn.cliveyuan.tools.poi.bean; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/excel/SheetData.java b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/SheetData.java similarity index 89% rename from src/main/java/com/gitee/cliveyuan/tools/bean/excel/SheetData.java rename to poi/src/main/java/cn/cliveyuan/tools/poi/bean/SheetData.java index 63afd1b..9d81477 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/excel/SheetData.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/SheetData.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean.excel; +package cn.cliveyuan.tools.poi.bean; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/excel/SheetInfo.java b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/SheetInfo.java similarity index 88% rename from src/main/java/com/gitee/cliveyuan/tools/bean/excel/SheetInfo.java rename to poi/src/main/java/cn/cliveyuan/tools/poi/bean/SheetInfo.java index e6e035a..e206766 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/excel/SheetInfo.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/SheetInfo.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean.excel; +package cn.cliveyuan.tools.poi.bean; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/exception/ExcelException.java b/poi/src/main/java/cn/cliveyuan/tools/poi/exception/ExcelException.java similarity index 91% rename from src/main/java/com/gitee/cliveyuan/tools/exception/ExcelException.java rename to poi/src/main/java/cn/cliveyuan/tools/poi/exception/ExcelException.java index 3f46f31..b370210 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/exception/ExcelException.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/exception/ExcelException.java @@ -1,4 +1,6 @@ -package com.gitee.cliveyuan.tools.exception; +package cn.cliveyuan.tools.poi.exception; + +import cn.cliveyuan.tools.common.exception.BaseException; /** * excel异常 diff --git a/src/main/java/com/gitee/cliveyuan/tools/ExcelTools.java b/poi/src/main/java/cn/cliveyuan/tools/poi/inner/ExcelInnerHelper.java similarity index 57% rename from src/main/java/com/gitee/cliveyuan/tools/ExcelTools.java rename to poi/src/main/java/cn/cliveyuan/tools/poi/inner/ExcelInnerHelper.java index 3f9b4fa..2f4150b 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/ExcelTools.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/inner/ExcelInnerHelper.java @@ -1,15 +1,20 @@ -package com.gitee.cliveyuan.tools; - -import com.gitee.cliveyuan.tools.bean.excel.ExcelGenerateParam; -import com.gitee.cliveyuan.tools.bean.excel.SheetData; -import com.gitee.cliveyuan.tools.bean.excel.SheetInfo; -import com.gitee.cliveyuan.tools.data.TableUtils; -import com.gitee.cliveyuan.tools.data.excel.ExcelReader; -import com.gitee.cliveyuan.tools.data.excel.ExcelWriter; -import com.gitee.cliveyuan.tools.exception.ExcelException; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; +package cn.cliveyuan.tools.poi.inner; + + +import cn.cliveyuan.tools.common.ArrayTools; +import cn.cliveyuan.tools.common.AssertTools; +import cn.cliveyuan.tools.common.FileTools; +import cn.cliveyuan.tools.common.StringTools; +import cn.cliveyuan.tools.common.TableUtils; +import cn.cliveyuan.tools.poi.bean.ExcelReader; +import cn.cliveyuan.tools.poi.bean.ExcelType; +import cn.cliveyuan.tools.poi.bean.ExcelWriter; +import cn.cliveyuan.tools.poi.bean.SheetContent; +import cn.cliveyuan.tools.poi.bean.SheetData; +import cn.cliveyuan.tools.poi.bean.SheetInfo; +import cn.cliveyuan.tools.poi.exception.ExcelException; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; @@ -19,19 +24,31 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.beans.PropertyDescriptor; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.text.DecimalFormat; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; -import static com.gitee.cliveyuan.tools.enums.ExcelType.*; +import static cn.cliveyuan.tools.poi.bean.ExcelType.XLS; +import static cn.cliveyuan.tools.poi.bean.ExcelType.XLSM; +import static cn.cliveyuan.tools.poi.bean.ExcelType.XLSX; /** * @author clive @@ -39,11 +56,11 @@ import static com.gitee.cliveyuan.tools.enums.ExcelType.*; * @since 1.0 */ @Slf4j -public class ExcelTools { +public class ExcelInnerHelper { - private static final Logger logger = LoggerFactory.getLogger(ExcelTools.class); + private static final Logger logger = LoggerFactory.getLogger(ExcelInnerHelper.class); - private ExcelTools() { + private ExcelInnerHelper() { } /** @@ -54,22 +71,22 @@ public class ExcelTools { */ public static List read(ExcelReader excelReader) { if (Objects.isNull(excelReader)) { - return Lists.newArrayList(); + return new ArrayList(); } - Assert.notNull(excelReader, "excelReader is required"); - List resultList = Lists.newArrayList(); + AssertTools.notNull(excelReader, "excelReader is required"); + List resultList = new ArrayList(); List sheetInfoList = excelReader.getSheetInfoList(); - Assert.notEmpty(sheetInfoList, "sheetInfoList can't be empty"); + AssertTools.notEmpty(sheetInfoList, "sheetInfoList can't be empty"); String[] sheetNames = sheetInfoList.stream().map(SheetInfo::getSheetName).toArray(String[]::new); Map sheetInfoMap = sheetInfoList.stream().collect(Collectors.toMap(SheetInfo::getSheetName, Function.identity())); - Map> sheetDataMap = ExcelTools.readRawExcel(excelReader.getExcelPath(), sheetNames); + Map> sheetDataMap = ExcelInnerHelper.readRawExcel(excelReader.getPathname(), sheetNames); sheetDataMap.forEach((sheetName, dataArray) -> { SheetInfo sheetInfo = sheetInfoMap.get(sheetName); if (Objects.isNull(sheetInfo)) { log.warn("can't find sheetInfo of sheetName " + sheetName); throw ExcelException.dataConvertError(); } - SheetData sheetData = new SheetData(sheetName, TableUtils.dataToObject(excelReader.getSkipRowNo(), dataArray, sheetInfo.getClazz())); + SheetData sheetData = new SheetData(sheetName, TableUtils.dataToObject(excelReader.getStartRowNo(), dataArray, sheetInfo.getClazz())); resultList.add(sheetData); } ); @@ -83,13 +100,8 @@ public class ExcelTools { * @return */ public static File write(ExcelWriter excelWriter) { - Assert.notNull(excelWriter, "excelWriter is required"); - ExcelGenerateParam param = ExcelGenerateParam.builder() - .filePath(excelWriter.getFilePath()) - .excelType(excelWriter.getExcelType()) - .sheetContentList(excelWriter.getSheetContentList()) - .build(); - return TableUtils.generate(param); + AssertTools.notNull(excelWriter, "excelWriter is required"); + return generate(excelWriter); } /** @@ -107,9 +119,9 @@ public class ExcelTools { //region private methods private static Map> doReadRawExcel(String absoluteFilePath, String... sheetNames) { - Assert.notBlank(absoluteFilePath, "excel file path is empty"); + AssertTools.notBlank(absoluteFilePath, "excel file path is empty"); File file = new File(absoluteFilePath); - Assert.isTrue(file.exists(), "excel file not exists"); + AssertTools.isTrue(file.exists(), "excel file not exists"); String extension = FileTools.getExtension(absoluteFilePath); if (!XLS.name().equalsIgnoreCase(extension) && !XLSX.name().equalsIgnoreCase(extension) @@ -117,7 +129,7 @@ public class ExcelTools { ) { throw ExcelException.notSupport(extension); } - Map> map = Maps.newHashMap(); + Map> map = new HashMap(); try { // 检查文件 // 获得Workbook工作薄对象 @@ -125,7 +137,7 @@ public class ExcelTools { // 仅读取第一个sheet, 如需读取其他sheet, 需自行封装方法 获取sheet数量:workbook.getNumberOfSheets() // 创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回 int numberOfSheets = workbook.getNumberOfSheets(); - List sheets = Lists.newArrayList(); + List sheets = new ArrayList(); for (int i = 0; i < numberOfSheets; i++) { Sheet sheet = workbook.getSheetAt(i); if (Objects.isNull(sheet)) { @@ -133,14 +145,14 @@ public class ExcelTools { } sheets.add(sheet); } - if (CollectionTools.isNotEmpty(sheetNames)) { + if (ArrayTools.isNotEmpty(sheetNames)) { List sheetNameList = Arrays.asList(sheetNames); List collect = sheets.stream().filter(x -> sheetNameList.contains(x.getSheetName())).collect(Collectors.toList()); sheets.clear(); sheets.addAll(collect); } for (Sheet sheet : sheets) { - List list = Lists.newArrayList(); + List list = new ArrayList(); // 获得当前sheet的开始行 int firstRowNum = sheet.getFirstRowNum(); @@ -232,5 +244,81 @@ public class ExcelTools { } return cellValue; } + + private static File generate(ExcelWriter param) { + String filePath = param.getFilePath(); + ExcelType excelType = param.getFileType(); + AssertTools.notBlank(filePath, "file path can't be empty"); + AssertTools.notNull(excelType, "excel type is required"); + AssertTools.notEmpty(param.getSheetContentList(), "sheetContentList can't be empty"); + + File file = new File(filePath); + if (!file.exists()) { + boolean result = file.mkdirs(); + if (!result) throw ExcelException.failToMkdirs(); + } + String name = param.getFileName(); + if (StringTools.isBlank(name)) { + name = UUID.randomUUID().toString(); + } + String fileName = name + "." + excelType.name().toLowerCase(); + if (!filePath.endsWith("/")) filePath = filePath + "/"; + file = new File(filePath + fileName); + try (FileOutputStream fos = new FileOutputStream(file)) { + Workbook wb; + if (excelType.equals(XLS)) wb = new HSSFWorkbook(); + else wb = new XSSFWorkbook(); + + for (SheetContent sheetContent : param.getSheetContentList()) { + String[] headers = sheetContent.getHeaders(); + Collection data = sheetContent.getData(); + AssertTools.isTrue(ArrayTools.isNotEmpty(headers), "headers can't be empty"); + AssertTools.isTrue(CollectionUtils.isNotEmpty(data), "data can't be empty"); + + Sheet sheet = wb.createSheet(sheetContent.getSheetName()); + Row firstRow = sheet.createRow(0); + //创建表头 + for (int i = 0; i < headers.length; i++) { + Cell cell = firstRow.createCell(i); + cell.setCellValue(headers[i]); + } + + //处理数据 + Iterator it = data.iterator(); + int index = 0; + while (it.hasNext()) { + index++; + Row row = sheet.createRow(index); + Object t = it.next(); + //反射 + Class clazz = t.getClass(); + Field[] fields = clazz.getDeclaredFields(); + for (int i = 0; i < fields.length; i++) { + Field field = fields[i]; + PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz); + Method getMethod = pd.getReadMethod(); + if (getMethod != null) { + Object invoke = getMethod.invoke(t); + String value = StringTools.EMPTY; + if (invoke != null) { + if (invoke instanceof Date) { + value = TableUtils.SDF.format((Date) invoke); + } else value = invoke.toString(); + } + Cell cell = row.createCell(i); + cell.setCellValue(value); + } + + } + } + } + wb.write(fos); + return file; + } catch (Exception e) { + log.error("excel generate exception", e); + throw ExcelException.failToGenerateExcel(); + } + } + //endregion } diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/ExcelToolsTest.java b/poi/src/test/java/cn/cliveyuan/tools/poi/test/ExcelToolsTest.java similarity index 56% rename from src/test/java/com/gitee/cliveyuan/tools/test/ExcelToolsTest.java rename to poi/src/test/java/cn/cliveyuan/tools/poi/test/ExcelToolsTest.java index 27bd0fc..46e49c4 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/ExcelToolsTest.java +++ b/poi/src/test/java/cn/cliveyuan/tools/poi/test/ExcelToolsTest.java @@ -1,17 +1,17 @@ -package com.gitee.cliveyuan.tools.test; +package cn.cliveyuan.tools.poi.test; -import com.gitee.cliveyuan.tools.ExcelTools; -import com.gitee.cliveyuan.tools.bean.excel.SheetData; -import com.gitee.cliveyuan.tools.data.excel.ExcelReader; -import com.gitee.cliveyuan.tools.data.excel.ExcelWriter; -import com.gitee.cliveyuan.tools.test.bean.excel.ExcelIdea; -import com.gitee.cliveyuan.tools.test.bean.excel.ExcelProfile; -import com.gitee.cliveyuan.tools.test.bean.excel.ExcelWebsite; -import com.google.common.collect.Lists; +import cn.cliveyuan.tools.poi.ExcelTools; +import cn.cliveyuan.tools.poi.bean.ExcelReader; +import cn.cliveyuan.tools.poi.bean.ExcelWriter; +import cn.cliveyuan.tools.poi.bean.SheetData; +import cn.cliveyuan.tools.poi.test.bean.ExcelIdea; +import cn.cliveyuan.tools.poi.test.bean.ExcelProfile; +import cn.cliveyuan.tools.poi.test.bean.ExcelWebsite; import org.junit.Assert; import org.junit.Test; import java.io.File; +import java.util.ArrayList; import java.util.List; /** @@ -29,7 +29,7 @@ public class ExcelToolsTest { String[] headers2 = {"name", "url"}; String[] headers3 = {"ide", "lang",}; - List data1 = Lists.newArrayList(); + List data1 = new ArrayList(); for (int i = 0; i < 10; i++) { data1.add(ExcelProfile.builder() .no(i + 1) @@ -39,15 +39,15 @@ public class ExcelToolsTest { .build() ); } - List data2 = Lists.newArrayList(); + List data2 = new ArrayList(); for (int i = 0; i < 10; i++) { data2.add(ExcelWebsite.builder() - .name("gat-" + i) - .url("www.guanaitong.com/" + i) + .name("clive-" + i) + .url("cliveyuan.cn/" + i) .build() ); } - List data3 = Lists.newArrayList(); + List data3 = new ArrayList(); for (int i = 0; i < 10; i++) { data3.add(ExcelIdea.builder() .ide("idea-" + i) @@ -59,6 +59,7 @@ public class ExcelToolsTest { String filePath = ExcelToolsTest.class.getResource("/").getPath(); File generatedFile = ExcelTools.write(ExcelWriter.builder() .filePath(filePath) + .fileName("测试") .build() .addSheetContent("profile", headers1, data1) .addSheetContent("website", headers2, data2) @@ -68,7 +69,8 @@ public class ExcelToolsTest { List excelDataList = ExcelTools.read(ExcelReader .builder() - .excelPath(generatedFile.getAbsolutePath()) + .pathname(generatedFile.getAbsolutePath()) + .skipFirstRow(true) .build() .addSheetInfo("profile", ExcelProfile.class) .addSheetInfo("website", ExcelWebsite.class) @@ -81,4 +83,25 @@ public class ExcelToolsTest { System.out.println("\n"); }); } + + @Test + public void writeAndReadSimply() { + String pathname = ExcelToolsTest.class.getResource("/").getPath(); + String[] headers = {"编号", "名字", "手机号", "年龄"}; + List data = new ArrayList(); + for (int i = 0; i < 10; i++) { + data.add(ExcelProfile.builder() + .no(i + 1) + .name("Clive-" + i) + .mobile("138001380" + i) + .age(i + 20) + .build() + ); + } + File file = ExcelTools.writeSimply(pathname, headers, data); + System.out.println("generatedFile:" + file.getAbsolutePath()); + List excelProfiles = ExcelTools.readSimply(file.getAbsolutePath(), ExcelProfile.class, true); + Assert.assertFalse(excelProfiles.isEmpty()); + excelProfiles.forEach(System.out::println); + } } diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelData.java b/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelData.java similarity index 98% rename from src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelData.java rename to poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelData.java index 68b53be..89e1284 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelData.java +++ b/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelData.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.test.bean.excel; +package cn.cliveyuan.tools.poi.test.bean; /** * @author clive diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelIdea.java b/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelIdea.java similarity index 84% rename from src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelIdea.java rename to poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelIdea.java index 23f3336..c2a9024 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelIdea.java +++ b/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelIdea.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.test.bean.excel; +package cn.cliveyuan.tools.poi.test.bean; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelProfile.java b/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelProfile.java similarity index 86% rename from src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelProfile.java rename to poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelProfile.java index 29e42e6..cafcd5d 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelProfile.java +++ b/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelProfile.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.test.bean.excel; +package cn.cliveyuan.tools.poi.test.bean; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelWebsite.java b/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelWebsite.java similarity index 85% rename from src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelWebsite.java rename to poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelWebsite.java index 95b2901..ec171ee 100644 --- a/src/test/java/com/gitee/cliveyuan/tools/test/bean/excel/ExcelWebsite.java +++ b/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/ExcelWebsite.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.test.bean.excel; +package cn.cliveyuan.tools.poi.test.bean; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/Person.java b/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/Person.java new file mode 100644 index 0000000..f5242ad --- /dev/null +++ b/poi/src/test/java/cn/cliveyuan/tools/poi/test/bean/Person.java @@ -0,0 +1,190 @@ +package cn.cliveyuan.tools.poi.test.bean; + +import java.math.BigDecimal; +import java.util.Date; + +/** + * 测试对象: 人 + * + * @author clive + * Created on 2018/07/25 + * @since 1.0 + */ +public class Person { + + private String name; + private String gender; + private int age; + private Date birthday; + private double height; + private float weight; + private String position; + private String nation; + private long liveSecond; + private boolean programmer; + private BigDecimal salary; + + public static Person build() { + return new Person(); + } + + public String getName() { + return name; + } + + public Person Name(String name) { + this.name = name; + return this; + } + + public String getGender() { + return gender; + } + + public Person Gender(String gender) { + this.gender = gender; + return this; + } + + public int getAge() { + return age; + } + + public Person Age(int age) { + this.age = age; + return this; + } + + public Date getBirthday() { + return birthday; + } + + public Person Birthday(Date birthday) { + this.birthday = birthday; + return this; + } + + public double getHeight() { + return height; + } + + public Person Height(double height) { + this.height = height; + return this; + } + + public float getWeight() { + return weight; + } + + public Person Weight(float weight) { + this.weight = weight; + return this; + } + + public String getPosition() { + return position; + } + + public Person Position(String position) { + this.position = position; + return this; + } + + public String getNation() { + return nation; + } + + public Person Nation(String nation) { + this.nation = nation; + return this; + } + + public long getLiveSecond() { + return liveSecond; + } + + public Person LiveSecond(long liveSecond) { + this.liveSecond = liveSecond; + return this; + } + + public boolean isProgrammer() { + return programmer; + } + + public Person Programmer(boolean programmer) { + this.programmer = programmer; + return this; + } + + public BigDecimal getSalary() { + return salary; + } + + public Person Salary(BigDecimal salary) { + this.salary = salary; + return this; + } + + public void setName(String name) { + this.name = name; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public void setAge(int age) { + this.age = age; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } + + public void setHeight(double height) { + this.height = height; + } + + public void setWeight(float weight) { + this.weight = weight; + } + + public void setPosition(String position) { + this.position = position; + } + + public void setNation(String nation) { + this.nation = nation; + } + + public void setLiveSecond(long liveSecond) { + this.liveSecond = liveSecond; + } + + public void setProgrammer(boolean programmer) { + this.programmer = programmer; + } + + public void setSalary(BigDecimal salary) { + this.salary = salary; + } + + @Override + public String toString() { + return "Person{" + + "name='" + name + '\'' + + ", gender='" + gender + '\'' + + ", age=" + age + + ", birthday=" + birthday + + ", height=" + height + + ", weight=" + weight + + ", position='" + position + '\'' + + ", nation='" + nation + '\'' + + ", liveSecond=" + liveSecond + + ", programmer=" + programmer + + ", salary=" + salary + + '}'; + } +} diff --git a/pom.xml b/pom.xml index 38f2b37..f90eea0 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,14 @@ 4.0.0 com.gitee.opensource4clive java-tools - 3.1.0 + pom + 4.0.0 + + poi + common + web + httpclient + java-tools Some commonly used methods in java https://gitee.com/opensource4clive/java-tools @@ -39,7 +46,6 @@ 1.8 UTF-8 3.2 - 1.7.25 1.2.70 1.6.1 25.1-jre @@ -53,137 +59,131 @@ 4.0.0 0.4.8 1.3.2 - 1.18.8 + + + + + com.gitee.opensource4clive + java-tools-common + ${project.version} + + + + org.slf4j + slf4j-api + 1.7.25 + + + + junit + junit + 4.12 + test + + + + org.projectlombok + lombok + 1.18.8 + + + + org.apache.poi + poi + ${poi.version} + + + org.apache.poi + poi-ooxml + ${poi.version} + + + + com.squareup.okhttp3 + okhttp + 3.14.9 + + + + org.apache.commons + commons-collections4 + 4.3 + + + commons-io + commons-io + ${commons-io.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + commons-codec + commons-codec + ${commons-codec.version} + + + + org.hibernate.validator + hibernate-validator + 6.1.5.Final + + + org.glassfish + jakarta.el + 3.0.3 + compile + + + + net.lingala.zip4j + zip4j + ${zip4j.version} + + + + com.alibaba + fastjson + ${fastjson.version} + + + + javax.mail + javax.mail-api + ${email.version} + + + com.sun.mail + javax.mail + ${email.version} + + + + javax.servlet + javax.servlet-api + ${javax.servlet.version} + provided + + + + org.springframework + spring-web + ${spring.version} + + + + - - - net.lingala.zip4j - zip4j - ${zip4j.version} - - - - org.slf4j - slf4j-api - ${slf4j.version} - - - org.slf4j - slf4j-log4j12 - test - 1.7.5 - - + junit junit - 4.12 test - - - commons-io - commons-io - ${commons-io.version} - - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - - com.alibaba - fastjson - ${fastjson.version} - - - - com.google.guava - guava - ${guava.version} - - - - javax.mail - javax.mail-api - ${email.version} - - - com.sun.mail - javax.mail - ${email.version} - - - - commons-codec - commons-codec - ${commons-codec.version} - - - - org.jsoup - jsoup - ${jsoup.version} - - - - org.apache.httpcomponents - httpclient - ${httpclient.version} - - - org.apache.httpcomponents - httpmime - ${httpclient.version} - - - - org.apache.poi - poi - ${poi.version} - - - org.apache.poi - poi-ooxml - ${poi.version} - - - - org.springframework - spring-web - ${spring.version} - - - - javax.servlet - javax.servlet-api - ${javax.servlet.version} - provided - - - - net.coobird - thumbnailator - ${thumbnailator.version} - - - - org.projectlombok - lombok - ${lombok.version} - - - - org.bouncycastle - bcprov-jdk15on - 1.64 - - - diff --git a/src/main/java/com/gitee/cliveyuan/tools/CollectionTools.java b/src/main/java/com/gitee/cliveyuan/tools/CollectionTools.java deleted file mode 100644 index 6fa5c6f..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/CollectionTools.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.gitee.cliveyuan.tools; - -import java.util.Collection; -import java.util.Map; -import java.util.Objects; - -/** - * 集合工具类 - * - * @author clive - * Created on 2018/07/23 - * @since 1.0 - */ -public class CollectionTools { - - private CollectionTools() { - } - - /** - * 集合是否为空 - * - * @param collection 集合 - */ - public static boolean isEmpty(Collection collection) { - return collection == null || collection.isEmpty(); - } - - /** - * 集合是否不为空 - * - * @param collection 集合 - */ - public static boolean isNotEmpty(Collection collection) { - return !isEmpty(collection); - } - - /** - * map是否为空 - * - * @param map map - */ - public static boolean isEmpty(Map map) { - return map == null || map.isEmpty(); - } - - /** - * map是否不为空 - * - * @param map map - */ - public static boolean isNotEmpty(Map map) { - return !isEmpty(map); - } - - /** - * 数组是否空 - * - * @param array 数组 - */ - public static boolean isEmpty(Object[] array) { - return Objects.isNull(array) || array.length == 0; - } - - /** - * 数组不为空 - * - * @param array 数组 - */ - public static boolean isNotEmpty(Object[] array) { - return !isEmpty(array); - } - - public static boolean isEmpty(int[] array) { - return Objects.isNull(array) || array.length == 0; - } - - public static boolean isNotEmpty(int[] array) { - return !isEmpty(array); - } - - public static boolean isEmpty(long[] array) { - return Objects.isNull(array) || array.length == 0; - } - - public static boolean isNotEmpty(long[] array) { - return !isEmpty(array); - } - - public static boolean isEmpty(float[] array) { - return Objects.isNull(array) || array.length == 0; - } - - public static boolean isNotEmpty(float[] array) { - return !isEmpty(array); - } - - public static boolean isEmpty(double[] array) { - return Objects.isNull(array) || array.length == 0; - } - - public static boolean isNotEmpty(double[] array) { - return !isEmpty(array); - } - -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/MathTools.java b/src/main/java/com/gitee/cliveyuan/tools/MathTools.java deleted file mode 100644 index cd96d2d..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/MathTools.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.gitee.cliveyuan.tools; - -import java.util.Random; - -/** - * 数学相关工具 - * - * @author clive - * Created on 2018/07/24 - * @since 1.0 - */ -public class MathTools { - - private MathTools(){} - - public static int randomInt(int min, int max) { - if (min == max) return min; - Assert.isTrue(min < max, "min must great than max"); - Random random = new Random(); - return random.nextInt(max) % (max - min + 1) + min; - } - - -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/action/Action.java b/src/main/java/com/gitee/cliveyuan/tools/action/Action.java deleted file mode 100644 index 9369489..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/action/Action.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.gitee.cliveyuan.tools.action; - -/** - * @author clive - * Created on 2018/08/01 - * @since 1.0 - */ -public interface Action { - - void invoke(); -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/action/Action2.java b/src/main/java/com/gitee/cliveyuan/tools/action/Action2.java deleted file mode 100644 index e6123fd..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/action/Action2.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.gitee.cliveyuan.tools.action; - -/** - * @author clive - * Created on 2018/08/01 - * @since 1.0 - */ -public interface Action2 { - void invoke(T t); -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/action/Action3.java b/src/main/java/com/gitee/cliveyuan/tools/action/Action3.java deleted file mode 100644 index 8760c06..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/action/Action3.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.gitee.cliveyuan.tools.action; - -/** - * @author clive - * Created on 2018/08/01 - * @since 1.0 - */ -public interface Action3 { - void invoke(T t, T2 t2); -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/action/Action4.java b/src/main/java/com/gitee/cliveyuan/tools/action/Action4.java deleted file mode 100644 index 07fa8a3..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/action/Action4.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.gitee.cliveyuan.tools.action; - -/** - * @author clive - * Created on 2018/08/01 - * @since 1.0 - */ -public interface Action4 { - void invoke(T t, T2 t2, T3 t3); -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/action/Action5.java b/src/main/java/com/gitee/cliveyuan/tools/action/Action5.java deleted file mode 100644 index 40726b0..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/action/Action5.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.gitee.cliveyuan.tools.action; - -/** - * @author clive - * Created on 2018/08/01 - * @since 1.0 - */ -public interface Action5 { - void invoke(T t, T2 t2, T3 t3, T4 t4); -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/excel/ExcelGenerateParam.java b/src/main/java/com/gitee/cliveyuan/tools/bean/excel/ExcelGenerateParam.java deleted file mode 100644 index 025b9f6..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/excel/ExcelGenerateParam.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.gitee.cliveyuan.tools.bean.excel; - -import com.gitee.cliveyuan.tools.enums.ExcelType; -import com.google.common.collect.Lists; -import lombok.Builder; -import lombok.Data; - -import java.io.Serializable; -import java.util.Collection; -import java.util.List; - -/** - * Created by Clive on 2019/10/28. - */ -@Data -@Builder -public class ExcelGenerateParam implements Serializable { - private String filePath; - private ExcelType excelType; - @Builder.Default - private List sheetContentList = Lists.newArrayList(); - - public ExcelGenerateParam addSheetContent(String sheetName, String[] headers, Collection data) { - this.sheetContentList.add(new SheetContent(sheetName, headers, data)); - return this; - } - - -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/cons/NetCons.java b/src/main/java/com/gitee/cliveyuan/tools/cons/NetCons.java deleted file mode 100644 index 5be140d..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/cons/NetCons.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.gitee.cliveyuan.tools.cons; - -import com.google.common.collect.Maps; - -import java.util.Map; - -/** - * 网络常量 - * - * @author clive - * Created on 2018/07/24 - * @since 1.0 - */ -public class NetCons { - - /** - * 默认请求头 - */ - public static final Map DEFAULT_HEADERS = Maps.newHashMap(); - - - /** - * jsop默认超时时间, 单位:毫秒 - */ - public static final int JSOUP_TIME_OUT = 30 * 1000; - /** - * httpClient默认超时时间, 单位:毫秒 - */ - public static final int HTTP_CLIENT_TIME_OUT = 30 * 1000; - - static { - DEFAULT_HEADERS.put("Accept", "*/*"); - DEFAULT_HEADERS.put("Accept-Encoding", "gzip, deflate"); - DEFAULT_HEADERS.put("Accept-Language", "en-US,en;q=0.9"); - DEFAULT_HEADERS.put("Connection", "keep-alive"); - DEFAULT_HEADERS.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) " - + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"); - DEFAULT_HEADERS.put("X-Requested-With", "XMLHttpRequest"); - } -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/exception/CsvException.java b/src/main/java/com/gitee/cliveyuan/tools/exception/CsvException.java deleted file mode 100644 index f78a866..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/exception/CsvException.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.gitee.cliveyuan.tools.exception; - -/** - * CSV异常 - * - * @author clive - * Created on 2018/07/27 - * @since 1.0 - */ -public class CsvException extends BaseException { - - public CsvException(int code, String message) { - super(code, message); - } - - public static CsvException failToMkdirs() { - return new CsvException(1000, "fail to create folder(s)"); - } - public static CsvException failToGenerateCsv() { - return new CsvException(1001, "fail to generate excel"); - } - public static CsvException failToParseCsv() { - return new CsvException(1004, "fail to parse excel"); - } - public static CsvException notSupport(String extension) { - return new CsvException(1005, "not support this csv format:" + extension); - } - -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/exception/NetException.java b/src/main/java/com/gitee/cliveyuan/tools/exception/NetException.java deleted file mode 100644 index 13ef13b..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/exception/NetException.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.gitee.cliveyuan.tools.exception; - -/** - * 网络异常 - * - * @author clive - * Created on 2018/07/24 - * @since 1.0 - */ -public class NetException extends BaseException { - - public NetException(int code, String message) { - super(code, message); - } - - public static NetException unknownHost() { - return new NetException(1000, "Unknown Host Error"); - } - - public static NetException httpStatusError(int statusCode) { - return new NetException(1001, "Http Status Error, statusCode:" + statusCode); - } - - public static NetException timeout(int timeout) { - return new NetException(1002, "Http Connect Timeout: " + timeout + " ms"); - } - - public static NetException unKnowError() { - return new NetException(1003, "UnKnow Error"); - } - - public static NetException illegalInvokeError(String msg) { - return new NetException(1004, "Illegal Invoke Error: " + msg); - } - - public static NetException connectionRefused(String url) { - return new NetException(1005, "[Connection Refused] Fail To Connect " + url); - } - -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/http/AbstractHttpRequest.java b/src/main/java/com/gitee/cliveyuan/tools/http/AbstractHttpRequest.java deleted file mode 100644 index f7976c2..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/http/AbstractHttpRequest.java +++ /dev/null @@ -1,260 +0,0 @@ -package com.gitee.cliveyuan.tools.http; - - -import com.alibaba.fastjson.JSONObject; -import com.gitee.cliveyuan.tools.StringTools; -import com.gitee.cliveyuan.tools.exception.NetException; -import com.google.common.collect.Maps; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; - -import java.util.Map; - -/** - * Created by clive at 2018/11/26. - */ -public abstract class AbstractHttpRequest { - - String url; - Map params = Maps.newHashMap(); - Map headers = Maps.newHashMap(); - Map cookies = Maps.newHashMap(); - int timeout; - boolean addDefaultHeaders; - /** - * 忽略SSL证书 - */ - boolean ignoreSslCertificate; - - /** - * 设置请求url地址 - * - * @param url http or https 请求地址 - */ - public AbstractHttpRequest url(String url) { - this.url = url; - return this; - } - - /** - * 设置请求入参, 如果是get请求时此入参将拼接到url后 - * - * @param params 入参map - */ - public AbstractHttpRequest params(Map params) { - this.params = params; - return this; - } - - /** - * 通过字符串设置请求入参, 如果是get请求时此入参将拼接到url后 - * 格式: - *
-     *     user: clive
-     *     pwd:  123456
-     * 
- * 每行一对,每对之间用分号分隔对应key-value - * 会去除前后空格 - * - * @param kv - */ - public AbstractHttpRequest params(String kv) { - this.params = InnerTools.keyValueStrToMap(kv); - return this; - } - - /** - * 添加入参 - * - * @param key - * @param value - */ - public AbstractHttpRequest addParam(String key, String value) { - this.params.put(key, value); - return this; - } - - /** - * 设置请求头信息 - * - * @param headers - */ - public AbstractHttpRequest headers(Map headers) { - this.headers = headers; - return this; - } - - /** - * 设置请求头信息 - * 格式: - *
-     *     token: 123456
-     *     uid:   111111
-     * 
- * 每行一对,每对之间用分号分隔对应key-value - * 会去除前后空格 - * - * @param kv - */ - public AbstractHttpRequest headers(String kv) { - this.headers = InnerTools.keyValueStrToMap(kv); - return this; - } - - /** - * 添加请求头 - * - * @param key - * @param value - */ - public AbstractHttpRequest addHeader(String key, String value) { - this.headers.put(key, value); - return this; - } - - /** - * 设置cookie信息 - * - * @param cookies - */ - public AbstractHttpRequest cookies(Map cookies) { - this.cookies = cookies; - return this; - } - - /** - * 设置cookie信息 - *

- * 格式: - *

-     *     JSESSIONID: 01010101010101010
-     *     _uid:   111111
-     * 
- * 每行一对,每对之间用分号分隔对应key-value - * 会去除前后空格 - * - * @param kv - */ - public AbstractHttpRequest cookies(String kv) { - this.cookies = InnerTools.keyValueStrToMap(kv); - return this; - } - - /** - * 添加cookie信息 - * - * @param key - * @param value - */ - public AbstractHttpRequest addCookie(String key, String value) { - this.cookies.put(key, value); - return this; - } - - /** - * 超时时间 - * 单位:毫秒, 默认:30000 - * - * @param timeout - */ - public AbstractHttpRequest timeout(int timeout) { - this.timeout = timeout; - return this; - } - - /** - * 添加默认的请求头 - */ - public AbstractHttpRequest addDefaultHeaders() { - this.addDefaultHeaders = true; - return this; - } - - /** - * 忽略SSL证书 - */ - public AbstractHttpRequest ignoreSslCertificate() { - this.ignoreSslCertificate = true; - return this; - } - - /** - * 发送get请求 - * - * @return 响应内容 - * @throws NetException - */ - public abstract String get() throws NetException; - - /** - * 发送post请求 - * - * @return 响应内容 - * @throws NetException - */ - public abstract String post() throws NetException; - - /** - * 执行get请求 - * - * @return 返回响应数据 - * @throws NetException - */ - public abstract HttpResp executeGet() throws NetException; - - /** - * 执行post请求 - * - * @return 返回响应数据 - * @throws NetException - */ - public abstract HttpResp executePost() throws NetException; - - - /** - * 发送get请求,并将结果转为Jsoup的Document - * - * @throws NetException - */ - public Document getByDocument() throws NetException { - return Jsoup.parse(get()); - } - - /** - * 发送get请求,并将结果转为JSON - * - * @return - */ - public JSONObject getByJson() { - return JSONObject.parseObject(this.get()); - } - - /** - * 发送post请求,并将结果转为JSON - * - * @return - */ - public JSONObject postByJson() { - return JSONObject.parseObject(this.post()); - } - - /** - * 发送post请求,并将结果转为Jsoup的Document - * - * @throws NetException - */ - public Document postByDocument() throws NetException { - return Jsoup.parse(post()); - } - - void validate() throws NetException { - String url = this.url; - if (StringTools.isBlank(url)) { - throw NetException.illegalInvokeError("url is required"); - } - url = url.toLowerCase(); - if (!url.startsWith("http://") && !url.startsWith("https://")) { - throw NetException.illegalInvokeError("url should start with http(s)://"); - } - } -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/http/HtmlTools.java b/src/main/java/com/gitee/cliveyuan/tools/http/HtmlTools.java deleted file mode 100644 index 00a6bfd..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/http/HtmlTools.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.gitee.cliveyuan.tools.http; - -import org.springframework.web.util.HtmlUtils; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * html工具类 - * - * @author clive - * Created on 2018/07/27 - * @since 1.0 - */ -public class HtmlTools extends HtmlUtils { - - - public static final String DEFAULT_ENCODING = "UTF-8"; - - private HtmlTools() { - } - - public static String htmlEscape(String input) { - return htmlEscape(input, DEFAULT_ENCODING); - } - - public static String delHTMLTag(String htmlStr) { - String regEx_script = "]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 - String regEx_style = "]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 - String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式 - - Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); - Matcher m_script = p_script.matcher(htmlStr); - htmlStr = m_script.replaceAll(""); //过滤script标签 - - Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); - Matcher m_style = p_style.matcher(htmlStr); - htmlStr = m_style.replaceAll(""); //过滤style标签 - - Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); - Matcher m_html = p_html.matcher(htmlStr); - htmlStr = m_html.replaceAll(""); //过滤html标签 - - return htmlStr.trim(); //返回文本字符串 - } - -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/http/HttpClientTools.java b/src/main/java/com/gitee/cliveyuan/tools/http/HttpClientTools.java deleted file mode 100644 index 300700f..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/http/HttpClientTools.java +++ /dev/null @@ -1,265 +0,0 @@ -package com.gitee.cliveyuan.tools.http; - -import com.gitee.cliveyuan.tools.CollectionTools; -import com.gitee.cliveyuan.tools.StringTools; -import com.gitee.cliveyuan.tools.cons.NetCons; -import com.gitee.cliveyuan.tools.exception.NetException; -import com.google.common.collect.Lists; -import org.apache.http.Header; -import org.apache.http.HttpEntity; -import org.apache.http.NameValuePair; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.conn.ConnectTimeoutException; -import org.apache.http.conn.HttpHostConnectException; -import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.conn.ssl.TrustStrategy; -import org.apache.http.cookie.Cookie; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.BasicCookieStore; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.ssl.SSLContexts; -import org.apache.http.util.EntityUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.net.ssl.SSLContext; -import java.io.IOException; -import java.net.SocketTimeoutException; -import java.net.UnknownHostException; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.util.List; -import java.util.Objects; - -/** - * httpClient 工具 - *

- * Created by Clive at 2018/07/24. - * Rebuilt by Clive at 2018/11/26. - * - * @since 2.0.0 - */ -public class HttpClientTools extends AbstractHttpRequest { - - private static final Logger logger = LoggerFactory.getLogger(HttpClientTools.class); - - private String encoding = "UTF-8"; - private boolean isPayload; - private String payload; - private boolean throwStatusException = true; - - private HttpClientTools() { - timeout = NetCons.HTTP_CLIENT_TIME_OUT; - } - - // region public methods - - /** - * 构建HttpClient工具 - */ - public static HttpClientTools build() { - return new HttpClientTools(); - } - - @Override - public String get() throws NetException { - HttpResp httpResp = sendGet(); - return httpResp.getContent(); - } - - @Override - public String post() throws NetException { - HttpResp httpResp = sendPost(); - return httpResp.getContent(); - } - - @Override - public HttpResp executeGet() throws NetException { - return sendGet(); - } - - @Override - public HttpResp executePost() throws NetException { - return sendPost(); - } - - /** - * 编码方式 - * - * @param encoding 默认UTF-8 - */ - public HttpClientTools encoding(String encoding) { - this.encoding = encoding; - return this; - } - - /** - * 请求入参是json格式时设置此字段为true - * - * @param isPayload - */ - public HttpClientTools isPayload(boolean isPayload) { - this.isPayload = isPayload; - return this; - } - - /** - * 设置payload内容 - * - * @param payload - */ - public HttpClientTools payload(String payload) { - this.payload = payload; - return this; - } - - /** - * 当http status != 200 时不抛出异常 - */ - public AbstractHttpRequest notThrowStatusException() { - this.throwStatusException = false; - return this; - } - - // endregion public methods - - - // region private methods - - private HttpResp sendPost() throws NetException { - validate(); - BasicCookieStore cookieStore = new BasicCookieStore(); - try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build()) { - - HttpPost httpPost = new HttpPost(url); - if (isPayload) { - StringEntity requestEntity = new StringEntity(payload != null ? payload : "", encoding); - requestEntity.setContentEncoding(encoding); - httpPost.setEntity(requestEntity); - httpPost.setHeader("Content-type", "application/json"); - } else { - List nameValuePairs = Lists.newArrayList(); - if (params != null) { - params.forEach((k, v) -> - nameValuePairs.add(new BasicNameValuePair(k, v)) - ); - httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding)); - } - } - RequestConfig requestConfig = RequestConfig.custom() - .setConnectTimeout(timeout) - .setConnectionRequestTimeout(timeout) - .setSocketTimeout(timeout).build(); - httpPost.setConfig(requestConfig); - if (addDefaultHeaders) { - NetCons.DEFAULT_HEADERS.forEach(httpPost::setHeader); - } - if (CollectionTools.isNotEmpty(headers)) { - headers.forEach(httpPost::setHeader); - } - if (CollectionTools.isNotEmpty(cookies)) { - httpPost.setHeader("cookie", StringTools.map2CookieStr(cookies)); - } - try (CloseableHttpResponse response = httpClient.execute(httpPost)) { - return parseResponse(response, cookieStore); - } - } catch (NetException e) { - throw e; - } catch (UnknownHostException e) { - throw NetException.unknownHost(); - } catch (SocketTimeoutException | ConnectTimeoutException e) { - throw NetException.timeout(timeout); - } catch (HttpHostConnectException e) { - throw NetException.connectionRefused(url); - } catch (Exception e) { - logger.error("request error", e); - throw NetException.unKnowError(); - } - } - - private HttpResp sendGet() throws NetException { - validate(); - BasicCookieStore cookieStore = new BasicCookieStore(); - try (CloseableHttpClient httpClient = this.createHttpClient()) { - url = InnerTools.getQueryUrl(url, params); - HttpGet httpGet = new HttpGet(url); - RequestConfig requestConfig = RequestConfig.custom() - .setConnectTimeout(timeout) - .setConnectionRequestTimeout(timeout) - .setSocketTimeout(timeout).build(); - httpGet.setConfig(requestConfig); - if (addDefaultHeaders) { - NetCons.DEFAULT_HEADERS.forEach(httpGet::setHeader); - } - if (CollectionTools.isNotEmpty(headers)) { - headers.forEach(httpGet::setHeader); - } - if (CollectionTools.isNotEmpty(cookies)) { - httpGet.setHeader("cookie", StringTools.map2CookieStr(cookies)); - } - try (CloseableHttpResponse response = httpClient.execute(httpGet)) { - return parseResponse(response, cookieStore); - } - } catch (NetException e) { - throw e; - } catch (UnknownHostException e) { - throw NetException.unknownHost(); - } catch (SocketTimeoutException | ConnectTimeoutException e) { - throw NetException.timeout(timeout); - } catch (HttpHostConnectException e) { - throw NetException.connectionRefused(url); - } catch (Exception e) { - logger.error("request error", e); - e.printStackTrace(); - throw NetException.unKnowError(); - } - } - - private HttpResp parseResponse(CloseableHttpResponse response, BasicCookieStore cookieStore) throws IOException, NetException { - List cookies = cookieStore.getCookies(); - HttpResp httpResp = HttpResp.create(); - cookies.forEach(cookie -> - httpResp.addCookie(cookie.getName(), cookie.getValue()) - ); - int statusCode = response.getStatusLine().getStatusCode(); - httpResp.setStatusCode(statusCode); - if (this.throwStatusException && statusCode != 200) { - logger.info("http status error: statusCode={}, url={}", statusCode, url); - } - HttpEntity entity = response.getEntity(); - if (Objects.nonNull(entity)) { - Header[] headers = response.getAllHeaders(); - if (CollectionTools.isNotEmpty(headers)) { - for (Header header : headers) { - httpResp.addHeader(header.getName(), header.getValue()); - } - } - httpResp.setContent(EntityUtils.toString(entity, encoding)); - } - return httpResp; - } - - private CloseableHttpClient createHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { - BasicCookieStore cookieStore = new BasicCookieStore(); - if (ignoreSslCertificate) { - SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, (TrustStrategy) (x509Certificates, s) -> true).build(); - //创建httpClient - return HttpClients.custom().setSSLContext(sslContext).setDefaultCookieStore(cookieStore). - setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); - } - return HttpClients.custom().setDefaultCookieStore(cookieStore).build(); - - } - - - // endregion private methods - - -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/http/HttpResp.java b/src/main/java/com/gitee/cliveyuan/tools/http/HttpResp.java deleted file mode 100644 index 90c9bc4..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/http/HttpResp.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.gitee.cliveyuan.tools.http; - -import com.google.common.collect.Maps; -import lombok.ToString; - -import java.util.Map; - -/** - * Created by clive at 2018/11/27. - */ -@ToString -public class HttpResp { - - private int statusCode = 200; - private String content; - private Map cookies = Maps.newHashMap(); - private Map headers = Maps.newHashMap(); - - public HttpResp() { - } - - public HttpResp(String content) { - this.content = content; - } - - public HttpResp(String content, Map cookies, Map headers) { - this.content = content; - this.cookies = cookies; - this.headers = headers; - } - - public static HttpResp create() { - return new HttpResp(); - } - - public static HttpResp create(String content) { - return new HttpResp(content); - } - - - public static HttpResp create(String content, Map cookies, Map headers) { - return new HttpResp(content, cookies, headers); - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public Map getCookies() { - return cookies; - } - - public void setCookies(Map cookies) { - this.cookies = cookies; - } - - public void addCookie(String key, String value) { - this.cookies.put(key, value); - } - - public Map getHeaders() { - return headers; - } - - public void setHeaders(Map headers) { - this.headers = headers; - } - - public void addHeader(String key, String value) { - this.headers.put(key, value); - } - - public int getStatusCode() { - return statusCode; - } - - public void setStatusCode(int statusCode) { - this.statusCode = statusCode; - } -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/http/InnerTools.java b/src/main/java/com/gitee/cliveyuan/tools/http/InnerTools.java deleted file mode 100644 index cfa30d7..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/http/InnerTools.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.gitee.cliveyuan.tools.http; - -import com.gitee.cliveyuan.tools.CollectionTools; -import com.gitee.cliveyuan.tools.StringTools; -import com.google.common.collect.Maps; - -import java.util.Map; - -/** - * Created by clive at 2018/11/26. - */ -class InnerTools { - - /** - * 将参数追加到url - * - * @param url 地址 - * @param params 参数 - */ - static String getQueryUrl(String url, Map params) { - if (CollectionTools.isNotEmpty(params)) { - String queryStr = StringTools.map2QueryStr(params); - return url.contains("?") ? url + "&" + queryStr : url + "?" + queryStr; - } - return url; - } - - /** - * key-value字符串转map - * - * @param keyValueStr key-value字符串 - */ - static Map keyValueStrToMap(String keyValueStr) { - Map params = Maps.newHashMap(); - if (StringTools.isBlank(keyValueStr)) return params; - String[] lines = keyValueStr.split("\n"); - for (String line : lines) { - if (StringTools.isBlank(line)) continue; - String[] kv = line.split(":"); - if (kv.length > 0) { - String key = kv[0].trim(); - String value = StringTools.EMPTY; - if (kv.length > 1) { - StringBuilder valueSb = new StringBuilder(); - for (int i = 1; i < kv.length; i++) { - valueSb.append(kv[i].trim()); - if (kv.length > 2 && i < kv.length - 1) - valueSb.append(":"); - } - value = valueSb.toString(); - } - params.put(key, value); - } - } - return params; - } -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/http/JsoupTools.java b/src/main/java/com/gitee/cliveyuan/tools/http/JsoupTools.java deleted file mode 100644 index 0eb6488..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/http/JsoupTools.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.gitee.cliveyuan.tools.http; - -import com.gitee.cliveyuan.tools.CollectionTools; -import com.gitee.cliveyuan.tools.cons.NetCons; -import com.gitee.cliveyuan.tools.exception.NetException; -import org.jsoup.Connection; -import org.jsoup.HttpStatusException; -import org.jsoup.Jsoup; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.ConnectException; -import java.net.SocketTimeoutException; -import java.net.UnknownHostException; - -/** - * jsoup 工具 - *

- * Created by Clive at 2018/07/24. - * Rebuilt by Clive at 2018/11/26. - * - * @since 2.0.0 - */ -public class JsoupTools extends AbstractHttpRequest { - - private static final Logger logger = LoggerFactory.getLogger(JsoupTools.class); - - private JsoupTools() { - timeout = NetCons.JSOUP_TIME_OUT; - } - - // region public methods - - /** - * 构建jsoup工具 - */ - public static JsoupTools build() { - return new JsoupTools(); - } - - @Override - public String get() throws NetException { - url = InnerTools.getQueryUrl(url, params); - return request(Connection.Method.GET).getContent(); - } - - @Override - public String post() throws NetException { - return request(Connection.Method.POST).getContent(); - } - - @Override - public HttpResp executeGet() throws NetException { - return request(Connection.Method.GET); - } - - @Override - public HttpResp executePost() throws NetException { - return request(Connection.Method.POST); - } - // endregion public methods - - - // region private methods - - private HttpResp request(Connection.Method method) throws NetException { - try { - Connection connection = getConnection() - .method(method); - if (CollectionTools.isNotEmpty(headers)) { - headers.forEach(connection::header); - } - if (CollectionTools.isNotEmpty(cookies)) { - connection.cookies(cookies); - } - if (CollectionTools.isNotEmpty(params)) { - connection.data(params); - } - if (ignoreSslCertificate) { - connection.validateTLSCertificates(false); - } - Connection.Response execute = connection.execute(); - return HttpResp.create(execute.body(), execute.cookies(), execute.headers()); - } catch (UnknownHostException e) { - throw NetException.unknownHost(); - } catch (HttpStatusException e) { - logger.info("http status error: statusCode={}, url={}", e.getStatusCode(), e.getUrl()); - // 如需要获取非200的内容请调HttpClientTools - throw NetException.httpStatusError(e.getStatusCode()); - } catch (SocketTimeoutException e) { - throw NetException.timeout(timeout); - } catch (ConnectException e) { - throw NetException.connectionRefused(url); - } catch (Exception e) { - logger.error("request error", e); - throw NetException.unKnowError(); - } - } - - private Connection getConnection() { - Connection connection = Jsoup.connect(url) - .ignoreContentType(true) - .timeout(timeout); - if (addDefaultHeaders) - NetCons.DEFAULT_HEADERS.forEach(connection::header); - return connection; - } - - // endregion private methods - - -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/img/ImageConvertTools.java b/src/main/java/com/gitee/cliveyuan/tools/img/ImageConvertTools.java deleted file mode 100644 index 3f0bcf9..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/img/ImageConvertTools.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.gitee.cliveyuan.tools.img; - -import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; - -import javax.imageio.ImageIO; - -import sun.misc.BASE64Decoder; -import sun.misc.BASE64Encoder; - -public class ImageConvertTools { - - private static BASE64Encoder encoder = new sun.misc.BASE64Encoder(); - private static BASE64Decoder decoder = new sun.misc.BASE64Decoder(); - - - /** - * 将其他格式的图片转换成CDR或其它文件格式 - * - * @param sourceFileName - * @param newFileName - */ - static void convert(String sourceFileName, String newFileName) { - try { - String base64String = getImageBinary(sourceFileName); - byte[] bytes1 = decoder.decodeBuffer(base64String); - ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes1); - BufferedImage bi1 = ImageIO.read(inputStream); - File w2 = new File(newFileName);//可以是jpg,png,gif格式 - ImageIO.write(bi1, getFileType(sourceFileName), w2);//不管输出什么格式图片,此处不需改动 - } catch (IOException e) { - e.printStackTrace(); - } - } - - private static String getImageBinary(String fileName) { - File f = new File(fileName); - BufferedImage bi; - try { - bi = ImageIO.read(f); - String fileType = getFileType(fileName); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ImageIO.write(bi, fileType, outputStream); - byte[] bytes = outputStream.toByteArray(); - - return encoder.encodeBuffer(bytes).trim(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - private static String getFileType(String fileName) { - return fileName.substring(fileName.indexOf(".") + 1, fileName.length()); - } - - -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/img/ImageTools.java b/src/main/java/com/gitee/cliveyuan/tools/img/ImageTools.java deleted file mode 100644 index 5af3699..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/img/ImageTools.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.gitee.cliveyuan.tools.img; - -import com.gitee.cliveyuan.tools.Assert; -import net.coobird.thumbnailator.Thumbnails; -import net.coobird.thumbnailator.geometry.Positions; - -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; - -/** - * 图片工具 - * - * doc - * - * @author clive - * Created on 2018/07/27 - * @see net.coobird.thumbnailator.Thumbnails - * @since 1.0 - */ -public class ImageTools { - - private ImageTools() { - } - - /** - * 压缩图片 - * - * @param srcImg 原始图片 - * @param scale 尺寸 - * @param quality 压缩质量 - * @param destImg 压缩后的图片 - * @throws IOException - */ - public static void compress(String srcImg, - float scale, - float quality, - String destImg - ) throws IOException { - Thumbnails.of(srcImg) - .scale(scale) - .outputQuality(quality) - .toFile(destImg); - } - - /** - * 裁剪 - * - * @param srcImg 原始图片 - * @param x - * @param y - * @param width - * @param height - * @param destImg 裁剪后的图片 - * @throws IOException - */ - public static void crop( - String srcImg, - int x, int y, int width, int height, - String destImg - ) throws IOException { - Thumbnails.of(srcImg) - .sourceRegion(x, y, width, height) - .size(width, height) - .toFile(destImg); - } - - /** - * 图片格式转换 - * - * @param srcImg 原图片 - * @param destImg 要转换的图片 (带格式) - */ - public static void convert(String srcImg, String destImg) { - Assert.notEmpty(srcImg, "src img is required"); - Assert.notEmpty(destImg, "dest img is required"); - ImageConvertTools.convert(srcImg, destImg); - } - - /** - * 获取图片真实格式 - * - * @param imgPath 图片路径 - */ - public static String getRealType(String imgPath) { - return ImgTypeTools.getImgType(imgPath); - } - - /** - * 添加水印图片 - * - * @param targetImg 目标图片: 要添加水印的图片 - * @param watermarkImg 水印图片 - * @param positions 位置 - * @param width 宽度 - * @param height 高度 - * @param opacity 透明度 - * @param destImg 生成的图片 - * @throws IOException - */ - public static void watermark(String targetImg, - String watermarkImg, - Positions positions, - int width, - int height, - float opacity, - String destImg - ) throws IOException { - BufferedImage watermarkImage = ImageIO.read(new File(watermarkImg)); - Thumbnails.of(targetImg) - .watermark(positions, watermarkImage, opacity) - .size(width, height) - .toFile(destImg); - } -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/img/ImgTypeTools.java b/src/main/java/com/gitee/cliveyuan/tools/img/ImgTypeTools.java deleted file mode 100644 index ae585b9..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/img/ImgTypeTools.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.gitee.cliveyuan.tools.img; - -import com.gitee.cliveyuan.tools.Assert; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; - -/** - * @author clive - * Created on 2018/07/27 - * @since 1.0 - */ -public class ImgTypeTools { - - private static final String TYPE_JPG = "jpg"; - private static final String TYPE_GIF = "gif"; - private static final String TYPE_PNG = "png"; - private static final String TYPE_BMP = "bmp"; - private static final String TYPE_UNKNOWN = "unknown"; - - - /** - * 获取图片真实格式 - * - * @param imgPath 图片路径 - */ - static String getImgType(String imgPath) { - try { - Assert.notEmpty(imgPath, "img file is required"); - File file = new File(imgPath); - Assert.isTrue(file.exists(), "img file not exists"); - return getImgType(new FileInputStream(file)); - } catch (FileNotFoundException e) { - throw new IllegalArgumentException("img file not found"); - } - } - - /** - * 根据文件流判断图片类型 - * - * @param fis - * @return jpg/png/gif/bmp - */ - private static String getImgType(FileInputStream fis) { - //读取文件的前几个字节来判断图片格式 - byte[] b = new byte[4]; - try { - fis.read(b, 0, b.length); - String type = bytesToHexString(b).toUpperCase(); - if (type.contains("FFD8FF")) { - return TYPE_JPG; - } else if (type.contains("89504E47")) { - return TYPE_PNG; - } else if (type.contains("47494638")) { - return TYPE_GIF; - } else if (type.contains("424D")) { - return TYPE_BMP; - } else { - return TYPE_UNKNOWN; - } - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (fis != null) { - try { - fis.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - return null; - } - - /** - * byte数组转换成16进制字符串 - * - * @param src - */ - private static String bytesToHexString(byte[] src) { - StringBuilder stringBuilder = new StringBuilder(); - if (src == null || src.length <= 0) { - return null; - } - for (int i = 0; i < src.length; i++) { - int v = src[i] & 0xFF; - String hv = Integer.toHexString(v); - if (hv.length() < 2) { - stringBuilder.append(0); - } - stringBuilder.append(hv); - } - return stringBuilder.toString(); - } - -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/sql/SqlTools.java b/src/main/java/com/gitee/cliveyuan/tools/sql/SqlTools.java deleted file mode 100644 index d84c4dc..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/sql/SqlTools.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.gitee.cliveyuan.tools.sql; - -import com.gitee.cliveyuan.tools.StringTools; - -/** - * @author Clive Yuan - * @date 2020/06/16 - */ -public class SqlTools { - - private SqlTools() { - } - - /** - * 仅支持字母、数字、下划线、空格、逗号(支持多个字段排序) - */ - public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,]+"; - - /** - * 检查字符,防止注入绕过 - */ - public static String escapeOrderBySql(String value) { - if (StringTools.isNotEmpty(value) && !isValidFieldName(value)) { - return StringTools.EMPTY; - } - return value; - } - - /** - * 验证字段语法是否符合规范 - */ - public static boolean isValidFieldName(String fieldName) { - return fieldName.matches(SQL_PATTERN); - } -} diff --git a/src/main/java/com/gitee/cliveyuan/tools/text/StrFormatter.java b/src/main/java/com/gitee/cliveyuan/tools/text/StrFormatter.java deleted file mode 100644 index f31e3f1..0000000 --- a/src/main/java/com/gitee/cliveyuan/tools/text/StrFormatter.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.gitee.cliveyuan.tools.text; - -import com.gitee.cliveyuan.tools.ArrayTools; -import com.gitee.cliveyuan.tools.StringTools; - -/** - * 字符串格式化 - * - * @author ruoyi - */ -public class StrFormatter { - - public static final String EMPTY_JSON = "{}"; - public static final char C_BACKSLASH = '\\'; - public static final char C_DELIM_START = '{'; - public static final char C_DELIM_END = '}'; - - /** - * 格式化字符串
- * 此方法只是简单将占位符 {} 按照顺序替换为参数
- * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可
- * 例:
- * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b
- * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a
- * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b
- * - * @param strPattern 字符串模板 - * @param argArray 参数列表 - * @return 结果 - */ - public static String format(final String strPattern, final Object... argArray) { - if (StringTools.isEmpty(strPattern) || ArrayTools.isEmpty(argArray)) { - return strPattern; - } - final int strPatternLength = strPattern.length(); - - // 初始化定义好的长度以获得更好的性能 - StringBuilder sbuf = new StringBuilder(strPatternLength + 50); - - int handledPosition = 0; - int delimIndex;// 占位符所在位置 - for (int argIndex = 0; argIndex < argArray.length; argIndex++) { - delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition); - if (delimIndex == -1) { - if (handledPosition == 0) { - return strPattern; - } else { // 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果 - sbuf.append(strPattern, handledPosition, strPatternLength); - return sbuf.toString(); - } - } else { - if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH) { - if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH) { - // 转义符之前还有一个转义符,占位符依旧有效 - sbuf.append(strPattern, handledPosition, delimIndex - 1); - sbuf.append(argArray[argIndex]); - handledPosition = delimIndex + 2; - } else { - // 占位符被转义 - argIndex--; - sbuf.append(strPattern, handledPosition, delimIndex - 1); - sbuf.append(C_DELIM_START); - handledPosition = delimIndex + 1; - } - } else { - // 正常占位符 - sbuf.append(strPattern, handledPosition, delimIndex); - sbuf.append(argArray[argIndex]); - handledPosition = delimIndex + 2; - } - } - } - // 加入最后一个占位符后所有的字符 - sbuf.append(strPattern, handledPosition, strPattern.length()); - - return sbuf.toString(); - } -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/EmailToolsTest.java b/src/test/java/com/gitee/cliveyuan/tools/test/EmailToolsTest.java deleted file mode 100644 index 1123743..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/EmailToolsTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.gitee.cliveyuan.tools.test; - -import com.gitee.cliveyuan.tools.mail.EmailTools; -import com.gitee.cliveyuan.tools.exception.EmailException; -import org.junit.Assert; -import org.junit.Test; - -/** - * @author clive - * Created on 2018/07/24 - * @since 1.0 - */ -public class EmailToolsTest { - -// @Test - public void send() throws EmailException { - EmailTools.build() - .host("smtp.exmail.qq.com") - .account("test@qq.com") - .password("*****") - .addRecipient("cliveyuan@foxmail.com") - .subject("Email Tools Test") - .content("Hello World!") - .send(); - } - -// @Test - public void sendBySSL() throws EmailException { - EmailTools.build() - .host("smtp.exmail.qq.com") - .account("test@qq.com") - .password("*****") - .addRecipient("cliveyuan@foxmail.com") - .subject("Email Tools Test") - .content("Hello World!") - .isSSL(true) - .send(); - } -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/HtmlToolsTest.java b/src/test/java/com/gitee/cliveyuan/tools/test/HtmlToolsTest.java deleted file mode 100644 index a3a9d86..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/HtmlToolsTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.gitee.cliveyuan.tools.test; - -import com.gitee.cliveyuan.tools.http.HtmlTools; -import org.junit.Test; - -/** - * @author clive - * Created on 2018/07/27 - * @since 1.0 - */ -public class HtmlToolsTest { - - @Test - public void test() { - String input = "\n" + - "\n" + - "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " Choose a website\n" + - "\n" + - " \n" + - " \n" + - "\n" + - " \n" + - " \n" + - " \n" + - "\n" + - "\n" + - "\n" + - "

\n" + - "
\n" + - "
\n" + - "
\n" + - " 招标采购导航网\n" + - "
\n" + - "
\n" + - " 中国石化采购平台\n" + - "
\n" + - "
\n" + - " 中国国际招标网\n" + - "
\n" + - "
\n" + - "
\n" + - "\n" + - "
\n" + - "\n" + - "\n" + - "\n" + - "\n" + - "\n" + - "\n" + - ""; - String output = HtmlTools.htmlEscape(input); -// System.out.println(output); - String parse = HtmlTools.htmlUnescape(output); - System.out.println(parse); - System.out.println(HtmlTools.delHTMLTag(parse)); - } -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/HttpClientToolsTest.java b/src/test/java/com/gitee/cliveyuan/tools/test/HttpClientToolsTest.java deleted file mode 100644 index 1b3972b..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/HttpClientToolsTest.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.gitee.cliveyuan.tools.test; - -import com.gitee.cliveyuan.tools.StringTools; -import com.gitee.cliveyuan.tools.exception.NetException; -import com.gitee.cliveyuan.tools.http.HttpClientTools; -import com.gitee.cliveyuan.tools.http.HttpResp; -import org.junit.Test; - -import java.util.Map; - -/** - * @author clive - * Created on 2018/07/24 - * @since 1.0 - */ -public class HttpClientToolsTest { - - //@Test - public void get() throws NetException { - String url = "http://h5.yit.com/login.html"; - int timeout = 5000; - String response = HttpClientTools.build().url(url).timeout(timeout).get(); - System.out.println(response); - } - - //@Test - public void post() throws NetException { - String url = "https://api.yit.com/apigw/m.api"; - String paramStr = "_aid: 1\n" + - "_ts: 1532419945579\n" + - "_sm: md5\n" + - "_mt: user.downSmsCode\n" + - "mobile: 13800138000\n" + - "askType: LOGORREGI\n" + - "_sig: 741a1b1dc6a4339a149e428a5da6e077"; - Map params = StringTools.str2Map(paramStr); - int timeout = 5000; - HttpResp httpResp = HttpClientTools - .build() - .url(url) - .params(params) - .timeout(timeout) - .executePost(); - System.out.println(httpResp.getContent()); - } - - //@Test - public void testGet() throws NetException { - String resp = HttpClientTools.build() - .url("http://localhost:8080/api-test-sys/mock_api/get") - .addParam("user", "clive") - .addHeader("_token", "abc:::") - .get(); - System.out.println(resp); - } - - //@Test - public void testPost() throws NetException { - String resp = HttpClientTools.build() - .url("http://localhost:8080/api-test-sys/mock_api/post") - .addParam("user", "clive") - .addHeader("_token", "abc:::") - .post(); - System.out.println(resp); - } - - // @Test - //以json字符串作为入参, springMVC以@RequestBody来接收 - public void payload() throws NetException { - String resp = HttpClientTools.build() - .isPayload(true) - .payload("{\"xx\":abc}") - .encoding("utf-8") - .url("http://localhost:8080/api-test-sys/mock_api/payload") - .post(); - System.out.println(resp); - } - - // @Test - public void test() { - String resp = HttpClientTools.build().url("http://miinac.miit.gov.cn/components/Notice.action?doType=view&id=1014420650053787972") - .get(); - System.out.println("resp: " + resp); - } - -// @Test - public void test_status() { - String url = "http://h5.yit.com/login2.html"; - HttpResp httpResp = HttpClientTools.build() - .url(url) - .addParam("from", "baidu") - .executeGet(); - - System.out.println(httpResp); - } - - // @Test - public void test_ignore_ssl_cer() { - HttpResp httpResp = HttpClientTools.build() - .url("https://frigate.wuxingdev.cn/dbms-front/#/?mysqlInstanceGroupId=61&instanceId=736") - .ignoreSslCertificate() - .executeGet(); - System.out.println(httpResp); - } - -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/ImageToolsTest.java b/src/test/java/com/gitee/cliveyuan/tools/test/ImageToolsTest.java deleted file mode 100644 index 810a8e6..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/ImageToolsTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.gitee.cliveyuan.tools.test; - -import com.gitee.cliveyuan.tools.img.ImageTools; -import net.coobird.thumbnailator.geometry.Positions; -import org.junit.Test; - -import java.io.IOException; - -/** - * @author clive - * Created on 2018/07/30 - * @since 1.0 - */ -public class ImageToolsTest { - private static final String ROOT = ImageToolsTest.class.getResource("/").getPath(); - private static final String SRC_IMG = ROOT + "git.jpg"; - - @Test - public void compress() throws IOException { - String destImg = ROOT + "git_compress.jpg"; - System.out.println(destImg); - ImageTools.compress(SRC_IMG, 1.0f, 0.5f, destImg); - } - - @Test - public void crop() throws IOException { - String destImg = ROOT + "git_crop.jpg"; - System.out.println(destImg); - ImageTools.crop(SRC_IMG, 198, 462, 244, 218, destImg); - } - - @Test - public void watermark() throws IOException { - String destImg = ROOT + "git_watermark.jpg"; - String watermark = ROOT + "watermark.png"; - System.out.println(destImg); - ImageTools.watermark(SRC_IMG, watermark, - Positions.BOTTOM_LEFT, - 200, 200, - 0.5f, destImg); - } -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/JsoupToolsTest.java b/src/test/java/com/gitee/cliveyuan/tools/test/JsoupToolsTest.java deleted file mode 100644 index 30b0403..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/JsoupToolsTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.gitee.cliveyuan.tools.test; - -import com.alibaba.fastjson.JSONObject; -import com.gitee.cliveyuan.tools.exception.NetException; -import com.gitee.cliveyuan.tools.http.HttpClientTools; -import com.gitee.cliveyuan.tools.http.HttpResp; -import com.gitee.cliveyuan.tools.http.JsoupTools; -import org.jsoup.nodes.Document; -import org.junit.Test; - -/** - * @author clive - * Created on 2018/07/24 - * @since 1.0 - */ -public class JsoupToolsTest { - - - // @Test - public void get() throws NetException { - String url = "http://h5.yit.com/login.html"; - int timeout = 5000; - String response = JsoupTools.build() - .url(url) - .addParam("from", "baidu") - .addHeader("origin", "https://www.yit.com") - .addCookie("userToken", "123456") - .timeout(timeout) - .get(); - System.out.println(response); - } - - //@Test - public void post() throws NetException { - String url = "https://api.yit.com/apigw/m.api"; - - String paramStr = "_aid: 1\n" + - "_ts: 1532419945579\n" + - "_sm: md5\n" + - "_mt: user.downSmsCode\n" + - "mobile: 13800138000\n" + - "askType: LOGORREGI\n" + - "_sig: 741a1b1dc6a4339:a149e428a5da6e0:77"; - int timeout = 5000; - HttpResp httpResp = JsoupTools.build() - .url(url) - .params(paramStr) - .timeout(timeout) - .executePost(); - System.out.println(httpResp.getContent()); - } - - //@Test - public void getByDocument() throws NetException { - String url = "http://h5.yit.com/login.html"; - int timeout = 5000; - Document document = JsoupTools.build() - .url(url) - .addParam("from", "baidu") - .addHeader("origin", "https://www.yit.com") - .addCookie("userToken", "123456") - .timeout(timeout) - .getByDocument(); - System.out.println(document.title()); - } - - // @Test - public void test_get_json() { - String url = "http://www.kuaidi100.com/query?type="; - JSONObject json = JsoupTools.build() - .url(url) - .addParam("from", "baidu") - .getByJson(); - - System.out.println(json); - } - - // @Test - public void test_ignore_ssl_cer() { - HttpResp httpResp = JsoupTools.build() - .url("https://frigate.wuxingdev.cn/dbms-front/#/?mysqlInstanceGroupId=61&instanceId=736") - .ignoreSslCertificate() - .executeGet(); - System.out.println(httpResp); - } - -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/MathToolsTest.java b/src/test/java/com/gitee/cliveyuan/tools/test/MathToolsTest.java deleted file mode 100644 index 6abbb0d..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/MathToolsTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.gitee.cliveyuan.tools.test; - -import com.gitee.cliveyuan.tools.MathTools; -import org.junit.Assert; -import org.junit.Test; - -/** - * @author clive - * Created on 2018/07/24 - * @since 1.0 - */ -public class MathToolsTest { - - @Test - public void test() { - int min = 1; - int max = 10; - for (int i = 0; i < 100; i++) { - int random = MathTools.randomInt(min, max); - System.out.printf("%d,", random); - Assert.assertTrue(random >= min && random <= max); - } - } -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/Test2.java b/src/test/java/com/gitee/cliveyuan/tools/test/Test2.java deleted file mode 100644 index 1598d1c..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/Test2.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.gitee.cliveyuan.tools.test; - -import com.gitee.cliveyuan.tools.StringTools; -import com.gitee.cliveyuan.tools.ValidateTools; -import com.google.common.collect.Lists; - -import java.io.File; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -/** - * Created by Clive on 2020/01/05. - */ -public class Test2 { - - public static void main(String[] args) { - String folderPath = "/Users/cliveyuan/data/clive_data/travel/template/Phuket03/(Footage)/02_photos or videos footage"; - File folder = new File(folderPath); - File[] files = folder.listFiles(); - List names = names(files); - System.out.println(names); - for (int i = 0; i < files.length; i++) { - File file = files[i]; - String newFileName = folderPath + "/" + getNum(i) + ".jpg"; - File dest = new File(newFileName); - if (dest.exists()) { - System.out.printf("file exist: %s %n", dest.getName()); - continue; - } - if (!needRename(file)) { - System.out.printf("file name right: %s %n", file.getName()); - continue; - } - file.renameTo(dest); - } - } - - private static String getNum(int i) { - if (i < 9) { - return "0" + (i + 1); - } - return Integer.toString(i + 1); - } - - private static List names(File[] files) { - return Arrays.stream(files) - .map(Test2::getFileName) - .filter(ValidateTools::isNumeric) - .map(Integer::valueOf) - .sorted() - .collect(Collectors.toList()); - } - - private static List nums() { - List nums = Lists.newArrayList(); - for (int i = 1; i <= 21; i++) { - nums.add(i); - } - return nums; - } - - private static String getFileName(File file) { - return file.getName().replace(".jpg", ""); - } - - private static boolean needRename(File file) { - String name = file.getName(); - return !(StringTools.isNumeric(name) && nums().contains(Integer.valueOf(getFileName(file)))); - } - -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/TestCons.java b/src/test/java/com/gitee/cliveyuan/tools/test/TestCons.java deleted file mode 100644 index f0d4273..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/TestCons.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.gitee.cliveyuan.tools.test; - -/** - * @author: clive - * Created on 2018/07/23 - * @since: 1.0 - */ -public class TestCons { - -// public static final String TEST_PATH = "/data/test"; -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/XmlyTest.java b/src/test/java/com/gitee/cliveyuan/tools/test/XmlyTest.java deleted file mode 100644 index dd1231b..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/XmlyTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.gitee.cliveyuan.tools.test; - -import com.gitee.cliveyuan.tools.StringTools; -import com.gitee.cliveyuan.tools.http.HttpClientTools; -import com.gitee.cliveyuan.tools.http.JsoupTools; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.select.Elements; -import org.junit.Test; - -import java.util.List; -import java.util.stream.Collectors; - -/** - * Created by Clive on 2020/02/04. - */ -public class XmlyTest { - - @Test - public void test_js() { - String keyword = "buyKey"; - String url = "https://www.ximalaya.com/xiangsheng/32445958/"; - Document document = JsoupTools.build().url(url).addDefaultHeaders().getByDocument(); - Elements scripts = document.select("script"); - List urls = scripts.stream().map(x -> x.attr("src")).filter(StringTools::isNotBlank).collect(Collectors.toList()); - this.findKeyword(keyword, urls); - } - - private void findKeyword(String keyword, List urls) { - urls.forEach(url->{ - if (StringTools.isBlank(url)) { - return; - } - System.out.println("scriptUrl: " + url); - if (url.startsWith("//")) { - url = "https:" + url; - } - String content = JsoupTools.build().url(url).get(); - if (content.contains(keyword)) { - System.out.println("==== matched scriptUrl: " + url); - } - }); - } -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/American.java b/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/American.java deleted file mode 100644 index bd21330..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/American.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.gitee.cliveyuan.tools.test.fastjson; - -/** - * @author Clive Yuan - * @date 2020/06/11 - */ -public class American extends Person { - @Override - public String toString() { - return "American{} " + super.toString(); - } -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/Chinese.java b/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/Chinese.java deleted file mode 100644 index b7d311f..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/Chinese.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.gitee.cliveyuan.tools.test.fastjson; - -/** - * @author Clive Yuan - * @date 2020/06/11 - */ -public class Chinese extends Person { - - @Override - public String toString() { - return "Chinese{} " + super.toString(); - } -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/FastjsonTest.java b/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/FastjsonTest.java deleted file mode 100644 index e037c17..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/FastjsonTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.gitee.cliveyuan.tools.test.fastjson; - -import com.alibaba.fastjson.JSON; -import org.junit.Test; - -/** - * @author Clive Yuan - * @date 2020/06/11 - */ -public class FastjsonTest { - @Test - public void test_person() { - String jsonString = "{\n" + - "\t\"id\": 1,\n" + - "\t\"name\": \"Clive\"\n" + - "}"; - Person person = JSON.parseObject(jsonString, Person.class); - System.out.println(person); - } - - @Test - public void test_chinese() { - String jsonString = "{\n" + - "\t\"@type\": \"com.gitee.cliveyuan.tools.test.fastjson.Chinese\",\n" + - "\t\"id\": 1,\n" + - "\t\"name\": \"Clive\"\n" + - "}"; - Person person = JSON.parseObject(jsonString, Person.class); - System.out.println(person); - } - - @Test - public void test_american() { - String jsonString = "{\n" + - "\t\"@type\": \"com.gitee.cliveyuan.tools.test.fastjson.American\",\n" + - "\t\"id\": 1,\n" + - "\t\"name\": \"American\"\n" + - "}"; - Person person = JSON.parseObject(jsonString, Person.class); - System.out.println(person); - } -} diff --git a/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/Person.java b/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/Person.java deleted file mode 100644 index c321e57..0000000 --- a/src/test/java/com/gitee/cliveyuan/tools/test/fastjson/Person.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.gitee.cliveyuan.tools.test.fastjson; - -import lombok.Data; - -/** - * @author Clive Yuan - * @date 2020/06/11 - */ -@Data -public class Person { - private Long id; - private String name; - - @Override - public String toString() { - return "Person{" + - "id=" + id + - ", name='" + name + '\'' + - '}'; - } -} diff --git a/src/test/resources/git.jpg b/src/test/resources/git.jpg deleted file mode 100644 index 6e9bcf445df027964807a7214d8bcf11020109a6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17106 zcmdtJXIN9q7C*cZdqbs*fJZ<&h|&@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h^tl5?B21gsalx1+U z3x}q!7$rc(jg(TDh%NTyKgs0gD5wSzL4d+U*#hFGHIgyOf(D8GY#7X<6S3W#y|ltq zKQe=3OJk|8^3?01_u*H&PrmKy{SCXD9kYfS*ase;xP0!a;UwZi2!Xk{zvy2P{bc~G<6Uf%UV50pe`UIr}7Klo| zIkhX;IPnbJ%_#m^UtmvFwDGez|`-(8FF^se?36*-wmMO@A-cK+SS}z diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties deleted file mode 100755 index 394fbd0..0000000 --- a/src/test/resources/log4j.properties +++ /dev/null @@ -1,12 +0,0 @@ -# set log levels -log4j.rootLogger = debug, stdout - -# output to console -log4j.appender.stdout = org.apache.log4j.ConsoleAppender -log4j.appender.stdout.Target = System.out -log4j.appender.stdout.layout = org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern =[%-d{yyyy-MM-dd HH:mm:ss}]-%C{1}(%L)(%r) | %m%n - -# set output style -log4j.appender.log.layout=org.apache.log4j.PatternLayout -log4j.appender.log.layout.ConversionPattern = [%-d{yyyy-MM-dd HH:mm:ss}]-%C{1}(%L)(%r) | %m%n diff --git a/src/test/resources/read_test.txt b/src/test/resources/read_test.txt deleted file mode 100644 index faf50f7..0000000 --- a/src/test/resources/read_test.txt +++ /dev/null @@ -1,10 +0,0 @@ -中文 -汉语 -hello -world -by clive - - -... - -the end \ No newline at end of file diff --git a/src/test/resources/watermark.png b/src/test/resources/watermark.png deleted file mode 100644 index 81ca1441a73930b388be4d5f7bbf511d5013de73..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 11169 zcmZ{J1ymeewRPE*}R6Q7@* zc1KdZ?03?6P`+AVYtr^hRU`G`?|wGz{GPZOqnhw57zyp}>+2vC3Jyg^jIhwqmrAIs z&p+K}k+a+l9o25{>YrP~_zh_`k-v6{D{KzB1EX$GzIvU}Pv{|`FDuz|iD>?y3B|nPK_TA!dpIxR+YSGCcZ>M@PiJ}3?_uzN5iS)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o{5@M7(Tw+kI> zy9qzoH3=fQ(s*b}*&Rs>;fj6p`SMWyIn3GLj}VXA&3!KGt6x2#m~tK}O^ zaVtxU4`8aZM(F}9!nf^H5vZ=0fUm29{)J;;f$Wsjx7%`0opi=<61?_uVz z8!oSFyN4$EVxMc!UKd$JR_84*NqwwuUKe7R^zUL-9A)XAwRFzK$ae4Uu#b{q*1Tk= zN=BA;NP7q72)GL?S7|Hp=L8p>y-HjfAU|TnPexvKDfBMp>&mIYz-CBU>6}U+&%oG7 j3XS}+>3@1lcIWpl0>o3gpgHr;|8QAJg-<0PjlTUaf* + + + java-tools + com.gitee.opensource4clive + 4.0.0 + + 4.0.0 + + java-tools-web + + + + com.gitee.opensource4clive + java-tools-common + + + javax.servlet + javax.servlet-api + provided + + + org.springframework + spring-web + provided + + + + diff --git a/src/main/java/com/gitee/cliveyuan/tools/web/CaptchaTools.java b/web/src/main/java/cn/cliveyuan/tools/web/CaptchaTools.java similarity index 82% rename from src/main/java/com/gitee/cliveyuan/tools/web/CaptchaTools.java rename to web/src/main/java/cn/cliveyuan/tools/web/CaptchaTools.java index 1481e6b..ec5504f 100755 --- a/src/main/java/com/gitee/cliveyuan/tools/web/CaptchaTools.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/CaptchaTools.java @@ -1,18 +1,17 @@ -package com.gitee.cliveyuan.tools.web; - +package cn.cliveyuan.tools.web; + +import cn.cliveyuan.tools.common.AssertTools; +import cn.cliveyuan.tools.common.IdTools; +import cn.cliveyuan.tools.common.StringTools; +import cn.cliveyuan.tools.web.bean.CaptchaCodeResult; +import cn.cliveyuan.tools.web.bean.CaptchaGenerateType; +import cn.cliveyuan.tools.web.bean.CaptchaRequest; +import cn.cliveyuan.tools.web.bean.CaptchaType; +import cn.cliveyuan.tools.web.bean.Captcha; +import cn.cliveyuan.tools.web.bean.CaptchaValidate; +import cn.cliveyuan.tools.web.exception.CaptchaException; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.gitee.cliveyuan.tools.Assert; -import com.gitee.cliveyuan.tools.IdTools; -import com.gitee.cliveyuan.tools.StringTools; -import com.gitee.cliveyuan.tools.VerifyCodeTools; -import com.gitee.cliveyuan.tools.bean.captcha.CaptchaCodeResult; -import com.gitee.cliveyuan.tools.bean.captcha.CaptchaRequest; -import com.gitee.cliveyuan.tools.bean.captcha.CaptchaV2; -import com.gitee.cliveyuan.tools.bean.CaptchaValidate; -import com.gitee.cliveyuan.tools.enums.CaptchaGenerateType; -import com.gitee.cliveyuan.tools.enums.CaptchaType; -import com.gitee.cliveyuan.tools.exception.CaptchaException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cache.Cache; @@ -53,21 +52,21 @@ public class CaptchaTools { logger.debug("CaptchaTools.validate: captchaValidate={}", captchaValidate); try { - Assert.notNull(captchaValidate, "captchaValidate is null"); + AssertTools.notNull(captchaValidate, "captchaValidate is null"); HttpServletRequest request = captchaValidate.getRequest(); CacheManager cacheManager = captchaValidate.getCacheManager(); String cacheNamespace = captchaValidate.getCacheNamespace(); - Assert.notNull(captchaValidate.getGenerateType(), "generateType is null"); + AssertTools.notNull(captchaValidate.getGenerateType(), "generateType is null"); if (CaptchaGenerateType.SESSION.equals(captchaValidate.getGenerateType())) { - Assert.notNull(request, "request is null"); + AssertTools.notNull(request, "request is null"); } else if (CaptchaGenerateType.CACHE.equals(captchaValidate.getGenerateType())) { - Assert.notNull(cacheManager, "cacheManager is null"); - Assert.notNull(cacheNamespace, "cacheNamespace is null"); + AssertTools.notNull(cacheManager, "cacheManager is null"); + AssertTools.notNull(cacheNamespace, "cacheNamespace is null"); } - CaptchaV2 inputCaptcha = captchaValidate.getCaptcha(); + Captcha inputCaptcha = captchaValidate.getCaptcha(); String captchaName = captchaValidate.getName(); if (Objects.isNull(inputCaptcha)) { throw CaptchaException.captchaIsNull(); @@ -84,7 +83,7 @@ public class CaptchaTools { inputCaptcha.setValue(inputCaptcha.getValue().toLowerCase()); - CaptchaV2 captcha = null; + Captcha captcha = null; if (CaptchaGenerateType.SESSION.equals(captchaValidate.getGenerateType())) { captcha = getFromSession(captchaValidate, captchaName); } else if (CaptchaGenerateType.CACHE.equals(captchaValidate.getGenerateType())) { @@ -123,20 +122,20 @@ public class CaptchaTools { public static void generate(CaptchaRequest captchaRequest) { logger.debug("CaptchaTools.generate captchaRequest={}", captchaRequest); - Assert.notNull(captchaRequest, "captchaRequest is null"); + AssertTools.notNull(captchaRequest, "captchaRequest is null"); HttpServletResponse response = captchaRequest.getResponse(); HttpServletRequest request = captchaRequest.getRequest(); CacheManager cacheManager = captchaRequest.getCacheManager(); String cacheNamespace = captchaRequest.getCacheNamespace(); - Assert.notNull(response, "response is null"); - Assert.notNull(captchaRequest.getGenerateType(), "generateType is null"); + AssertTools.notNull(response, "response is null"); + AssertTools.notNull(captchaRequest.getGenerateType(), "generateType is null"); if (CaptchaGenerateType.SESSION.equals(captchaRequest.getGenerateType())) { - Assert.notNull(request, "request is null"); + AssertTools.notNull(request, "request is null"); } else if (CaptchaGenerateType.CACHE.equals(captchaRequest.getGenerateType())) { - Assert.notNull(cacheManager, "cacheManager is null"); - Assert.notNull(cacheNamespace, "cacheNamespace is null"); + AssertTools.notNull(cacheManager, "cacheManager is null"); + AssertTools.notNull(cacheNamespace, "cacheNamespace is null"); } int length = captchaRequest.getLength(); int width = captchaRequest.getWidth(); @@ -172,7 +171,7 @@ public class CaptchaTools { if (StringTools.isBlank(id)) { id = IdTools.randomShortUUID(); } - CaptchaV2 captcha = new CaptchaV2(id, captchaCodeResult.getVerifyContent()); + Captcha captcha = new Captcha(id, captchaCodeResult.getVerifyContent()); if (CaptchaGenerateType.SESSION.equals(captchaRequest.getGenerateType())) { saveToSession(captchaRequest, captcha); } else if (CaptchaGenerateType.CACHE.equals(captchaRequest.getGenerateType())) { @@ -191,7 +190,7 @@ public class CaptchaTools { return String.format("%s_%s", captchaName, id); } - private static void saveToSession(CaptchaRequest captchaRequest, CaptchaV2 captcha) { + private static void saveToSession(CaptchaRequest captchaRequest, Captcha captcha) { HttpSession session = captchaRequest.getRequest().getSession(true); String name = captchaRequest.getName(); if (Objects.isNull(name)) { @@ -200,7 +199,7 @@ public class CaptchaTools { session.setAttribute(getKey(name, captcha.getId()), JSON.toJSONString(captcha)); } - private static void saveToCache(CaptchaRequest captchaRequest, CaptchaV2 captcha) { + private static void saveToCache(CaptchaRequest captchaRequest, Captcha captcha) { CacheManager cacheManager = captchaRequest.getCacheManager(); Cache cache = cacheManager.getCache(captchaRequest.getCacheNamespace()); String name = captchaRequest.getName(); @@ -210,21 +209,21 @@ public class CaptchaTools { cache.put(getKey(name, captcha.getId()), captcha); } - private static CaptchaV2 getFromSession(CaptchaValidate captchaValidate, String captchaName) { + private static Captcha getFromSession(CaptchaValidate captchaValidate, String captchaName) { HttpSession session = captchaValidate.getRequest().getSession(true); String captchaStr = (String) session.getAttribute(getKey(captchaName, captchaValidate.getCaptcha().getId())); if (StringTools.isBlank(captchaStr)) { throw CaptchaException.notInSession(); } - return JSONObject.parseObject(captchaStr, CaptchaV2.class); + return JSONObject.parseObject(captchaStr, Captcha.class); } - private static CaptchaV2 getFromCache(CaptchaValidate captchaValidate, String captchaName) { + private static Captcha getFromCache(CaptchaValidate captchaValidate, String captchaName) { CacheManager cacheManager = captchaValidate.getCacheManager(); Cache cache = cacheManager.getCache(captchaValidate.getCacheNamespace()); Cache.ValueWrapper valueWrapper = cache.get(getKey(captchaName, captchaValidate.getCaptcha().getId())); if (Objects.nonNull(valueWrapper)) { - return (CaptchaV2) valueWrapper.get(); + return (Captcha) valueWrapper.get(); } return null; } diff --git a/src/main/java/com/gitee/cliveyuan/tools/web/FileUploadTools.java b/web/src/main/java/cn/cliveyuan/tools/web/FileUploadTools.java similarity index 82% rename from src/main/java/com/gitee/cliveyuan/tools/web/FileUploadTools.java rename to web/src/main/java/cn/cliveyuan/tools/web/FileUploadTools.java index 3ee7007..2457e8b 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/web/FileUploadTools.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/FileUploadTools.java @@ -1,12 +1,12 @@ -package com.gitee.cliveyuan.tools.web; +package cn.cliveyuan.tools.web; -import com.gitee.cliveyuan.tools.Assert; -import com.gitee.cliveyuan.tools.FileTools; -import com.gitee.cliveyuan.tools.MathTools; -import com.gitee.cliveyuan.tools.StringTools; -import com.gitee.cliveyuan.tools.bean.FileUploadRequest; -import com.gitee.cliveyuan.tools.bean.FileUploadResponse; -import com.gitee.cliveyuan.tools.exception.FileUploadException; +import cn.cliveyuan.tools.common.AssertTools; +import cn.cliveyuan.tools.common.FileTools; +import cn.cliveyuan.tools.common.StringTools; +import cn.cliveyuan.tools.web.bean.FileUploadRequest; +import cn.cliveyuan.tools.web.bean.FileUploadResponse; +import cn.cliveyuan.tools.web.exception.FileUploadException; +import org.apache.commons.lang3.RandomUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.multipart.MultipartFile; @@ -36,7 +36,7 @@ public class FileUploadTools { * @throws FileUploadException */ public static FileUploadResponse upload(FileUploadRequest fileUploadRequest) throws FileUploadException { - Assert.notNull(fileUploadRequest, "fileUploadRequest is required"); + AssertTools.notNull(fileUploadRequest, "fileUploadRequest is required"); MultipartFile multipartFile = fileUploadRequest.getMultipartFile(); HttpServletRequest request = fileUploadRequest.getRequest(); String relativeSavePath = fileUploadRequest.getRelativeSavePath(); @@ -44,15 +44,15 @@ public class FileUploadTools { String fileName = fileUploadRequest.getFileName(); String allowedExtensions = fileUploadRequest.getAllowedExtensions(); - Assert.notNull(multipartFile, "multipartFile is required"); - Assert.notNull(request, "request is required"); - Assert.isTrue(StringTools.isNotBlank(relativeSavePath) + AssertTools.notNull(multipartFile, "multipartFile is required"); + AssertTools.notNull(request, "request is required"); + AssertTools.isTrue(StringTools.isNotBlank(relativeSavePath) || StringTools.isNotBlank(absoluteSavePath), "relativeSavePath and absoluteSavePath can't be both blank"); try { if (multipartFile.getSize() > 0) { String extension = FileTools.getExtension(multipartFile.getOriginalFilename()); - Assert.notEmpty(extension, "can't get the extension"); + AssertTools.notEmpty(extension, "can't get the extension"); if (StringTools.isNotEmpty(allowedExtensions)) { List allowedExtensionList = Arrays.asList(allowedExtensions.toLowerCase().split(",")); if (!allowedExtensionList.contains(extension.toLowerCase())) { @@ -68,7 +68,7 @@ public class FileUploadTools { if (StringTools.isBlank(path)) throw FileUploadException.uploadPathNotExist(); if (StringTools.isBlank(fileName)) { - fileName = Long.toString(System.currentTimeMillis()) + MathTools.randomInt(1000000, 9999999); + fileName = Long.toString(System.currentTimeMillis()) + RandomUtils.nextInt(1000000, 9999999); } String fullFileName = fileName + "." + extension; File targetDirectory = new File(path); diff --git a/src/main/java/com/gitee/cliveyuan/tools/web/IpTools.java b/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java similarity index 96% rename from src/main/java/com/gitee/cliveyuan/tools/web/IpTools.java rename to web/src/main/java/cn/cliveyuan/tools/web/IpTools.java index a273b92..e0fc2af 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/web/IpTools.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java @@ -1,10 +1,9 @@ -package com.gitee.cliveyuan.tools.web; +package cn.cliveyuan.tools.web; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; -import java.net.InetAddress; /** * ip 工具 diff --git a/src/main/java/com/gitee/cliveyuan/tools/VerifyCodeTools.java b/web/src/main/java/cn/cliveyuan/tools/web/VerifyCodeTools.java similarity index 97% rename from src/main/java/com/gitee/cliveyuan/tools/VerifyCodeTools.java rename to web/src/main/java/cn/cliveyuan/tools/web/VerifyCodeTools.java index 3768e3f..053dd6a 100755 --- a/src/main/java/com/gitee/cliveyuan/tools/VerifyCodeTools.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/VerifyCodeTools.java @@ -1,13 +1,10 @@ -package com.gitee.cliveyuan.tools; +package cn.cliveyuan.tools.web; -import com.gitee.cliveyuan.tools.bean.captcha.CaptchaCodeResult; +import cn.cliveyuan.tools.web.bean.CaptchaCodeResult; import org.apache.commons.lang3.RandomUtils; -import java.awt.Color; -import java.awt.Font; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.RenderingHints; +import javax.imageio.ImageIO; +import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; @@ -17,8 +14,6 @@ import java.io.OutputStream; import java.util.Arrays; import java.util.Random; -import javax.imageio.ImageIO; - /** * 验证码工具 * diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/captcha/CaptchaV2.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/Captcha.java similarity index 78% rename from src/main/java/com/gitee/cliveyuan/tools/bean/captcha/CaptchaV2.java rename to web/src/main/java/cn/cliveyuan/tools/web/bean/Captcha.java index c589668..17c4d85 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/captcha/CaptchaV2.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/Captcha.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean.captcha; +package cn.cliveyuan.tools.web.bean; import lombok.AllArgsConstructor; import lombok.Data; @@ -14,7 +14,7 @@ import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor -public class CaptchaV2 { +public class Captcha { private String id; private String value; diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/captcha/CaptchaCodeResult.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaCodeResult.java similarity index 84% rename from src/main/java/com/gitee/cliveyuan/tools/bean/captcha/CaptchaCodeResult.java rename to web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaCodeResult.java index 9c6f148..268d28b 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/captcha/CaptchaCodeResult.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaCodeResult.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean.captcha; +package cn.cliveyuan.tools.web.bean; import lombok.Builder; import lombok.Data; diff --git a/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaGenerateType.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaGenerateType.java new file mode 100644 index 0000000..cfa388c --- /dev/null +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaGenerateType.java @@ -0,0 +1,18 @@ +package cn.cliveyuan.tools.web.bean; + +/** + * Created by Clive on 2019/06/16. + */ +public enum CaptchaGenerateType { + + /** + * 回话 + */ + SESSION, + + /** + * 缓存 + */ + CACHE + +} diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/captcha/CaptchaRequest.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaRequest.java similarity index 91% rename from src/main/java/com/gitee/cliveyuan/tools/bean/captcha/CaptchaRequest.java rename to web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaRequest.java index 9ac6db6..29e3bd4 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/captcha/CaptchaRequest.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaRequest.java @@ -1,7 +1,5 @@ -package com.gitee.cliveyuan.tools.bean.captcha; +package cn.cliveyuan.tools.web.bean; -import com.gitee.cliveyuan.tools.enums.CaptchaGenerateType; -import com.gitee.cliveyuan.tools.enums.CaptchaType; import lombok.Builder; import lombok.Data; import org.springframework.cache.CacheManager; diff --git a/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaType.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaType.java new file mode 100644 index 0000000..3885939 --- /dev/null +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaType.java @@ -0,0 +1,20 @@ +package cn.cliveyuan.tools.web.bean; + +/** + * 验证码类型 + * + * Created by Clive on 2020/02/17. + */ +public enum CaptchaType { + + /** + * 字母数字 + */ + LETTER_NUM, + + /** + * 数学加法 + */ + MATH_ADD + +} diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/CaptchaValidate.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaValidate.java similarity index 83% rename from src/main/java/com/gitee/cliveyuan/tools/bean/CaptchaValidate.java rename to web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaValidate.java index 26dd9a1..a6ea703 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/CaptchaValidate.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaValidate.java @@ -1,7 +1,5 @@ -package com.gitee.cliveyuan.tools.bean; +package cn.cliveyuan.tools.web.bean; -import com.gitee.cliveyuan.tools.bean.captcha.CaptchaV2; -import com.gitee.cliveyuan.tools.enums.CaptchaGenerateType; import lombok.Builder; import lombok.Data; import org.springframework.cache.CacheManager; @@ -43,7 +41,7 @@ public class CaptchaValidate implements Serializable { /** * 输入的验证码 (必须) */ - private CaptchaV2 captcha; + private Captcha captcha; /** * 当验证失败时抛CaptchaException异常 diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/FileUploadRequest.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/FileUploadRequest.java similarity index 95% rename from src/main/java/com/gitee/cliveyuan/tools/bean/FileUploadRequest.java rename to web/src/main/java/cn/cliveyuan/tools/web/bean/FileUploadRequest.java index 5c21c25..fe8c54d 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/FileUploadRequest.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/FileUploadRequest.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean; +package cn.cliveyuan.tools.web.bean; import lombok.Builder; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/bean/FileUploadResponse.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/FileUploadResponse.java similarity index 93% rename from src/main/java/com/gitee/cliveyuan/tools/bean/FileUploadResponse.java rename to web/src/main/java/cn/cliveyuan/tools/web/bean/FileUploadResponse.java index a6a7c1e..0125e72 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/bean/FileUploadResponse.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/FileUploadResponse.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.bean; +package cn.cliveyuan.tools.web.bean; import lombok.Builder; import lombok.Data; diff --git a/src/main/java/com/gitee/cliveyuan/tools/exception/CaptchaException.java b/web/src/main/java/cn/cliveyuan/tools/web/exception/CaptchaException.java similarity index 96% rename from src/main/java/com/gitee/cliveyuan/tools/exception/CaptchaException.java rename to web/src/main/java/cn/cliveyuan/tools/web/exception/CaptchaException.java index 5ddbf4b..f606c3b 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/exception/CaptchaException.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/exception/CaptchaException.java @@ -1,4 +1,4 @@ -package com.gitee.cliveyuan.tools.exception; +package cn.cliveyuan.tools.web.exception; /** * 验证码异常 diff --git a/src/main/java/com/gitee/cliveyuan/tools/exception/FileUploadException.java b/web/src/main/java/cn/cliveyuan/tools/web/exception/FileUploadException.java similarity index 90% rename from src/main/java/com/gitee/cliveyuan/tools/exception/FileUploadException.java rename to web/src/main/java/cn/cliveyuan/tools/web/exception/FileUploadException.java index de6d99c..3561e62 100644 --- a/src/main/java/com/gitee/cliveyuan/tools/exception/FileUploadException.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/exception/FileUploadException.java @@ -1,4 +1,6 @@ -package com.gitee.cliveyuan.tools.exception; +package cn.cliveyuan.tools.web.exception; + +import cn.cliveyuan.tools.common.exception.BaseException; /** * 文件上传异常 -- Gitee From 11c1f21c30fbc38ef7b6992b3eb0d94ae786c3d8 Mon Sep 17 00:00:00 2001 From: CliveYuan Date: Tue, 15 Jun 2021 17:06:31 +0800 Subject: [PATCH 2/4] deploy v3.1.1 --- README.md | 3 +++ .../java/cn/cliveyuan/tools/web/IpTools.java | 22 +++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 4502a1a..3de9215 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,9 @@ Some commonly used methods in JAVA ### v4.0.0 2021-06-09 * slim tools: split to common, httpclient, poi, web +### v3.1.1 2021-06-15 +* Refine IpTools + ### v3.1.0 2020-06-16 * add tools: SqlTools,ArrayTools * add methods: format,substring,trim in StringTools diff --git a/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java b/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java index e0fc2af..2c50808 100644 --- a/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java @@ -1,5 +1,6 @@ package cn.cliveyuan.tools.web; +import com.gitee.cliveyuan.tools.StringTools; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,20 +24,18 @@ public class IpTools { */ public static String getClientIp(HttpServletRequest request) { try { - String ipAddress = request.getHeader("x-forwarded-for"); - if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { + String ipAddress = request.getRemoteAddr(); + if (StringTools.isBlank(ipAddress)) { + ipAddress = request.getHeader("x-forwarded-for"); + } else if (StringTools.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("Proxy-Client-IP"); - } - if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { + } else if (StringTools.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("WL-Proxy-Client-IP"); } - if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { - ipAddress = request.getRemoteAddr(); - if ("127.0.0.1".equals(ipAddress) - || "0:0:0:0:0:0:0:1".equals(ipAddress) - || "localhost".equals(ipAddress)) { - return "127.0.0.1"; - } + if ("127.0.0.1".equals(ipAddress) + || "0:0:0:0:0:0:0:1".equals(ipAddress) + || "localhost".equals(ipAddress)) { + return "127.0.0.1"; } //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 if (ipAddress != null && ipAddress.length() > 15) { //"***.***.***.***".length() = 15 @@ -49,6 +48,5 @@ public class IpTools { logger.error("getClientIp Exception", e); return null; } - } } -- Gitee From 7c88a86e5e9a1d77abf02894468465c638433cfc Mon Sep 17 00:00:00 2001 From: CliveYuan Date: Tue, 15 Jun 2021 17:12:04 +0800 Subject: [PATCH 3/4] refine IpTools --- web/src/main/java/cn/cliveyuan/tools/web/IpTools.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java b/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java index 2c50808..cb531b6 100644 --- a/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java @@ -1,6 +1,6 @@ package cn.cliveyuan.tools.web; -import com.gitee.cliveyuan.tools.StringTools; +import cn.cliveyuan.tools.common.StringTools; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -- Gitee From 45d1915dfa9cd64cb89704428e84fc44ffd74885 Mon Sep 17 00:00:00 2001 From: CliveYuan Date: Wed, 16 Jun 2021 15:02:53 +0800 Subject: [PATCH 4/4] slim tools, deploy version v4.0.0 --- README.md | 2 +- .../cn/cliveyuan/tools/common/CsvTools.java | 2 +- .../cn/cliveyuan/tools/common/IdTools.java | 6 ++---- .../cliveyuan/tools/common/ThreadTools.java | 6 ++++-- .../cliveyuan/tools/common/ValidateTools.java | 1 + .../cn/cliveyuan/tools/common/ZipTools.java | 8 +++----- .../tools/common/bean/rsa/RSASignReq.java | 1 - .../common/bean/rsa/RSAVerifySignReq.java | 1 - .../common/enums/CaptchaGenerateType.java | 18 ----------------- .../tools/common/enums/CaptchaType.java | 20 ------------------- .../tools/common/enums/DateTimeField.java | 4 ++-- .../tools/common/enums/DateTimeFormat.java | 6 +++--- .../common/exception/EmailException.java | 2 ++ .../tools/common/test/CsvToolsTest.java | 2 +- .../tools/httpclient/HttpClientTools.java | 14 ++++++------- .../httpclient/bean/HttpClientRequest.java | 1 - .../cliveyuan/tools/poi/bean/ExcelReader.java | 4 ++-- .../cliveyuan/tools/poi/bean/ExcelWriter.java | 4 ++-- .../tools/poi/exception/ExcelException.java | 6 ++++++ .../tools/poi/inner/ExcelInnerHelper.java | 8 ++++---- .../tools/poi/test/ExcelToolsTest.java | 8 ++++---- pom.xml | 8 ++++++++ update_versions.sh | 3 +++ .../cn/cliveyuan/tools/web/CaptchaTools.java | 2 +- .../java/cn/cliveyuan/tools/web/IpTools.java | 4 ++-- .../cliveyuan/tools/web/VerifyCodeTools.java | 11 ++++++---- .../tools/web/bean/CaptchaGenerateType.java | 2 +- .../cliveyuan/tools/web/bean/CaptchaType.java | 2 +- .../tools/web/bean/FileUploadResponse.java | 1 + .../tools/web/exception/CaptchaException.java | 5 +++++ .../web/exception/FileUploadException.java | 3 +++ 31 files changed, 76 insertions(+), 89 deletions(-) delete mode 100644 common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaGenerateType.java delete mode 100644 common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaType.java create mode 100755 update_versions.sh diff --git a/README.md b/README.md index 3de9215..39c3c3d 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Some commonly used methods in JAVA ## Release Logs -### v4.0.0 2021-06-09 +### v4.0.0 2021-06-16 * slim tools: split to common, httpclient, poi, web ### v3.1.1 2021-06-15 diff --git a/common/src/main/java/cn/cliveyuan/tools/common/CsvTools.java b/common/src/main/java/cn/cliveyuan/tools/common/CsvTools.java index b0e585e..3c07a0a 100644 --- a/common/src/main/java/cn/cliveyuan/tools/common/CsvTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/CsvTools.java @@ -66,7 +66,7 @@ public class CsvTools { AssertTools.notBlank(absoluteFilePath, "csv file path is empty"); File file = new File(absoluteFilePath); AssertTools.isTrue(file.exists(), "csv file not exists"); - List list = new ArrayList(); + List list = new ArrayList<>(); try { String extension = FileTools.getExtension(absoluteFilePath); if (!CSV.equalsIgnoreCase(extension)) { diff --git a/common/src/main/java/cn/cliveyuan/tools/common/IdTools.java b/common/src/main/java/cn/cliveyuan/tools/common/IdTools.java index 8e262d4..82c552c 100644 --- a/common/src/main/java/cn/cliveyuan/tools/common/IdTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/IdTools.java @@ -14,11 +14,11 @@ import java.util.UUID; */ public class IdTools { - private IdTools(){} + private IdTools() { + } /** * 随机一个原生UUID - * */ public static String randomUUID() { return UUID.randomUUID().toString(); @@ -26,7 +26,6 @@ public class IdTools { /** * 随机去掉横杠的UUID - * */ public static String randomShortUUID() { String uuid = randomUUID(); @@ -36,7 +35,6 @@ public class IdTools { /** * 生成以日期时间开头的20位订单号 - * */ public static String genOrderNo() { String dateTime = DateTimeTools.format(DateTimeTools.now(), DateTimeFormat.DATE_TIME_0); diff --git a/common/src/main/java/cn/cliveyuan/tools/common/ThreadTools.java b/common/src/main/java/cn/cliveyuan/tools/common/ThreadTools.java index cce0a25..e79c210 100644 --- a/common/src/main/java/cn/cliveyuan/tools/common/ThreadTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/ThreadTools.java @@ -9,12 +9,14 @@ package cn.cliveyuan.tools.common; */ public class ThreadTools { - private ThreadTools(){} + private ThreadTools() { + } /** * 异步执行 * 适合调用不是很频繁的任务 * 频繁任务请使用线程池 + * * @param runnable 执行线程 */ public static void async(Runnable runnable) { @@ -27,7 +29,7 @@ public class ThreadTools { * @param seconds 秒 */ public static void sleep(int seconds) { - sleep((long)seconds * 1000L); + sleep((long) seconds * 1000L); } /** diff --git a/common/src/main/java/cn/cliveyuan/tools/common/ValidateTools.java b/common/src/main/java/cn/cliveyuan/tools/common/ValidateTools.java index 5439a94..e14e318 100644 --- a/common/src/main/java/cn/cliveyuan/tools/common/ValidateTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/ValidateTools.java @@ -238,6 +238,7 @@ public class ValidateTools { ValidationResult validationResult = ValidateTools.validateEntity(obj); AssertTools.isTrue(!validationResult.isHasErrors(), validationResult.getMessage()); } + /** * 校验实体,返回实体所有属性的校验结果 * diff --git a/common/src/main/java/cn/cliveyuan/tools/common/ZipTools.java b/common/src/main/java/cn/cliveyuan/tools/common/ZipTools.java index c720117..58e36a8 100644 --- a/common/src/main/java/cn/cliveyuan/tools/common/ZipTools.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/ZipTools.java @@ -4,8 +4,6 @@ import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.File; import java.util.Objects; @@ -25,7 +23,7 @@ public class ZipTools { /** * zip压缩 * - * @param sourcePath 源文件(夹)路径 + * @param sourcePath 源文件(夹)路径 * @param zipFilePath zip路径 * @throws ZipException */ @@ -49,7 +47,7 @@ public class ZipTools { zipFile.addFile(f, parameters); } } - } else { + } else { zipFile.addFile(currentFile, parameters); } @@ -59,7 +57,7 @@ public class ZipTools { * zip解压 * * @param zipFilePath zip路径 - * @param destPath 解压路径 + * @param destPath 解压路径 * @throws ZipException */ public static void unzip(String zipFilePath, String destPath) throws ZipException { diff --git a/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSASignReq.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSASignReq.java index 096042d..3fd24bb 100644 --- a/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSASignReq.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSASignReq.java @@ -25,7 +25,6 @@ public class RSASignReq implements Serializable { // endregion required - // region optional /** * 密钥算法, 默认 RSA diff --git a/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSAVerifySignReq.java b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSAVerifySignReq.java index 95bcad3..247a6ca 100644 --- a/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSAVerifySignReq.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/bean/rsa/RSAVerifySignReq.java @@ -29,7 +29,6 @@ public class RSAVerifySignReq implements Serializable { // endregion required - // region optional /** * 密钥算法, 默认 RSA diff --git a/common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaGenerateType.java b/common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaGenerateType.java deleted file mode 100644 index f21546f..0000000 --- a/common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaGenerateType.java +++ /dev/null @@ -1,18 +0,0 @@ -package cn.cliveyuan.tools.common.enums; - -/** - * Created by Clive on 2019/06/16. - */ -public enum CaptchaGenerateType { - - /** - * 回话 - */ - SESSION, - - /** - * 缓存 - */ - CACHE - -} diff --git a/common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaType.java b/common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaType.java deleted file mode 100644 index 46f7617..0000000 --- a/common/src/main/java/cn/cliveyuan/tools/common/enums/CaptchaType.java +++ /dev/null @@ -1,20 +0,0 @@ -package cn.cliveyuan.tools.common.enums; - -/** - * 验证码类型 - * - * Created by Clive on 2020/02/17. - */ -public enum CaptchaType { - - /** - * 字母数字 - */ - LETTER_NUM, - - /** - * 数学加法 - */ - MATH_ADD - -} diff --git a/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeField.java b/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeField.java index 81e7f51..4f507a5 100644 --- a/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeField.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeField.java @@ -3,9 +3,9 @@ package cn.cliveyuan.tools.common.enums; /** * 日期时间字段常量 * - * @author: clive + * @author clive * Created on 2018/07/23 - * @since: 1.0 + * @since 1.0 */ public enum DateTimeField { YEAR, diff --git a/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeFormat.java b/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeFormat.java index afaceb9..547cf19 100644 --- a/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeFormat.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/enums/DateTimeFormat.java @@ -3,9 +3,9 @@ package cn.cliveyuan.tools.common.enums; /** * 日期时间格式常量 * - * @author: clive + * @author clive * Created on 2018/07/23 - * @since: 1.0 + * @since 1.0 */ public enum DateTimeFormat { @@ -28,7 +28,7 @@ public enum DateTimeFormat { this.format = format; } - private String format; + private final String format; public String getFormat() { return format; diff --git a/common/src/main/java/cn/cliveyuan/tools/common/exception/EmailException.java b/common/src/main/java/cn/cliveyuan/tools/common/exception/EmailException.java index cb1dcf3..79d0c97 100644 --- a/common/src/main/java/cn/cliveyuan/tools/common/exception/EmailException.java +++ b/common/src/main/java/cn/cliveyuan/tools/common/exception/EmailException.java @@ -21,9 +21,11 @@ public class EmailException extends BaseException { public static EmailException authFail() { return new EmailException(1001, "account or password is wrong of email sender"); } + public static EmailException invalidAddress() { return new EmailException(1002, "Invalid Addresses"); } + public static EmailException unKnowError() { return new EmailException(1003, "UnKnow Error"); } diff --git a/common/src/test/java/cn/cliveyuan/tools/common/test/CsvToolsTest.java b/common/src/test/java/cn/cliveyuan/tools/common/test/CsvToolsTest.java index 823a53a..ba6e92b 100644 --- a/common/src/test/java/cn/cliveyuan/tools/common/test/CsvToolsTest.java +++ b/common/src/test/java/cn/cliveyuan/tools/common/test/CsvToolsTest.java @@ -23,7 +23,7 @@ public class CsvToolsTest { @Test public void writeAndRead() { String[] headers = {"姓名", "性别", "年龄", "生日", "身高", "体重", "职位", "国籍", "活了多少秒", "是程序员吗", "月薪"}; - List data = new ArrayList(); + List data = new ArrayList<>(); for (int i = 0; i < 10; i++) { data.add(Person.build() .Name("张三" + i) diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientTools.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientTools.java index 5ad8fcf..f08633e 100644 --- a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientTools.java +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/HttpClientTools.java @@ -1,9 +1,7 @@ package cn.cliveyuan.tools.httpclient; -import cn.cliveyuan.tools.common.AssertTools; import cn.cliveyuan.tools.common.StringTools; import cn.cliveyuan.tools.common.ValidateTools; -import cn.cliveyuan.tools.common.bean.ValidationResult; import cn.cliveyuan.tools.httpclient.bean.HttpClientRequest; import cn.cliveyuan.tools.httpclient.bean.HttpClientResponse; import cn.cliveyuan.tools.httpclient.bean.RequestContextTypeEnum; @@ -42,11 +40,11 @@ public class HttpClientTools { /** * 发起get请求 * - * @param url 请求地址 + * @param url 请求地址 * @param headers 请求头 * @return 响应内容 */ - public static String get(String url, Map headers) { + public static String get(String url, Map headers) { return request(HttpClientRequest.builder().url(url).headers(headers).build()).getResponse(); } @@ -70,10 +68,10 @@ public class HttpClientTools { * * @param url 请求地址 * @param paramMap 参数 - * @param headers 请求头 + * @param headers 请求头 * @return 响应内容 */ - public static String post(String url, Map paramMap, Map headers) { + public static String post(String url, Map paramMap, Map headers) { return request(HttpClientRequest.builder() .url(url) .method(RequestMethodEnum.POST) @@ -103,10 +101,10 @@ public class HttpClientTools { * * @param url 请求地址 * @param requestBody 请求体 - * @param headers 请求头 + * @param headers 请求头 * @return 响应内容 */ - public static String postJson(String url, String requestBody, Map headers) { + public static String postJson(String url, String requestBody, Map headers) { return request(HttpClientRequest.builder() .url(url) .method(RequestMethodEnum.POST) diff --git a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientRequest.java b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientRequest.java index f6cd2dd..4839b3e 100644 --- a/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientRequest.java +++ b/httpclient/src/main/java/cn/cliveyuan/tools/httpclient/bean/HttpClientRequest.java @@ -7,7 +7,6 @@ import org.apache.commons.lang3.StringUtils; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.HashMap; import java.util.Map; diff --git a/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelReader.java b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelReader.java index 561e76a..49f6245 100644 --- a/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelReader.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelReader.java @@ -1,11 +1,11 @@ package cn.cliveyuan.tools.poi.bean; -import java.util.ArrayList; import lombok.Builder; import lombok.Data; import lombok.extern.slf4j.Slf4j; import java.io.Serializable; +import java.util.ArrayList; import java.util.List; /** @@ -24,7 +24,7 @@ public class ExcelReader implements Serializable { * sheet 信息列表 */ @Builder.Default - private List sheetInfoList = new ArrayList(); + private List sheetInfoList = new ArrayList<>(); /** * 开始行号 */ diff --git a/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelWriter.java b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelWriter.java index 9df0552..981ba52 100644 --- a/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelWriter.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/bean/ExcelWriter.java @@ -1,11 +1,11 @@ package cn.cliveyuan.tools.poi.bean; -import java.util.ArrayList; import lombok.Builder; import lombok.Data; import lombok.extern.slf4j.Slf4j; import java.io.Serializable; +import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -34,7 +34,7 @@ public class ExcelWriter implements Serializable { * sheet 内容列表 */ @Builder.Default - private List sheetContentList = new ArrayList(); + private List sheetContentList = new ArrayList<>(); public ExcelWriter addSheetContent(String sheetName, String[] headers, Collection data) { this.sheetContentList.add(new SheetContent(sheetName, headers, data)); diff --git a/poi/src/main/java/cn/cliveyuan/tools/poi/exception/ExcelException.java b/poi/src/main/java/cn/cliveyuan/tools/poi/exception/ExcelException.java index b370210..7f46dd4 100644 --- a/poi/src/main/java/cn/cliveyuan/tools/poi/exception/ExcelException.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/exception/ExcelException.java @@ -18,21 +18,27 @@ public class ExcelException extends BaseException { public static ExcelException failToMkdirs() { return new ExcelException(1000, "fail to create folder(s)"); } + public static ExcelException failToGenerateExcel() { return new ExcelException(1001, "fail to generate excel"); } + public static ExcelException excelReadError() { return new ExcelException(1002, "excel read error"); } + public static ExcelException failToReadSheet() { return new ExcelException(1003, "fail to read sheet"); } + public static ExcelException failToParseExcel() { return new ExcelException(1004, "fail to parse excel"); } + public static ExcelException notSupport(String extension) { return new ExcelException(1005, "not support this excel format:" + extension); } + public static ExcelException dataConvertError() { return new ExcelException(1006, "data convert error"); } diff --git a/poi/src/main/java/cn/cliveyuan/tools/poi/inner/ExcelInnerHelper.java b/poi/src/main/java/cn/cliveyuan/tools/poi/inner/ExcelInnerHelper.java index 2f4150b..4711414 100644 --- a/poi/src/main/java/cn/cliveyuan/tools/poi/inner/ExcelInnerHelper.java +++ b/poi/src/main/java/cn/cliveyuan/tools/poi/inner/ExcelInnerHelper.java @@ -71,10 +71,10 @@ public class ExcelInnerHelper { */ public static List read(ExcelReader excelReader) { if (Objects.isNull(excelReader)) { - return new ArrayList(); + return new ArrayList<>(); } AssertTools.notNull(excelReader, "excelReader is required"); - List resultList = new ArrayList(); + List resultList = new ArrayList<>(); List sheetInfoList = excelReader.getSheetInfoList(); AssertTools.notEmpty(sheetInfoList, "sheetInfoList can't be empty"); String[] sheetNames = sheetInfoList.stream().map(SheetInfo::getSheetName).toArray(String[]::new); @@ -137,7 +137,7 @@ public class ExcelInnerHelper { // 仅读取第一个sheet, 如需读取其他sheet, 需自行封装方法 获取sheet数量:workbook.getNumberOfSheets() // 创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回 int numberOfSheets = workbook.getNumberOfSheets(); - List sheets = new ArrayList(); + List sheets = new ArrayList<>(); for (int i = 0; i < numberOfSheets; i++) { Sheet sheet = workbook.getSheetAt(i); if (Objects.isNull(sheet)) { @@ -152,7 +152,7 @@ public class ExcelInnerHelper { sheets.addAll(collect); } for (Sheet sheet : sheets) { - List list = new ArrayList(); + List list = new ArrayList<>(); // 获得当前sheet的开始行 int firstRowNum = sheet.getFirstRowNum(); diff --git a/poi/src/test/java/cn/cliveyuan/tools/poi/test/ExcelToolsTest.java b/poi/src/test/java/cn/cliveyuan/tools/poi/test/ExcelToolsTest.java index 46e49c4..7059bc1 100644 --- a/poi/src/test/java/cn/cliveyuan/tools/poi/test/ExcelToolsTest.java +++ b/poi/src/test/java/cn/cliveyuan/tools/poi/test/ExcelToolsTest.java @@ -29,7 +29,7 @@ public class ExcelToolsTest { String[] headers2 = {"name", "url"}; String[] headers3 = {"ide", "lang",}; - List data1 = new ArrayList(); + List data1 = new ArrayList<>(); for (int i = 0; i < 10; i++) { data1.add(ExcelProfile.builder() .no(i + 1) @@ -39,7 +39,7 @@ public class ExcelToolsTest { .build() ); } - List data2 = new ArrayList(); + List data2 = new ArrayList<>(); for (int i = 0; i < 10; i++) { data2.add(ExcelWebsite.builder() .name("clive-" + i) @@ -47,7 +47,7 @@ public class ExcelToolsTest { .build() ); } - List data3 = new ArrayList(); + List data3 = new ArrayList<>(); for (int i = 0; i < 10; i++) { data3.add(ExcelIdea.builder() .ide("idea-" + i) @@ -88,7 +88,7 @@ public class ExcelToolsTest { public void writeAndReadSimply() { String pathname = ExcelToolsTest.class.getResource("/").getPath(); String[] headers = {"编号", "名字", "手机号", "年龄"}; - List data = new ArrayList(); + List data = new ArrayList<>(); for (int i = 0; i < 10; i++) { data.add(ExcelProfile.builder() .no(i + 1) diff --git a/pom.xml b/pom.xml index f90eea0..460a5e5 100644 --- a/pom.xml +++ b/pom.xml @@ -250,6 +250,14 @@ + + org.codehaus.mojo + versions-maven-plugin + 2.7 + + false + +
diff --git a/update_versions.sh b/update_versions.sh new file mode 100755 index 0000000..3afeec6 --- /dev/null +++ b/update_versions.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +mvn versions:set -DnewVersion=4.0.0 diff --git a/web/src/main/java/cn/cliveyuan/tools/web/CaptchaTools.java b/web/src/main/java/cn/cliveyuan/tools/web/CaptchaTools.java index ec5504f..65c387a 100755 --- a/web/src/main/java/cn/cliveyuan/tools/web/CaptchaTools.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/CaptchaTools.java @@ -3,11 +3,11 @@ package cn.cliveyuan.tools.web; import cn.cliveyuan.tools.common.AssertTools; import cn.cliveyuan.tools.common.IdTools; import cn.cliveyuan.tools.common.StringTools; +import cn.cliveyuan.tools.web.bean.Captcha; import cn.cliveyuan.tools.web.bean.CaptchaCodeResult; import cn.cliveyuan.tools.web.bean.CaptchaGenerateType; import cn.cliveyuan.tools.web.bean.CaptchaRequest; import cn.cliveyuan.tools.web.bean.CaptchaType; -import cn.cliveyuan.tools.web.bean.Captcha; import cn.cliveyuan.tools.web.bean.CaptchaValidate; import cn.cliveyuan.tools.web.exception.CaptchaException; import com.alibaba.fastjson.JSON; diff --git a/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java b/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java index cb531b6..bc8846f 100644 --- a/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/IpTools.java @@ -24,12 +24,12 @@ public class IpTools { */ public static String getClientIp(HttpServletRequest request) { try { - String ipAddress = request.getRemoteAddr(); + String ipAddress = request.getRemoteAddr(); if (StringTools.isBlank(ipAddress)) { ipAddress = request.getHeader("x-forwarded-for"); } else if (StringTools.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("Proxy-Client-IP"); - } else if (StringTools.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) { + } else if (StringTools.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("WL-Proxy-Client-IP"); } if ("127.0.0.1".equals(ipAddress) diff --git a/web/src/main/java/cn/cliveyuan/tools/web/VerifyCodeTools.java b/web/src/main/java/cn/cliveyuan/tools/web/VerifyCodeTools.java index 053dd6a..b178ec2 100755 --- a/web/src/main/java/cn/cliveyuan/tools/web/VerifyCodeTools.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/VerifyCodeTools.java @@ -37,6 +37,7 @@ public class VerifyCodeTools { /** * 生成字母数字验证码 + * * @param verifySize 验证码长度 * @return */ @@ -49,15 +50,16 @@ public class VerifyCodeTools { /** * 生成范围内的加法验证码 (边界都包括) + * * @param startInt 开始 (大于0) - * @param endInt 结束 (大于0) + * @param endInt 结束 (大于0) * @return */ public static CaptchaCodeResult generateMathAddVerifyCode(int startInt, int endInt) { - if (startInt <=0) { + if (startInt <= 0) { startInt = 1; } - if (endInt <=0) { + if (endInt <= 0) { endInt = 10; } int int1 = RandomUtils.nextInt(startInt, endInt + 1); @@ -209,6 +211,7 @@ public class VerifyCodeTools { g2.dispose(); ImageIO.write(image, "jpg", os); } + /** * 输出指定验证码图片流 * @@ -219,7 +222,7 @@ public class VerifyCodeTools { * @throws IOException */ public static void outputImage(int w, int h, OutputStream os, String code) throws IOException { - outputImage(w, h, os, code, true); + outputImage(w, h, os, code, true); } private static Color getRandColor(int fc, int bc) { diff --git a/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaGenerateType.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaGenerateType.java index cfa388c..d08eedc 100644 --- a/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaGenerateType.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaGenerateType.java @@ -6,7 +6,7 @@ package cn.cliveyuan.tools.web.bean; public enum CaptchaGenerateType { /** - * 回话 + * 会话 */ SESSION, diff --git a/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaType.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaType.java index 3885939..faf149e 100644 --- a/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaType.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/CaptchaType.java @@ -2,7 +2,7 @@ package cn.cliveyuan.tools.web.bean; /** * 验证码类型 - * + *

* Created by Clive on 2020/02/17. */ public enum CaptchaType { diff --git a/web/src/main/java/cn/cliveyuan/tools/web/bean/FileUploadResponse.java b/web/src/main/java/cn/cliveyuan/tools/web/bean/FileUploadResponse.java index 0125e72..da17036 100644 --- a/web/src/main/java/cn/cliveyuan/tools/web/bean/FileUploadResponse.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/bean/FileUploadResponse.java @@ -5,6 +5,7 @@ import lombok.Data; /** * 文件上传响应 + * * @author clive * Created on 2018/07/27 * @since 1.0 diff --git a/web/src/main/java/cn/cliveyuan/tools/web/exception/CaptchaException.java b/web/src/main/java/cn/cliveyuan/tools/web/exception/CaptchaException.java index f606c3b..42a22c3 100644 --- a/web/src/main/java/cn/cliveyuan/tools/web/exception/CaptchaException.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/exception/CaptchaException.java @@ -27,18 +27,23 @@ public class CaptchaException extends IllegalArgumentException { public static CaptchaException captchaIsNull() { return new CaptchaException(1000, "验证码为空"); } + public static CaptchaException idIsEmpty() { return new CaptchaException(1001, "验证码错误"); } + public static CaptchaException valueIsEmpty() { return new CaptchaException(1002, "验证码为空"); } + public static CaptchaException notMatch() { return new CaptchaException(1003, "验证码错误"); } + public static CaptchaException notInSession() { return new CaptchaException(1004, "验证码错误"); } + public static CaptchaException parseJsonError() { return new CaptchaException(1005, "验证码解析异常"); } diff --git a/web/src/main/java/cn/cliveyuan/tools/web/exception/FileUploadException.java b/web/src/main/java/cn/cliveyuan/tools/web/exception/FileUploadException.java index 3561e62..9bb4d01 100644 --- a/web/src/main/java/cn/cliveyuan/tools/web/exception/FileUploadException.java +++ b/web/src/main/java/cn/cliveyuan/tools/web/exception/FileUploadException.java @@ -22,12 +22,15 @@ public class FileUploadException extends BaseException { public static FileUploadException notSupport(String message) { return new FileUploadException(1001, message); } + public static FileUploadException failToMkdirs(String folder) { return new FileUploadException(1002, "fail to create folder(s): " + folder); } + public static FileUploadException fileEmpty() { return new FileUploadException(1003, "file is empty"); } + public static FileUploadException unKnowError() { return new FileUploadException(1003, "unKnow Error"); } -- Gitee