├── _config.yml ├── .settings ├── org.eclipse.m2e.core.prefs ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── src ├── main │ ├── resources │ │ └── log4j.properties │ └── java │ │ └── com │ │ └── zhazhapan │ │ ├── util │ │ ├── enums │ │ │ ├── JsonType.java │ │ │ ├── FieldModifier.java │ │ │ ├── JsonMethod.java │ │ │ └── LogLevel.java │ │ ├── interfaces │ │ │ ├── IChecker.java │ │ │ ├── SimpleHutoolWatcher.java │ │ │ ├── SimpleLogService.java │ │ │ └── MultipartFileService.java │ │ ├── annotation │ │ │ ├── SensitiveData.java │ │ │ ├── AopLog.java │ │ │ ├── ToJsonString.java │ │ │ └── FieldChecking.java │ │ ├── WebUtils.java │ │ ├── json │ │ │ └── PrettyJsonObject.java │ │ ├── HttpUtils.java │ │ ├── model │ │ │ ├── SimpleColor.java │ │ │ ├── CheckResult.java │ │ │ ├── SimpleMultipartFile.java │ │ │ ├── SimpleDateTime.java │ │ │ └── ResultObject.java │ │ ├── fx │ │ │ └── FxmlLoader.java │ │ ├── common │ │ │ └── interceptor │ │ │ │ └── ToStringMethodInterceptor.java │ │ ├── office │ │ │ ├── MsUtils.java │ │ │ └── MsExcelUtils.java │ │ ├── decryption │ │ │ ├── SimpleDecrypt.java │ │ │ └── JavaDecrypt.java │ │ ├── AopLogUtils.java │ │ ├── encryption │ │ │ └── SimpleEncrypt.java │ │ ├── dialog │ │ │ ├── Dialogs.java │ │ │ └── Alerts.java │ │ ├── ListUtils.java │ │ ├── CryptUtils.java │ │ ├── web │ │ │ └── BaseController.java │ │ ├── Downloader.java │ │ ├── ThreadPool.java │ │ ├── Calculator.java │ │ ├── RandomUtils.java │ │ ├── LoggerUtils.java │ │ ├── MailSender.java │ │ ├── Utils.java │ │ └── ReflectUtils.java │ │ └── modules │ │ └── constant │ │ ├── HttpHeaders.java │ │ └── ValueConsts.java └── test │ └── java │ └── com │ └── zhazhapan │ └── util │ ├── WebUtilsTest.java │ ├── model │ ├── SimpleDateTimeTest.java │ ├── B.java │ ├── A.java │ └── TestBean.java │ ├── LoggerUtilsTest.java │ ├── PrettyJsonObjectTest.java │ ├── JavaEncryptTest.java │ ├── AppTest.java │ ├── FastJsonTest.java │ ├── CalculatorTest.java │ ├── ReflectUtilsTest.java │ ├── RandomUtilsTest.java │ ├── office │ ├── MsExcelUtilsTest.java │ └── MsWordUtilsTest.java │ ├── RandomTest.java │ ├── UtilsTest.java │ ├── CommonsTest.java │ ├── ArrayUtilsTest.java │ ├── BeanUtilsTest.java │ ├── FormatterTest.java │ ├── NetUtilsTest.java │ ├── FileExecutorTest.java │ └── CheckerTest.java ├── .project ├── .gitignore ├── LICENSE ├── .classpath └── pom.xml /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-hacker -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding/=UTF-8 5 | -------------------------------------------------------------------------------- /src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO, stdout 2 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 | log4j.appender.stdout.Target=System.out 4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c]: %m%n -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/enums/JsonType.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.enums; 2 | 3 | /** 4 | * @author pantao 5 | * @since 2018/1/19 6 | */ 7 | public enum JsonType { 8 | /** 9 | * 压缩的 10 | */ 11 | CONDENSED, 12 | 13 | /** 14 | * 格式化的 15 | */ 16 | PRETTY 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/WebUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertTrue; 6 | 7 | public class WebUtilsTest { 8 | 9 | @Test 10 | public void testScriptFilter() { 11 | assertTrue("ff".equals(WebUtils.scriptFilter("ff"))); 12 | } 13 | } -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/interfaces/IChecker.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.interfaces; 2 | 3 | /** 4 | * 回调检查函数 5 | * 6 | * @author pantao 7 | * @since 2018/5/9 8 | */ 9 | @FunctionalInterface 10 | public interface IChecker { 11 | 12 | /** 13 | * 自定义检查函数 14 | * 15 | * @return {@link Boolean} 16 | */ 17 | boolean check(); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/annotation/SensitiveData.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.annotation; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * 此注解仅作用于 {@link String} 类型 7 | * 8 | * @author pantao 9 | * @since 2018/9/23 10 | */ 11 | @Documented 12 | @Target(ElementType.FIELD) 13 | @Retention(RetentionPolicy.RUNTIME) 14 | public @interface SensitiveData {} 15 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/enums/FieldModifier.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.enums; 2 | 3 | /** 4 | * 字段的权限修饰符 5 | * 6 | * @author pantao 7 | * @since 2018/1/20 8 | */ 9 | public enum FieldModifier { 10 | 11 | /** 12 | * public类型 13 | */ 14 | PUBLIC, 15 | 16 | /** 17 | * private类型 18 | */ 19 | PRIVATE, 20 | 21 | /** 22 | * 全部类型 23 | */ 24 | ALL 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/annotation/AopLog.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.annotation; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * @author pantao 7 | * @since 2018/9/10 8 | **/ 9 | @Documented 10 | @Target(ElementType.METHOD) 11 | @Retention(RetentionPolicy.RUNTIME) 12 | public @interface AopLog { 13 | 14 | /** 15 | * the description of method 16 | * 17 | * @return {@link String} 18 | */ 19 | String value() default ""; 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/model/SimpleDateTimeTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.model; 2 | 3 | import com.zhazhapan.util.DateUtils; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | import java.util.Date; 8 | 9 | public class SimpleDateTimeTest { 10 | 11 | @Test 12 | public void getMonth() { 13 | SimpleDateTime dateTime = new SimpleDateTime(); 14 | Assert.assertEquals(DateUtils.getMonth(new Date()), dateTime.getMonth().toString()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/enums/JsonMethod.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.enums; 2 | 3 | /** 4 | * Bean类字段转换成JSON的方式 5 | * 6 | * @author pantao 7 | * @since 2018/1/20 8 | */ 9 | public enum JsonMethod { 10 | 11 | /** 12 | * 使用阿里巴巴的JSONObject 13 | */ 14 | AUTO, 15 | 16 | /** 17 | * 使用手动的方式 18 | * 19 | * @deprecated 推荐使用 {@link JsonMethod#MANUAL} 20 | */ 21 | HANDLE, 22 | 23 | /** 24 | * 手动的方式 25 | * 26 | * @since 1.0.8 27 | */ 28 | MANUAL 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/LoggerUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import org.junit.Test; 4 | 5 | public class LoggerUtilsTest { 6 | 7 | @Test 8 | public void getLogger() { 9 | LoggerUtils.getLogger(this).info("test get logger via object"); 10 | LoggerUtils.getLogger(null).error("test get logger via null"); 11 | } 12 | 13 | @Test 14 | public void info() { 15 | LoggerUtils.info("today is a nice day"); 16 | LoggerUtils.info(this, "%s is fall in loved", "god"); 17 | } 18 | } -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/PrettyJsonObjectTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.zhazhapan.util.json.PrettyJsonObject; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author pantao 8 | * @since 2018/1/22 9 | */ 10 | public class PrettyJsonObjectTest { 11 | 12 | @Test 13 | public void test() { 14 | PrettyJsonObject object = new PrettyJsonObject(); 15 | object.put("i", "love you!"); 16 | object.put("you", "love me?"); 17 | System.out.println(object.toString()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/enums/LogLevel.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.enums; 2 | 3 | /** 4 | * @author pantao 5 | * @since 2018/7/5 6 | */ 7 | public enum LogLevel { 8 | 9 | /** 10 | * 普通消息 11 | * 12 | * @since 1.0.9 13 | */ 14 | INFO, 15 | 16 | /** 17 | * 警告 18 | * 19 | * @since 1.0.9 20 | */ 21 | WARN, 22 | 23 | /** 24 | * 错误 25 | * 26 | * @since 1.0.9 27 | */ 28 | ERROR, 29 | 30 | /** 31 | * 严重错误 32 | * 33 | * @since 1.0.9 34 | */ 35 | FATAL 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/interfaces/SimpleHutoolWatcher.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.interfaces; 2 | 3 | import java.nio.file.Path; 4 | import java.nio.file.WatchEvent; 5 | 6 | /** 7 | * @author pantao 8 | * @since 2018/10/11 9 | **/ 10 | public interface SimpleHutoolWatcher { 11 | 12 | /** 13 | * 文件发生变化 14 | * 15 | * @param event 时间 16 | * @param currentPath 当前路径 17 | */ 18 | default void onModify(WatchEvent event, Path currentPath) {} 19 | 20 | /** 21 | * 执行一些业务代码 22 | */ 23 | default void doSomething() {} 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/WebUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.zhazhapan.modules.constant.ValueConsts; 4 | 5 | /** 6 | * @author pantao 7 | * @since 2018/1/30 8 | * @deprecated 这个类将不再更新维护,请使用 {@link NetUtils} 9 | */ 10 | public class WebUtils { 11 | 12 | private WebUtils() {} 13 | 14 | /** 15 | * 脚本过滤 16 | * 17 | * @param string {@link String} 18 | * 19 | * @return 过滤后的字符串 20 | */ 21 | public static String scriptFilter(String string) { 22 | return Checker.checkNull(string).replaceAll(ValueConsts.SCRIPT_FILTER_PATTERN, ""); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | util 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/model/B.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.model; 2 | 3 | /** 4 | * @author pantao 5 | * @since 2018/9/22 6 | */ 7 | public class B { 8 | 9 | private Integer age; 10 | 11 | private String bio; 12 | 13 | @Override 14 | public String toString() { 15 | return "B{" + "age=" + age + ", bio='" + bio + '\'' + '}'; 16 | } 17 | 18 | public String getBio() { 19 | return bio; 20 | } 21 | 22 | public void setBio(String bio) { 23 | this.bio = bio; 24 | } 25 | 26 | public Integer getAge() { 27 | return age; 28 | } 29 | 30 | public void setAge(Integer age) { 31 | this.age = age; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 6 | org.eclipse.jdt.core.compiler.compliance=1.8 7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 13 | org.eclipse.jdt.core.compiler.source=1.8 14 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/JavaEncryptTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.zhazhapan.util.encryption.JavaEncrypt; 4 | import org.junit.Test; 5 | 6 | import java.io.UnsupportedEncodingException; 7 | import java.security.NoSuchAlgorithmException; 8 | 9 | import static org.junit.Assert.assertTrue; 10 | 11 | /** 12 | * @author pantao 13 | * @since 2018/1/18 14 | */ 15 | public class JavaEncryptTest { 16 | 17 | @Test 18 | public void testSha() throws NoSuchAlgorithmException { 19 | assertTrue("7c4a8d09ca3762af61e59520943dc26494f8941b".equals(JavaEncrypt.sha("123456", 16))); 20 | } 21 | 22 | @Test 23 | public void testSha256() throws NoSuchAlgorithmException, UnsupportedEncodingException { 24 | System.out.println(JavaEncrypt.sha256("123456")); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/json/PrettyJsonObject.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.json; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.alibaba.fastjson.serializer.JSONSerializer; 5 | import com.alibaba.fastjson.serializer.SerializeWriter; 6 | import com.zhazhapan.util.Formatter; 7 | 8 | /** 9 | * 继承自{@link JSONObject} 10 | * 11 | * @author pantao 12 | * @since 2018/1/22 13 | */ 14 | public class PrettyJsonObject extends JSONObject { 15 | 16 | /** 17 | * 重写toString方法,对JSON进行格式化 18 | * 19 | * @return {@link String} 20 | */ 21 | @Override 22 | public String toString() { 23 | try (SerializeWriter out = new SerializeWriter()) { 24 | new JSONSerializer(out).write(this); 25 | return Formatter.formatJson(out.toString()); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/HttpUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import javax.servlet.http.Cookie; 4 | 5 | /** 6 | * @author pantao 7 | * @since 2018/1/26 8 | * @deprecated 这个类将不再更新维护,请使用 {@link NetUtils} 9 | */ 10 | public class HttpUtils { 11 | 12 | private HttpUtils() {} 13 | 14 | /** 15 | * 通过名称获取Cookie 16 | * 17 | * @param name Cookie名 18 | * @param cookies cookie数组 19 | * 20 | * @return {@link Cookie} 21 | */ 22 | public static Cookie getCookie(String name, Cookie[] cookies) { 23 | if (Checker.isNotNull(cookies)) { 24 | for (Cookie cookie : cookies) { 25 | if (name.equals(cookie.getName())) { 26 | return cookie; 27 | } 28 | } 29 | } 30 | return null; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/model/SimpleColor.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.model; 2 | 3 | /** 4 | * @author pantao 5 | */ 6 | public class SimpleColor { 7 | 8 | public int r = 0; 9 | 10 | public int g = 0; 11 | 12 | public int b = 0; 13 | 14 | public double opacity = 1; 15 | 16 | public SimpleColor(int r, int g, int b, double opacity) { 17 | this.r = r; 18 | this.g = g; 19 | this.b = b; 20 | this.opacity = opacity; 21 | } 22 | 23 | /** 24 | * 转换为十六进制 25 | * 26 | * @return {@link String} 27 | */ 28 | public String toHexString() { 29 | return Integer.toHexString(r) + Integer.toHexString(g) + Integer.toHexString(b); 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return r + "," + g + "," + b + "," + opacity; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/fx/FxmlLoader.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.fx; 2 | 3 | import javafx.fxml.FXMLLoader; 4 | import javafx.scene.Scene; 5 | import javafx.scene.layout.BorderPane; 6 | import org.apache.log4j.Logger; 7 | 8 | import java.io.IOException; 9 | import java.net.URL; 10 | 11 | /** 12 | * @author pantao 13 | */ 14 | public class FxmlLoader { 15 | 16 | private static final Logger logger = Logger.getLogger(FxmlLoader.class); 17 | 18 | private FxmlLoader() {} 19 | 20 | /** 21 | * 加密fxml文件 22 | * 23 | * @param url {@link URL} 24 | * 25 | * @return {@link Scene} 26 | * 27 | * @throws IOException 异常 28 | */ 29 | public static Scene loadFxml(URL url) throws IOException { 30 | logger.info("load fxml from url: " + url); 31 | BorderPane root = FXMLLoader.load(url); 32 | return new Scene(root); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/model/A.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.model; 2 | 3 | /** 4 | * @author pantao 5 | * @since 2018/9/22 6 | */ 7 | public class A { 8 | 9 | private String name; 10 | 11 | private String gender; 12 | 13 | private Integer age; 14 | 15 | public String getName() { 16 | return name; 17 | } 18 | 19 | public void setName(String name) { 20 | this.name = name; 21 | } 22 | 23 | public String getGender() { 24 | return gender; 25 | } 26 | 27 | public void setGender(String gender) { 28 | this.gender = gender; 29 | } 30 | 31 | public Integer getAge() { 32 | return age; 33 | } 34 | 35 | public void setAge(Integer age) { 36 | this.age = age; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "A{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", age=" + age + '}'; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/annotation/ToJsonString.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.annotation; 2 | 3 | import com.zhazhapan.util.enums.FieldModifier; 4 | import com.zhazhapan.util.enums.JsonMethod; 5 | import com.zhazhapan.util.enums.JsonType; 6 | 7 | import java.lang.annotation.*; 8 | 9 | /** 10 | * 重写类的toString方法,返回一个JSON字符串 11 | * 12 | * @author pantao 13 | * @since 2018/1/19 14 | */ 15 | @Documented 16 | @Retention(RetentionPolicy.RUNTIME) 17 | @Target(ElementType.TYPE) 18 | public @interface ToJsonString { 19 | 20 | /** 21 | * JSON字符串的格式 22 | * 23 | * @return {@link String} 24 | */ 25 | JsonType type() default JsonType.CONDENSED; 26 | 27 | /** 28 | * 哪些修饰符的字段需要转换成JSON 29 | * 30 | * @return {@link FieldModifier} 31 | */ 32 | FieldModifier modifier() default FieldModifier.ALL; 33 | 34 | 35 | /** 36 | * JSON转换的方式 37 | * 38 | * @return {@link JsonMethod} 39 | */ 40 | JsonMethod method() default JsonMethod.AUTO; 41 | } -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | import java.util.Random; 8 | 9 | /** 10 | * @author pantao 11 | */ 12 | public class AppTest extends TestCase { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest(String testName) { 19 | super(testName); 20 | } 21 | 22 | /** 23 | * @return the suite of tests being tested 24 | */ 25 | public static Test suite() { 26 | return new TestSuite(AppTest.class); 27 | } 28 | 29 | /** 30 | * Rigourous Test :-) 31 | */ 32 | public void testApp() { 33 | assertTrue(true); 34 | } 35 | 36 | @org.junit.Test 37 | public void test() { 38 | for (int i = 0; i < 100; i++) { 39 | System.out.println(new Random().nextInt(1)); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | 24 | target/ 25 | pom.xml.tag 26 | pom.xml.releaseBackup 27 | pom.xml.versionsBackup 28 | pom.xml.next 29 | release.properties 30 | dependency-reduced-pom.xml 31 | buildNumber.properties 32 | .mvn/timing.properties 33 | 34 | # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) 35 | !/.mvn/wrapper/maven-wrapper.jar 36 | 37 | ### IntelliJ IDEA ### 38 | .idea 39 | *.iws 40 | *.iml 41 | *.ipr 42 | 43 | .DS_Store 44 | !.mvn/wrapper/maven-wrapper.jar 45 | 46 | ### STS ### 47 | .apt_generated 48 | .classpath 49 | .factorypath 50 | .project 51 | .settings 52 | .springBeans 53 | 54 | ### NetBeans ### 55 | nbproject/private/ 56 | build/ 57 | nbbuild/ 58 | dist/ 59 | nbdist/ 60 | .nb-gradle/ -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/common/interceptor/ToStringMethodInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.common.interceptor; 2 | 3 | import net.sf.cglib.proxy.MethodInterceptor; 4 | import net.sf.cglib.proxy.MethodProxy; 5 | import org.apache.log4j.Logger; 6 | 7 | import java.lang.reflect.Method; 8 | 9 | /** 10 | * @author pantao 11 | * @since 2018/1/20 12 | */ 13 | public class ToStringMethodInterceptor implements MethodInterceptor { 14 | 15 | private static Logger logger = Logger.getLogger(ToStringMethodInterceptor.class); 16 | 17 | /** 18 | * 此方法实现了cglib的动态代理 19 | * 20 | * @param o {@link Object} 21 | * @param method {@link Method} 22 | * @param objects {@link Object[]} 23 | * @param methodProxy {@link MethodProxy} 24 | * 25 | * @return {@link Object} 26 | * 27 | * @throws Throwable 异常 28 | */ 29 | @Override 30 | public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { 31 | return methodProxy.invokeSuper(o, objects); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 潘滔 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/model/TestBean.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.model; 2 | 3 | import com.zhazhapan.modules.constant.ValueConsts; 4 | import com.zhazhapan.util.annotation.FieldChecking; 5 | 6 | /** 7 | * @author pantao 8 | * @since 2018/7/17 9 | */ 10 | @FieldChecking(code = 200, message = "测试通过", status = ValueConsts.SUCCESS) 11 | public class TestBean { 12 | 13 | /** 14 | * 此字段不进行验证 15 | */ 16 | public boolean none; 17 | 18 | @FieldChecking(message = "{}必须大于0", expression = "val!=null&&val>0") 19 | public Integer id; 20 | 21 | /** 22 | * 使用默认设置 23 | */ 24 | @FieldChecking() 25 | public String defaultChecking; 26 | 27 | @FieldChecking(message = "用户名长度必须是4~16位的字符串", expression = "val!=null&&val.length()>3&&val.length()<17") 28 | public String username; 29 | 30 | /** 31 | * 正则匹配(用英文冒号“:”开头,匹配时开头的英文冒号“:”会被忽略) 32 | */ 33 | @FieldChecking(message = "邮箱不合法", expression = ":^[a-z]+@[a-z]+\\.[a-z]+$") 34 | public String email; 35 | 36 | @FieldChecking(message = "年龄必须0且小于150", expression = "val!=null&&val>0&&val<150") 37 | public Integer age; 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/interfaces/SimpleLogService.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.interfaces; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | /** 7 | * @author pantao 8 | * @since 2018/10/14 9 | **/ 10 | public interface SimpleLogService { 11 | 12 | /** 13 | * 保存日志 14 | * 15 | * @param log {@link T} 16 | * 17 | * @return {@link T} 18 | * 19 | * @since 1.1.1 20 | */ 21 | T save(T log); 22 | 23 | /** 24 | * 保存异常信息 25 | * 26 | * @param log {@link T} 27 | * @param exceptionClass 异常类 28 | * @param exceptionDetail 异常详情 29 | * 30 | * @return {@link T} 31 | * 32 | * @since 1.1.1 33 | */ 34 | T saveException(T log, String exceptionClass, String exceptionDetail); 35 | 36 | /** 37 | * 清楚过期的日志 38 | * 39 | * @since 1.1.1 40 | */ 41 | default void removeExpired() {} 42 | 43 | /** 44 | * 查询日志 45 | * 46 | * @param criteria 查询日志 47 | * 48 | * @return 日志列表 49 | * 50 | * @since 1.1.1 51 | */ 52 | default List listBy(Map criteria) { return null;} 53 | } 54 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/annotation/FieldChecking.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.annotation; 2 | 3 | import com.zhazhapan.modules.constant.ValueConsts; 4 | 5 | import java.lang.annotation.*; 6 | 7 | /** 8 | * @author pantao 9 | * @since 2018/7/17 10 | */ 11 | @Documented 12 | @Retention(RetentionPolicy.RUNTIME) 13 | @Target({ElementType.FIELD, ElementType.TYPE}) 14 | public @interface FieldChecking { 15 | 16 | /** 17 | * 校验码,默认验证失败 18 | * 19 | * @return {@link Integer} 20 | */ 21 | int code() default 104; 22 | 23 | /** 24 | * 提示消息 25 | * 26 | * @return {@link String} 27 | */ 28 | String message() default "{} is required or incorrect format"; 29 | 30 | /** 31 | * 验证状态 32 | * 33 | * @return {@link String} 34 | */ 35 | String status() default ValueConsts.ERROR_EN; 36 | 37 | /** 38 | * 自定义验证表达式(用val代替字段值),支持正则匹配(使用英文冒号:开头),验证返回true时isPassed返回true,验证返回false时isPassed返回false 39 | * 40 | * @return {@link String} 41 | */ 42 | String expression() default ""; 43 | 44 | /** 45 | * 默认值(仅针对字段值为NULL时) 46 | * 47 | * @return {@link String} 48 | */ 49 | String defaultValue() default ""; 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/FastJsonTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package com.zhazhapan.util; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | import org.junit.Test; 9 | 10 | import com.zhazhapan.config.JsonParser; 11 | 12 | /** 13 | * @author pantao 14 | * 15 | */ 16 | public class FastJsonTest { 17 | 18 | String json = "{key1:test,key2:{child1:node1,child2:node2},key3:[{fast:json1},{fast:json2}],key4:[arr1,arr2]}"; 19 | 20 | @Test 21 | public void testGetString() { 22 | JsonParser jsonParser = new JsonParser(json, false); 23 | try { 24 | assertEquals(jsonParser.eval("key1"), jsonParser.getString("key1")); 25 | assertEquals(jsonParser.eval("key2.child1"), jsonParser.getString("key2.child1")); 26 | assertEquals(jsonParser.eval("key3[1].fast"), jsonParser.getString("key3[1].fast")); 27 | assertEquals(jsonParser.eval("key4[1]"), jsonParser.getString("key4[1]")); 28 | assertEquals(jsonParser.eval("key4"), jsonParser.getArray("key4")); 29 | assertEquals(jsonParser.eval("key3[0]"), jsonParser.getObject("key3[0]")); 30 | jsonParser.set("key1", "test_set_value"); 31 | assertEquals("test_set_value", jsonParser.eval("key1")); 32 | } catch (Exception e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/interfaces/MultipartFileService.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.interfaces; 2 | 3 | import com.zhazhapan.util.model.SimpleMultipartFile; 4 | 5 | /** 6 | * @author pantao 7 | * @since 2018/9/12 8 | **/ 9 | public interface MultipartFileService { 10 | 11 | /** 12 | * 检测当前 {@link SimpleMultipartFile}是否存在,如果文件不存在则写入磁盘,否则不写入。返回 NULL 时会尝试调用 {@link 13 | * #getBySimpleMultipartFile(SimpleMultipartFile)} 14 | * 15 | * @param simpleMultipartFile {@link SimpleMultipartFile} 16 | * 17 | * @return 是否存在 18 | * 19 | * @since 1.1.0 20 | */ 21 | default Boolean existsMultipartFile(SimpleMultipartFile simpleMultipartFile) { return null;} 22 | 23 | /** 24 | * 获取一条数据库记录,当返回值为 NULL时将文件写入磁盘,否则不写入 25 | * 26 | * @param simpleMultipartFile {@link SimpleMultipartFile} 27 | * 28 | * @return T 一个实体类 29 | * 30 | * @since 1.1.0 31 | */ 32 | default T getBySimpleMultipartFile(SimpleMultipartFile simpleMultipartFile) { return null;} 33 | 34 | /** 35 | * 定义将数据写入数据库中的方法 36 | * 37 | * @param simpleMultipartFile {@link SimpleMultipartFile} 38 | * 39 | * @return T 实体类 40 | * 41 | * @since 1.1.0 42 | */ 43 | default T saveEntity(SimpleMultipartFile simpleMultipartFile) {return null;} 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/office/MsUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.office; 2 | 3 | import com.zhazhapan.util.Checker; 4 | import com.zhazhapan.util.ReflectUtils; 5 | import org.apache.log4j.Logger; 6 | import org.apache.poi.POIXMLDocument; 7 | 8 | import java.io.FileOutputStream; 9 | import java.io.IOException; 10 | import java.io.OutputStream; 11 | import java.lang.reflect.InvocationTargetException; 12 | 13 | /** 14 | * @author pantao 15 | * @since 2018/2/28 16 | */ 17 | public class MsUtils { 18 | 19 | private static Logger logger = Logger.getLogger(MsUtils.class); 20 | 21 | private MsUtils() {} 22 | 23 | /** 24 | * 保存Office文档 25 | * 26 | * @param object {@link POIXMLDocument} 对象 27 | * @param path 输出路径 28 | * 29 | * @throws IOException 异常 30 | * @throws NoSuchMethodException 异常 31 | * @throws IllegalAccessException 异常 32 | * @throws InvocationTargetException 异常 33 | */ 34 | public static void writeTo(Object object, String path) throws IOException, NoSuchMethodException, 35 | IllegalAccessException, InvocationTargetException { 36 | OutputStream os = new FileOutputStream(path); 37 | ReflectUtils.invokeMethod(object, "write", new Class[]{OutputStream.class}, new Object[]{os}); 38 | os.close(); 39 | logger.info("文件已输出:" + path); 40 | } 41 | 42 | static Integer checkInteger(Integer integer) { 43 | return Checker.isNull(integer) ? 0 : integer; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/CalculatorTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package com.zhazhapan.util; 5 | 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | import org.junit.Test; 10 | 11 | /** 12 | * @author pantao 13 | * 14 | */ 15 | public class CalculatorTest { 16 | 17 | @Test 18 | public void testCalculator() { 19 | String for1 = "2+4-4+78-26"; 20 | String for2 = "2.56+4.33-4.445+78.1212-26.667"; 21 | String for3 = "1.2+2.3"; 22 | String for4 = "2.3*2"; 23 | String for5 = "1.2+2.3*3.45-56.2/43="; 24 | String for6 = "5(23+12)+(23-21)/2"; 25 | String for7 = "(23-9*7)/22+2(35-45+56/(12-9+4*(22/10-2)))"; 26 | Calculator.setPrecision(5); 27 | try { 28 | assertTrue(54 == Calculator.calculate(for1).doubleValue()); 29 | assertTrue(53.8992 == Calculator.calculate(for2).doubleValue()); 30 | assertTrue(3.5 == Calculator.calculate(for3).doubleValue()); 31 | assertTrue(4.6 == Calculator.calculate(for4).doubleValue()); 32 | assertTrue(7.82802 == Calculator.calculate(for5).doubleValue()); 33 | assertTrue(176 == Calculator.calculate(for6).doubleValue()); 34 | assertTrue(7.65550 == Calculator.calculate(for7).doubleValue()); 35 | } catch (Exception e) { 36 | e.printStackTrace(); 37 | } 38 | } 39 | 40 | @Test 41 | public void testCalculationFormula() { 42 | assertTrue(Calculator.isFormula("(23-9*7)/22+2(35-45+56/(12-9+4*(22/10-2)))")); 43 | assertTrue(Calculator.isFormula("(23+23)")); 44 | assertFalse(Calculator.isFormula("(23+4")); 45 | assertFalse(Calculator.isFormula("33+99)")); 46 | assertFalse(Calculator.isFormula("445+66+")); 47 | assertTrue(Calculator.isFormula("(12+33)/34*12/(66)=")); 48 | assertFalse(Calculator.isFormula("(23-9*7)/22+2(35-45+56/(12-9+4*(22/10-2))")); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/model/CheckResult.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.model; 2 | 3 | import com.zhazhapan.modules.constant.ValueConsts; 4 | 5 | /** 6 | * @author pantao 7 | * @since 2018/7/17 8 | */ 9 | public class CheckResult { 10 | 11 | private static final int DEFAULT_ERROR_CODE = 400; 12 | 13 | private static final String DEFAULT_ERROR_MESSAGE = "参数输入错误"; 14 | 15 | public boolean passed = true; 16 | 17 | public ResultObject resultObject = null; 18 | 19 | /** 20 | * 获取失败的结果 21 | * 22 | * @param code 错误码 23 | * @param message 消息 24 | * 25 | * @return {@link ResultObject} 26 | * 27 | * @since 1.1.0 28 | */ 29 | public static ResultObject getErrorResult(int code, String message) { 30 | return new ResultObject<>(code, message, ValueConsts.ERROR_EN); 31 | } 32 | 33 | /** 34 | * 获取失败的结果 35 | * 36 | * @return {@link ResultObject} 37 | * 38 | * @since 1.1.0 39 | */ 40 | public static ResultObject getErrorResult() { 41 | return getErrorResult(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MESSAGE); 42 | } 43 | 44 | /** 45 | * 获取失败的结果 46 | * 47 | * @param code 错误码 48 | * 49 | * @return {@link ResultObject} 50 | * 51 | * @since 1.1.0 52 | */ 53 | public static ResultObject getErrorResult(int code) { 54 | return getErrorResult(code, DEFAULT_ERROR_MESSAGE); 55 | } 56 | 57 | /** 58 | * 获取失败的结果 59 | * 60 | * @param message 消息 61 | * 62 | * @return {@link ResultObject} 63 | * 64 | * @since 1.1.0 65 | */ 66 | public static ResultObject getErrorResult(String message) { 67 | return getErrorResult(DEFAULT_ERROR_CODE, message); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/ReflectUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import org.junit.Test; 4 | 5 | import java.io.IOException; 6 | import java.lang.reflect.InvocationTargetException; 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | /** 12 | * @author pantao 13 | * @since 2018/1/20 14 | */ 15 | public class ReflectUtilsTest { 16 | 17 | public void setFontSize(Integer size) { 18 | System.out.println(size); 19 | } 20 | 21 | @Test 22 | public void testGetClass() throws IOException, ClassNotFoundException { 23 | List> list = ReflectUtils.getClasses("com.zhazhapan.util"); 24 | for (Class type : list) { 25 | System.out.println(type.getName()); 26 | } 27 | } 28 | 29 | @Test 30 | public void invokeMethod() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { 31 | System.out.println(ReflectUtils.invokeMethod(new ReflectUtilsTest(), "setFontSize", new Object[]{32})); 32 | } 33 | 34 | @Test 35 | public void executeExpression() { 36 | Map map = new HashMap<>(); 37 | map.put("alive", "coding every day"); 38 | map.put("out", System.out); 39 | String expression = "out.print(alive)"; 40 | ReflectUtils.executeExpression(expression, map); 41 | } 42 | 43 | @Test 44 | public void invokeMethodUseBasicType() { 45 | } 46 | 47 | @Test 48 | public void getBasicTypes() { 49 | } 50 | 51 | @Test 52 | public void getTypes() { 53 | } 54 | 55 | @Test 56 | public void scanPackage() { 57 | } 58 | 59 | @Test 60 | public void getClasses() { 61 | } 62 | 63 | @Test 64 | public void addClassesInPackageByFile() { 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/RandomUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.zhazhapan.modules.constant.ValueConsts; 4 | import org.junit.Test; 5 | 6 | public class RandomUtilsTest { 7 | 8 | @Test 9 | public void getRandomInteger() { 10 | System.out.println(RandomUtils.getRandomInteger(ValueConsts.NINE_INT)); 11 | } 12 | 13 | @Test 14 | public void getRandomUid() throws InterruptedException { 15 | for (int i = 0; i < 100; i++) { 16 | System.out.println(RandomUtils.getRandomUid()); 17 | Thread.sleep(1000); 18 | } 19 | } 20 | 21 | @Test 22 | public void getRandomEmail() { 23 | } 24 | 25 | @Test 26 | public void getRandomString() { 27 | } 28 | 29 | @Test 30 | public void getRandomStringWithoutSymbol() { 31 | } 32 | 33 | @Test 34 | public void getRandomStringOnlyLetter() { 35 | } 36 | 37 | @Test 38 | public void getRandomStringOnlyLowerCase() { 39 | } 40 | 41 | @Test 42 | public void getRandomStringOnlyUpperCase() { 43 | } 44 | 45 | @Test 46 | public void getRandomTextIgnoreRange() { 47 | } 48 | 49 | @Test 50 | public void getRandomText() { 51 | } 52 | 53 | @Test 54 | public void getRandomDouble() { 55 | } 56 | 57 | @Test 58 | public void getRandomDouble1() { 59 | } 60 | 61 | @Test 62 | public void getRandomDouble2() { 63 | } 64 | 65 | @Test 66 | public void getRandomIntegerIgnoreRange() { 67 | } 68 | 69 | @Test 70 | public void getRandomColor() { 71 | } 72 | 73 | @Test 74 | public void getRandomColor1() { 75 | } 76 | 77 | @Test 78 | public void getRandomNumber() { 79 | for (int i = 0; i < 100; i++) { 80 | assert Checker.isNumber(RandomUtils.getRandomNumber(11)); 81 | } 82 | } 83 | } -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/office/MsExcelUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.office; 2 | 3 | import com.zhazhapan.modules.constant.ValueConsts; 4 | import com.zhazhapan.util.Formatter; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.lang.reflect.InvocationTargetException; 9 | import java.util.Date; 10 | import java.util.List; 11 | 12 | public class MsExcelUtilsTest { 13 | 14 | private String path = ValueConsts.USER_DESKTOP + ValueConsts.SEPARATOR + "test.xlsx"; 15 | 16 | @Test 17 | public void appendTable() throws NoSuchMethodException, IOException, IllegalAccessException, 18 | InvocationTargetException { 19 | String[][] values = new String[][]{{"1", "小明", "男", Formatter.datetimeToString(new Date())}, {"2", "小明", "女", 20 | Formatter.datetimeToString(new Date())}}; 21 | MsExcelUtils.appendTable(values, null); 22 | MsExcelUtils.writeAndClose(path); 23 | } 24 | 25 | 26 | @Test 27 | public void createXssfWorkbook() { 28 | } 29 | 30 | @Test 31 | public void createXssfSheet() { 32 | } 33 | 34 | @Test 35 | public void readTable() throws IOException { 36 | MsExcelUtils.setXssfWorkbook(path); 37 | List> values = MsExcelUtils.readTable(ValueConsts.ZERO_INT); 38 | values.forEach((value) -> { 39 | value.forEach(val -> System.out.print(val + "\t")); 40 | System.out.println(); 41 | }); 42 | } 43 | 44 | @Test 45 | public void writeTo() { 46 | } 47 | 48 | @Test 49 | public void writeAndClose() { 50 | } 51 | 52 | @Test 53 | public void getXssfWorkbook() { 54 | } 55 | 56 | @Test 57 | public void setXssfWorkbook() { 58 | } 59 | 60 | @Test 61 | public void setXssfWorkbook1() { 62 | } 63 | 64 | @Test 65 | public void getXssfSheet() { 66 | } 67 | 68 | @Test 69 | public void setXssfSheet() { 70 | } 71 | } -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/decryption/SimpleDecrypt.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.decryption; 2 | 3 | import com.zhazhapan.util.CryptUtils; 4 | import com.zhazhapan.util.encryption.SimpleEncrypt; 5 | 6 | import java.io.IOException; 7 | 8 | /** 9 | * @author pantao 10 | */ 11 | public class SimpleDecrypt { 12 | 13 | private SimpleDecrypt() {} 14 | 15 | /** 16 | * 异或解密 17 | * 18 | * @param code {@link String} 19 | * @param key {@link Integer} 20 | * 21 | * @return {@link String} 22 | */ 23 | public static String xor(String code, int key) { 24 | return SimpleEncrypt.xor(code, key); 25 | } 26 | 27 | /** 28 | * ascii解密 29 | * 30 | * @param code {@link String} 31 | * @param key {@link Integer} 32 | * 33 | * @return {@link String} 34 | */ 35 | public static String ascii(String code, int key) { 36 | StringBuilder string = new StringBuilder(); 37 | int[] keys = CryptUtils.getKeys(key); 38 | for (int i = 0; i < code.length(); i++) { 39 | string.append((char) (code.charAt(i) - (i + keys[0]) % keys[1] % keys[2])); 40 | } 41 | return string.toString(); 42 | } 43 | 44 | /** 45 | * sscii解密 46 | * 47 | * @param code {@link String} 48 | * @param key {@link String} 49 | * 50 | * @return {@link String} 51 | */ 52 | public static String ascii(String code, String key) { 53 | return ascii(code, CryptUtils.stringToKey(key)); 54 | } 55 | 56 | /** 57 | * 混合解密 58 | * 59 | * @param code {@link String} 60 | * @param key {@link Integer} 61 | * 62 | * @return {@link String} 63 | * 64 | * @throws IOException 异常 65 | */ 66 | public static String mix(String code, int key) throws IOException { 67 | return xor(JavaDecrypt.base64(ascii(code, key)), key); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/model/SimpleMultipartFile.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.model; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | 5 | import java.io.File; 6 | 7 | /** 8 | * @author pantao 9 | * @since 2018/9/12 10 | **/ 11 | public class SimpleMultipartFile { 12 | 13 | private String filename; 14 | 15 | private String originalFilename; 16 | 17 | private String storagePath; 18 | 19 | private Long size; 20 | 21 | private String md5; 22 | 23 | private JSONObject values = new JSONObject(); 24 | 25 | public SimpleMultipartFile put(String key, Object value) { 26 | values.put(key, value); 27 | return this; 28 | } 29 | 30 | public JSONObject getValues() { 31 | return values; 32 | } 33 | 34 | public String getMd5() { 35 | return md5; 36 | } 37 | 38 | public SimpleMultipartFile setMd5(String md5) { 39 | this.md5 = md5; 40 | return this; 41 | } 42 | 43 | public Long getSize() { 44 | return size; 45 | } 46 | 47 | public SimpleMultipartFile setSize(Long size) { 48 | this.size = size; 49 | return this; 50 | } 51 | 52 | public String getStoragePath() { 53 | return storagePath; 54 | } 55 | 56 | public SimpleMultipartFile setStoragePath(String storagePath) { 57 | this.storagePath = storagePath + (storagePath.endsWith(File.separator) ? "" : File.separator); 58 | return this; 59 | } 60 | 61 | public String getOriginalFilename() { 62 | return originalFilename; 63 | } 64 | 65 | public SimpleMultipartFile setOriginalFilename(String originalFilename) { 66 | this.originalFilename = originalFilename; 67 | return this; 68 | } 69 | 70 | public String getFilename() { 71 | return filename; 72 | } 73 | 74 | public SimpleMultipartFile setFilename(String filename) { 75 | this.filename = filename; 76 | return this; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/AopLogUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.zhazhapan.util.annotation.AopLog; 4 | 5 | import java.lang.reflect.Method; 6 | 7 | /** 8 | * @author pantao 9 | * @since 2018/9/10 10 | **/ 11 | public class AopLogUtils { 12 | 13 | private AopLogUtils() {} 14 | 15 | /** 16 | * 获取 {@link AopLog}注解中的描述 17 | * 18 | * @param className 类名:joinPoint#getTarget#getClass#getName 19 | * @param methodName 方法名:joinPoint#getSignature#getName 20 | * @param args 参数数组:joinPoint#getArgs 21 | * 22 | * @return 描述 23 | * 24 | * @throws ClassNotFoundException 异常 25 | * @since 1.1.0 26 | */ 27 | public static String getDescription(String className, String methodName, Object[] args) throws ClassNotFoundException { 28 | Class targetClass = Class.forName(className); 29 | Method[] methods = targetClass.getMethods(); 30 | for (Method method : methods) { 31 | if (method.getName().equals(methodName)) { 32 | Class[] clazz = method.getParameterTypes(); 33 | if (clazz.length == args.length) { 34 | return method.getAnnotation(AopLog.class).value(); 35 | } 36 | } 37 | } 38 | return ""; 39 | } 40 | 41 | /** 42 | * 获取 {@link AopLog}注解中的描述 43 | * 44 | * @param className 类名:joinPoint#getTarget#getClass#getName 45 | * @param methodName 方法名:joinPoint#getSignature#getName 46 | * @param args 参数数组:joinPoint#getArgs 47 | * 48 | * @return 描述 49 | * 50 | * @since 1.1.0 51 | */ 52 | public static String getDescriptionNoThrow(String className, String methodName, Object[] args) { 53 | try { 54 | return getDescription(className, methodName, args); 55 | } catch (ClassNotFoundException e) { 56 | LoggerUtils.error("get description from {}#{} error: {}", className, methodName, e.getMessage()); 57 | return ""; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/encryption/SimpleEncrypt.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.encryption; 2 | 3 | import com.zhazhapan.util.CryptUtils; 4 | 5 | /** 6 | * @author pantao 7 | */ 8 | public class SimpleEncrypt { 9 | 10 | private SimpleEncrypt() {} 11 | 12 | /** 13 | * 混合加密 14 | * 15 | * @param string {@link String} 16 | * @param key {@link Integer} 17 | * 18 | * @return {@link String} 19 | */ 20 | public static String mix(String string, int key) { 21 | return ascii(JavaEncrypt.base64(xor(string, key)), key); 22 | } 23 | 24 | /** 25 | * 异或加密 26 | * 27 | * @param string {@link String} 28 | * @param key {@link Integer} 29 | * 30 | * @return {@link String} 31 | */ 32 | public static String xor(String string, int key) { 33 | char[] encrypt = string.toCharArray(); 34 | for (int i = 0; i < encrypt.length; i++) { 35 | encrypt[i] = (char) (encrypt[i] ^ key); 36 | } 37 | return new String(encrypt); 38 | } 39 | 40 | /** 41 | * 异或加密 42 | * 43 | * @param string {@link String} 44 | * @param key {@link String} 45 | * 46 | * @return {@link String} 47 | */ 48 | public static String xor(String string, String key) { 49 | return xor(string, CryptUtils.stringToKey(string)); 50 | } 51 | 52 | /** 53 | * ascii加密 54 | * 55 | * @param string {@link String} 56 | * @param key {@link Integer} 57 | * 58 | * @return {@link String} 59 | */ 60 | public static String ascii(String string, int key) { 61 | StringBuilder code = new StringBuilder(); 62 | int[] keys = CryptUtils.getKeys(key); 63 | for (int i = 0; i < string.length(); i++) { 64 | code.append((char) (string.charAt(i) + (i + keys[0]) % keys[1] % keys[2])); 65 | } 66 | return code.toString(); 67 | } 68 | 69 | /** 70 | * ascii加密 71 | * 72 | * @param string {@link String} 73 | * @param key {@link String} 74 | * 75 | * @return {@link String} 76 | */ 77 | public static String ascii(String string, String key) { 78 | return ascii(string, CryptUtils.stringToKey(key)); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/dialog/Dialogs.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.dialog; 2 | 3 | import javafx.geometry.Insets; 4 | import javafx.scene.control.ButtonBar.ButtonData; 5 | import javafx.scene.control.ButtonType; 6 | import javafx.scene.control.Dialog; 7 | import javafx.scene.control.TextInputDialog; 8 | import javafx.scene.layout.GridPane; 9 | import javafx.stage.Modality; 10 | 11 | import java.util.Optional; 12 | 13 | /** 14 | * @author pantao 15 | */ 16 | public class Dialogs { 17 | 18 | private static final String CANCEL_BUTTON_TEXT = "取消"; 19 | 20 | private Dialogs() {} 21 | 22 | /** 23 | * 弹出输入框 24 | * 25 | * @param title 标题 26 | * @param header 信息头 27 | * @param content 内容 28 | * @param defaultValue 输入框默认值 29 | * 30 | * @return 输入的内容 31 | */ 32 | public static String showInputDialog(String title, String header, String content, String defaultValue) { 33 | TextInputDialog dialog = new TextInputDialog(defaultValue); 34 | dialog.setTitle(title); 35 | dialog.setHeaderText(header); 36 | dialog.setContentText(content); 37 | 38 | Optional result = dialog.showAndWait(); 39 | return result.orElse(null); 40 | } 41 | 42 | /** 43 | * 获取一个{@link GridPane}对象 44 | * 45 | * @return {@link GridPane} 46 | */ 47 | public static GridPane getGridPane() { 48 | GridPane grid = new GridPane(); 49 | grid.setHgap(10); 50 | grid.setVgap(10); 51 | grid.setPadding(new Insets(10, 10, 10, 10)); 52 | return grid; 53 | } 54 | 55 | /** 56 | * 获得一个{@link Dialog}对象 57 | * 58 | * @param title 标题 59 | * @param ok 确认按钮 60 | * 61 | * @return {@link Dialog} 62 | */ 63 | public static Dialog getDialog(String title, ButtonType ok) { 64 | Dialog dialog = new Dialog<>(); 65 | dialog.setTitle(title); 66 | dialog.setHeaderText(null); 67 | 68 | dialog.initModality(Modality.APPLICATION_MODAL); 69 | 70 | // 自定义确认和取消按钮 71 | ButtonType cancel = new ButtonType(CANCEL_BUTTON_TEXT, ButtonData.CANCEL_CLOSE); 72 | dialog.getDialogPane().getButtonTypes().addAll(ok, cancel); 73 | return dialog; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/ListUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.LinkedList; 6 | import java.util.Vector; 7 | 8 | /** 9 | * @author pantao 10 | * @since 2018/7/24 11 | */ 12 | public class ListUtils { 13 | 14 | public static final int DEFAULT_LIST_CAPACITY = 10; 15 | 16 | private ListUtils() {} 17 | 18 | /** 19 | * 生成一个 {@link ArrayList} 20 | * 21 | * @param capacity 初始化长度 22 | * @param values 值数组 23 | * @param 值类型 24 | * 25 | * @return {@link ArrayList} 26 | * 27 | * @since 1.0.9 28 | */ 29 | @SafeVarargs 30 | public static ArrayList getArrayList(int capacity, T... values) { 31 | return new ArrayList(capacity) {{addAll(Arrays.asList(values));}}; 32 | } 33 | 34 | /** 35 | * 生成一个 {@link ArrayList} 36 | * 37 | * @param values 值数组 38 | * @param 值类型 39 | * 40 | * @return {@link ArrayList} 41 | * 42 | * @since 1.0.9 43 | */ 44 | public static ArrayList getArrayList(T... values) { 45 | return getArrayList(DEFAULT_LIST_CAPACITY, values); 46 | } 47 | 48 | /** 49 | * 生成一个 {@link LinkedList} 50 | * 51 | * @param values 值数组 52 | * @param 值类型 53 | * 54 | * @return {@link LinkedList} 55 | * 56 | * @since 1.0.9 57 | */ 58 | @SafeVarargs 59 | public static LinkedList getLinkedList(T... values) { 60 | return new LinkedList() {{addAll(Arrays.asList(values));}}; 61 | } 62 | 63 | /** 64 | * 生成一个 {@link Vector} 65 | * 66 | * @param values 值数组 67 | * @param capacity 初始化长度 68 | * @param 值类型 69 | * 70 | * @return {@link Vector} 71 | * 72 | * @since 1.0.9 73 | */ 74 | public static Vector getVector(int capacity, T... values) { 75 | return new Vector() {{addAll(Arrays.asList(values));}}; 76 | } 77 | 78 | /** 79 | * 生成一个 {@link Vector} 80 | * 81 | * @param values 值数组 82 | * @param 值类型 83 | * 84 | * @return {@link Vector} 85 | * 86 | * @since 1.0.9 87 | */ 88 | public static Vector getVector(T... values) { 89 | return getVector(DEFAULT_LIST_CAPACITY, values); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/CryptUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | /** 4 | * @author pantao 5 | */ 6 | public class CryptUtils { 7 | 8 | private static final int TEN = 10; 9 | 10 | private static final int HUNDRED = 100; 11 | 12 | private static final int THOUSAND = 1000; 13 | 14 | private CryptUtils() {} 15 | 16 | /** 17 | * 通过获取一个KEY数组 18 | * 19 | * @param key 可自定义一个INT型数值 20 | * 21 | * @return 长度为3的KEY数组 22 | * 23 | * @deprecated 无用类 24 | */ 25 | public static int[] getKeys(int key) { 26 | int[] keys = {0, 1, 1}; 27 | if (key > 0) { 28 | // 自定义算Key 29 | if (key < TEN) { 30 | keys[0] = key; 31 | keys[1] = key - key / 2; 32 | keys[2] = Math.abs(keys[1] - key % keys[1]); 33 | } else if (key < HUNDRED) { 34 | keys[0] = key % 10; 35 | keys[1] = Math.abs(key / 10 - keys[0]) % 9 + 1; 36 | keys[2] = keys[0] * keys[1] / (keys[0] + keys[1]) % 10; 37 | } else if (key < THOUSAND) { 38 | int middle = key % 100 / 10; 39 | keys[0] = key % 10; 40 | keys[1] = key / 100; 41 | keys[2] = ((int) (Math.pow(keys[1], keys[0]) / middle) % 9 + 1); 42 | } else { 43 | int length = String.valueOf(key).length(); 44 | keys[0] = key % 10; 45 | keys[1] = key / (int) Math.pow(10, length - 1); 46 | length = (keys[0] + keys[1] + keys[0] * keys[1]) % (length - 2) + 2; 47 | keys[2] = (int) (key % Math.pow(10, length) / Math.pow(10, length - 1)); 48 | } 49 | keys[2] = keys[2] % 8 + 2; 50 | } 51 | return keys; 52 | } 53 | 54 | /** 55 | * 通过字符串获取KEY 56 | * 57 | * @param string 可随机一个字符串 58 | * 59 | * @return INT型KEY 60 | */ 61 | public static int stringToKey(String string) { 62 | string = Checker.checkNull(string); 63 | int length = string.length(); 64 | if (length > THOUSAND) { 65 | return length; 66 | } 67 | int key = 0; 68 | for (int i = 0; i < length; i++) { 69 | key += (int) string.charAt(i); 70 | } 71 | return key; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/RandomTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.zhazhapan.modules.constant.ValueConsts; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author pantao 8 | */ 9 | public class RandomTest { 10 | 11 | @Test 12 | public void testRandomColor() { 13 | for (int i = 0; i < ValueConsts.ONE_HUNDRED_INT; i++) { 14 | System.out.println(RandomUtils.getRandomColor().toString()); 15 | } 16 | } 17 | 18 | @Test 19 | public void testRandomInteger() { 20 | for (int i = 0; i < ValueConsts.ONE_HUNDRED_INT; i++) { 21 | System.out.println(RandomUtils.getRandomInteger(0, 456)); 22 | } 23 | } 24 | 25 | @Test 26 | public void testRandomDouble() { 27 | for (int i = 0; i < ValueConsts.ONE_HUNDRED_INT; i++) { 28 | System.out.println(RandomUtils.getRandomDouble(0, 999.999, 7)); 29 | } 30 | } 31 | 32 | @Test 33 | public void testRandomString() { 34 | for (int i = 0; i < ValueConsts.ONE_HUNDRED_INT; i++) { 35 | System.out.println(RandomUtils.getRandomString(10)); 36 | } 37 | } 38 | 39 | @Test 40 | public void testRandomStringWithoutSymbol() { 41 | for (int i = 0; i < ValueConsts.ONE_HUNDRED_INT; i++) { 42 | System.out.println(RandomUtils.getRandomStringWithoutSymbol(10)); 43 | } 44 | } 45 | 46 | @Test 47 | public void testRandomStringOnlyLetter() { 48 | for (int i = 0; i < ValueConsts.ONE_HUNDRED_INT; i++) { 49 | System.out.println(RandomUtils.getRandomStringOnlyLetter(10)); 50 | } 51 | } 52 | 53 | @Test 54 | public void testRandomStringOnlyLowerCase() { 55 | for (int i = 0; i < ValueConsts.ONE_HUNDRED_INT; i++) { 56 | System.out.println(RandomUtils.getRandomStringOnlyLowerCase(10)); 57 | } 58 | } 59 | 60 | @Test 61 | public void testRandomStringOnlyUpperCase() { 62 | for (int i = 0; i < ValueConsts.ONE_HUNDRED_INT; i++) { 63 | System.out.println(RandomUtils.getRandomStringOnlyUpperCase(10)); 64 | } 65 | } 66 | 67 | @Test 68 | public void testRandomEmail() { 69 | for (int i = 0; i < ValueConsts.ONE_HUNDRED_INT; i++) { 70 | System.out.println(RandomUtils.getRandomEmail()); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/model/SimpleDateTime.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.model; 2 | 3 | import cn.hutool.core.date.DateField; 4 | import cn.hutool.core.date.DateTime; 5 | import cn.hutool.core.date.format.DateParser; 6 | 7 | import java.text.DateFormat; 8 | import java.util.Calendar; 9 | import java.util.Date; 10 | 11 | /** 12 | * @author pantao 13 | * @since 2018/9/24 14 | */ 15 | public class SimpleDateTime { 16 | 17 | private final String DATE_FORMAT = "yyyy-MM-dd"; 18 | 19 | private DateTime dateTime; 20 | 21 | private Integer year; 22 | 23 | private Integer month; 24 | 25 | private Integer day; 26 | 27 | private Integer hour; 28 | 29 | private Integer minute; 30 | 31 | private Integer second; 32 | 33 | public SimpleDateTime() { 34 | dateTime = new DateTime(); 35 | parse(); 36 | } 37 | 38 | public SimpleDateTime(Date date) { 39 | dateTime = new DateTime(date); 40 | parse(); 41 | } 42 | 43 | public SimpleDateTime(Calendar calendar) { 44 | dateTime = new DateTime(calendar); 45 | parse(); 46 | } 47 | 48 | public SimpleDateTime(long timeMillis) { 49 | dateTime = new DateTime(timeMillis); 50 | parse(); 51 | } 52 | 53 | public SimpleDateTime(String dateStr) { 54 | dateTime = new DateTime(dateStr, DATE_FORMAT); 55 | parse(); 56 | } 57 | 58 | public SimpleDateTime(String dateStr, String format) { 59 | dateTime = new DateTime(dateStr, format); 60 | parse(); 61 | } 62 | 63 | public SimpleDateTime(String dateStr, DateFormat dateFormat) { 64 | dateTime = new DateTime(dateStr, dateFormat); 65 | parse(); 66 | } 67 | 68 | public SimpleDateTime(String dateStr, DateParser dateParser) { 69 | dateTime = new DateTime(dateStr, dateParser); 70 | parse(); 71 | } 72 | 73 | public Integer getYear() { 74 | return year; 75 | } 76 | 77 | public Integer getMonth() { 78 | return month + 1; 79 | } 80 | 81 | public Integer getDay() { 82 | return day; 83 | } 84 | 85 | public Integer getHour() { 86 | return hour; 87 | } 88 | 89 | public Integer getMinute() { 90 | return minute; 91 | } 92 | 93 | public Integer getSecond() { 94 | return second; 95 | } 96 | 97 | private void parse() { 98 | year = dateTime.getField(DateField.YEAR); 99 | month = dateTime.getField(DateField.MONTH); 100 | day = dateTime.getField(DateField.DAY_OF_MONTH); 101 | hour = dateTime.getField(DateField.HOUR_OF_DAY); 102 | minute = dateTime.getField(DateField.MINUTE); 103 | second = dateTime.getField(DateField.SECOND); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/office/MsWordUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.office; 2 | 3 | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 4 | import org.apache.poi.xwpf.usermodel.ParagraphAlignment; 5 | import org.apache.poi.xwpf.usermodel.XWPFDocument; 6 | import org.apache.poi.xwpf.usermodel.XWPFRun; 7 | import org.junit.Test; 8 | 9 | import java.io.IOException; 10 | import java.lang.reflect.InvocationTargetException; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | 14 | /** 15 | * @author pantao 16 | * @since 2018/2/28 17 | */ 18 | public class MsWordUtilsTest { 19 | 20 | private final String path = "/Users/pantao/Desktop/test.docx"; 21 | 22 | public MsWordUtilsTest() throws IOException { 23 | MsWordUtils.setXwpfDocument(path); 24 | } 25 | 26 | @Test 27 | public void test() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { 28 | Map styles = new HashMap<>(); 29 | 30 | XWPFRun run = MsWordUtils.getNewRun(ParagraphAlignment.CENTER); 31 | styles.put("text", "市政府第25次常务会议议题报批单"); 32 | styles.put("bold", Boolean.TRUE); 33 | styles.put("fontFamily", "宋体"); 34 | styles.put("fontSize", 18); 35 | MsWordUtils.setStyle(run, styles); 36 | 37 | run = MsWordUtils.getNewRun(ParagraphAlignment.LEFT); 38 | styles.put("text", "时间:"); 39 | styles.put("bold", false); 40 | styles.put("fontSize", 12); 41 | MsWordUtils.setStyle(run, styles); 42 | 43 | MsWordUtils.writeAndClose(path); 44 | } 45 | 46 | 47 | @Test 48 | public void appendImage() throws IOException, InvalidFormatException, NoSuchMethodException, 49 | IllegalAccessException, InvocationTargetException { 50 | MsWordUtils.appendImage("/Users/pantao/Documents/images/portrait/avator.png", XWPFDocument.PICTURE_TYPE_PNG, 51 | 200, 200, ParagraphAlignment.CENTER); 52 | XWPFRun run = MsWordUtils.getNewRun(ParagraphAlignment.CENTER); 53 | run.setText("图1"); 54 | run.setBold(true); 55 | run.setColor("8d64e7"); 56 | MsWordUtils.writeAndClose(path); 57 | } 58 | 59 | @Test 60 | public void getNewRun() { 61 | } 62 | 63 | @Test 64 | public void getNewRunOfParagraph() { 65 | } 66 | 67 | @Test 68 | public void getRun() { 69 | } 70 | 71 | @Test 72 | public void getRunSize() { 73 | } 74 | 75 | @Test 76 | public void getParagraph() { 77 | } 78 | 79 | @Test 80 | public void getParagraphSize() { 81 | } 82 | 83 | @Test 84 | public void appendTable() throws IOException, NoSuchMethodException, IllegalAccessException, 85 | InvocationTargetException { 86 | MsWordUtils.createXwpfDocument(); 87 | String[][] values = new String[][]{{"编号", "姓名", "性别"}, {"1", "小明", "男"}, {"2", "小红", "女"}}; 88 | MsWordUtils.appendTable(ParagraphAlignment.CENTER, 3, 3, values, 200, 3000, null, null); 89 | MsWordUtils.writeAndClose(path); 90 | } 91 | } -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/UtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | import static org.junit.Assert.assertTrue; 7 | 8 | /** 9 | * @author pantao 10 | */ 11 | public class UtilsTest { 12 | 13 | private int[] test1 = {78, 56, 34, 23, 12, 1}; 14 | 15 | private int[] test2 = {45, 33, 32, 23, 22, 13}; 16 | 17 | private int[] test3 = {1, 2, 4, 6, 9}; 18 | 19 | private int[] test4 = {3, 5, 7, 8}; 20 | 21 | @Test 22 | public void testGetCurrentDate() { 23 | System.out.println(DateUtils.getCurrentMonth()); 24 | } 25 | 26 | @Test 27 | public void testNumberExtract() { 28 | String s1 = "+.12 3"; 29 | String s2 = "+ab12.c123 "; 30 | String s3 = "+0.c123"; 31 | String s4 = " +89h.7.123"; 32 | String s5 = "afda.afaa"; 33 | String s6 = "-.abca"; 34 | String s7 = "-.1 2-3"; 35 | String s8 = "-8afa232.123"; 36 | String s9 = "-0.123"; 37 | assertTrue(123 == Utils.extractInt(s1)); 38 | assertTrue(12123 == Utils.extractInt(s2)); 39 | assertTrue(0.123 == Utils.extractDouble(s3)); 40 | assertTrue(897.123 == Utils.extractDouble(s4)); 41 | assertTrue("".equals(Utils.extractDigit(s5))); 42 | assertTrue("".equals(Utils.extractDigit(s6))); 43 | assertTrue(-0.123 == Utils.extractDouble(s7)); 44 | assertTrue(-8232123 == Utils.extractInt(s8)); 45 | assertTrue(-0.123f == Utils.extractFloat(s9)); 46 | } 47 | 48 | @Test 49 | public void testConcatArrays() { 50 | int[] nums = ArrayUtils.concatArrays(test1, test2, test4, test3); 51 | for (int i : nums) { 52 | System.out.print(i + " "); 53 | } 54 | } 55 | 56 | @Test 57 | public void testGetMaxValue() { 58 | int[] test1 = {1, 2, 3, 2, 123, 34}; 59 | assertEquals(123, Utils.getMaxValue(test1)); 60 | } 61 | 62 | @Test 63 | public void trim() { 64 | } 65 | 66 | @Test 67 | public void leftTrim() { 68 | } 69 | 70 | @Test 71 | public void rightTrim() { 72 | } 73 | 74 | @Test 75 | public void getCurrentOS() { 76 | } 77 | 78 | @Test 79 | public void extractDouble() { 80 | } 81 | 82 | @Test 83 | public void extractFloat() { 84 | } 85 | 86 | @Test 87 | public void extractShort() { 88 | } 89 | 90 | @Test 91 | public void extractLong() { 92 | } 93 | 94 | @Test 95 | public void extractInt() { 96 | } 97 | 98 | @Test 99 | public void extractDigit() { 100 | } 101 | 102 | @Test 103 | public void maxLengthString() { 104 | } 105 | 106 | @Test 107 | public void copyToClipboard() { 108 | } 109 | 110 | @Test 111 | public void openLink() { 112 | } 113 | 114 | @Test 115 | public void openFile() { 116 | } 117 | 118 | @Test 119 | public void openFile1() { 120 | } 121 | 122 | @Test 123 | public void run() { 124 | } 125 | 126 | @Test 127 | public void getCurrentWorkDir() { 128 | System.out.println(Utils.getCurrentWorkDir()); 129 | } 130 | 131 | @Test 132 | public void getMaxValue() { 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/CommonsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.zhazhapan.util.annotation.ToJsonString; 5 | import com.zhazhapan.util.enums.JsonType; 6 | import com.zhazhapan.util.model.ResultObject; 7 | import com.zhazhapan.util.model.TestBean; 8 | import org.junit.Test; 9 | 10 | import java.lang.reflect.InvocationTargetException; 11 | import java.util.ArrayList; 12 | import java.util.HashMap; 13 | import java.util.List; 14 | import java.util.Map; 15 | 16 | /** 17 | * @author pantao 18 | * @since 2018/5/22 19 | */ 20 | public class CommonsTest { 21 | 22 | @Test 23 | public void testRefelect() throws NoSuchFieldException { 24 | assert TestBean.class.getField("username").getType() == String.class; 25 | assert TestBean.class.getField("age").getType() != String.class; 26 | } 27 | 28 | @Test 29 | public void testResultObject() { 30 | ResultObject resultObject = new ResultObject<>(); 31 | System.out.println(resultObject.setCode(200).setMessage("ok").setData("everything is ok")); 32 | } 33 | 34 | @Test 35 | public void testCastToBean() throws InvocationTargetException, IllegalAccessException { 36 | Person person = new Person("pan", 120); 37 | List> fingers = new ArrayList<>(); 38 | String[] f = {"one", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}; 39 | Map fingerMap = new HashMap<>(); 40 | Finger finger; 41 | for (String s : f) { 42 | finger = new Finger(RandomUtils.getRandomInteger(0, 100), RandomUtils.getRandomInteger(0, 100)); 43 | fingerMap.put(s, finger); 44 | } 45 | fingers.add(fingerMap); 46 | person.setFingers(fingers); 47 | JSONObject object = BeanUtils.beanToJson(person); 48 | System.out.println(object); 49 | System.out.println(JSONObject.toJavaObject(object, Person.class)); 50 | } 51 | } 52 | 53 | @ToJsonString(type = JsonType.PRETTY) 54 | class Person { 55 | 56 | private List> fingers; 57 | 58 | private String name; 59 | 60 | private int age; 61 | 62 | Person() {} 63 | 64 | Person(String name, int age) { 65 | this.name = name; 66 | this.age = age; 67 | } 68 | 69 | public List> getFingers() { 70 | return fingers; 71 | } 72 | 73 | public void setFingers(List> fingers) { 74 | this.fingers = fingers; 75 | } 76 | 77 | public String getName() { 78 | return name; 79 | } 80 | 81 | public void setName(String name) { 82 | this.name = name; 83 | } 84 | 85 | public int getAge() { 86 | return age; 87 | } 88 | 89 | public void setAge(int age) { 90 | this.age = age; 91 | } 92 | 93 | @Override 94 | public String toString() { 95 | try { 96 | return BeanUtils.toJsonStringByAnnotation(this); 97 | } catch (IllegalAccessException e) { 98 | e.printStackTrace(); 99 | } 100 | return ""; 101 | } 102 | } 103 | 104 | class Finger { 105 | 106 | private int height; 107 | 108 | private int width; 109 | 110 | public Finger() {} 111 | 112 | public Finger(int height, int width) { 113 | this.height = height; 114 | this.width = width; 115 | } 116 | 117 | public int getHeight() { 118 | return height; 119 | } 120 | 121 | public void setHeight(int height) { 122 | this.height = height; 123 | } 124 | 125 | public int getWidth() { 126 | return width; 127 | } 128 | 129 | public void setWidth(int width) { 130 | this.width = width; 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/decryption/JavaDecrypt.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.decryption; 2 | 3 | import com.zhazhapan.util.encryption.JavaEncrypt; 4 | import sun.misc.BASE64Decoder; 5 | 6 | import javax.crypto.BadPaddingException; 7 | import javax.crypto.IllegalBlockSizeException; 8 | import javax.crypto.NoSuchPaddingException; 9 | import java.io.IOException; 10 | import java.io.UnsupportedEncodingException; 11 | import java.security.InvalidKeyException; 12 | import java.security.NoSuchAlgorithmException; 13 | 14 | /** 15 | * @author pantao 16 | */ 17 | public class JavaDecrypt { 18 | 19 | private JavaDecrypt() {} 20 | 21 | /** 22 | * base64解密 23 | * 24 | * @param code {@link String} 25 | * 26 | * @return {@link String} 27 | * 28 | * @throws IOException 异常 29 | */ 30 | public static String base64(String code) throws IOException { 31 | return new String(new BASE64Decoder().decodeBuffer(code)); 32 | } 33 | 34 | /** 35 | * des解密 36 | * 37 | * @param code {@link String} 38 | * 39 | * @return {@link String} 40 | * 41 | * @throws InvalidKeyException 异常 42 | * @throws NoSuchAlgorithmException 异常 43 | * @throws NoSuchPaddingException 异常 44 | * @throws UnsupportedEncodingException 异常 45 | * @throws IllegalBlockSizeException 异常 46 | * @throws BadPaddingException 异常 47 | */ 48 | public static String des(String code) throws InvalidKeyException, NoSuchAlgorithmException, 49 | NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException { 50 | return JavaEncrypt.decryptDES(code, "DES"); 51 | } 52 | 53 | /** 54 | * des3解密 55 | * 56 | * @param code {@link String} 57 | * 58 | * @return {@link String} 59 | * 60 | * @throws InvalidKeyException 异常 61 | * @throws NoSuchAlgorithmException 异常 62 | * @throws NoSuchPaddingException 异常 63 | * @throws UnsupportedEncodingException 异常 64 | * @throws IllegalBlockSizeException 异常 65 | * @throws BadPaddingException 异常 66 | */ 67 | public static String des3(String code) throws InvalidKeyException, NoSuchAlgorithmException, 68 | NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException { 69 | return JavaEncrypt.decryptDES(code, "DESede"); 70 | } 71 | 72 | /** 73 | * aes解密 74 | * 75 | * @param code {@link String} 76 | * 77 | * @return {@link String} 78 | * 79 | * @throws InvalidKeyException 异常 80 | * @throws NoSuchAlgorithmException 异常 81 | * @throws NoSuchPaddingException 异常 82 | * @throws UnsupportedEncodingException 异常 83 | * @throws IllegalBlockSizeException 异常 84 | * @throws BadPaddingException 异常 85 | */ 86 | public static String aes(String code) throws InvalidKeyException, NoSuchAlgorithmException, 87 | NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException { 88 | return JavaEncrypt.decryptDES(code, "AES"); 89 | } 90 | 91 | /** 92 | * rsa解密 93 | * 94 | * @param code {@link String} 95 | * 96 | * @return {@link String} 97 | * 98 | * @throws InvalidKeyException 异常 99 | * @throws NoSuchAlgorithmException 异常 100 | * @throws NoSuchPaddingException 异常 101 | * @throws UnsupportedEncodingException 异常 102 | * @throws IllegalBlockSizeException 异常 103 | * @throws BadPaddingException 异常 104 | */ 105 | public static String rsa(String code) throws InvalidKeyException, NoSuchAlgorithmException, 106 | NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException { 107 | return JavaEncrypt.decryptDES(code, "RSA"); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/ArrayUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.Arrays; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | public class ArrayUtilsTest { 10 | 11 | /** 12 | * 实现复制数组,或者扩容数组 13 | */ 14 | @Test 15 | public void copyOf() { 16 | // 定义一个数据源数组 17 | Integer[] array = {1, 2, 3}; 18 | // 定义一个目标数组,用于测试 19 | Integer[] resultArray = {1, 2}; 20 | // 以前复制数组 21 | Integer[] array1 = new Integer[2]; 22 | System.arraycopy(array, 0, array1, 0, 2); 23 | assert Arrays.equals(resultArray, array1); 24 | // 现在复制数组,是不是比以前的跟简洁了,嘿嘿 25 | Integer[] array2 = ArrayUtils.copyOf(array, 2); 26 | assert Arrays.equals(array2, resultArray); 27 | } 28 | 29 | /** 30 | * 将 {@link Map} 中的 Value 转换成数组 31 | */ 32 | @Test 33 | public void mapToArray() { 34 | // 定义一个集合 35 | Map map = new HashMap<>(); 36 | map.put("monday", "nice day"); 37 | map.put("tuesday", "so bad"); 38 | // 定义一个结果数组,需要说明的是结果数组的顺序是和集合相反的 39 | String[] resultArray = {"so bad", "nice day"}; 40 | // 转换成数组 41 | String[] array = ArrayUtils.mapToArray(map, String.class); 42 | assert Arrays.equals(resultArray, array); 43 | } 44 | 45 | /** 46 | * 将 {@link Map}中的 Key 转换成数组 47 | */ 48 | @Test 49 | public void mapKeyToArray() { 50 | // 定义一个集合 51 | Map map = new HashMap<>(); 52 | map.put("monday", "nice day"); 53 | map.put("tuesday", "so bad"); 54 | // 定义一个结果数组,需要说明的是结果数组的顺序是和集合相反的 55 | String[] resultArray = {"tuesday", "monday"}; 56 | // 转换成数组 57 | String[] array = ArrayUtils.mapKeyToArray(map, String.class); 58 | assert Arrays.equals(resultArray, array); 59 | } 60 | 61 | @Test 62 | public void concatArrays() { 63 | } 64 | 65 | @Test 66 | public void concatArrays1() { 67 | } 68 | 69 | @Test 70 | public void concatArrays2() { 71 | } 72 | 73 | @Test 74 | public void concatArrays3() { 75 | } 76 | 77 | @Test 78 | public void concatArrays4() { 79 | } 80 | 81 | @Test 82 | public void concatArrays5() { 83 | } 84 | 85 | @Test 86 | public void concatArrays6() { 87 | } 88 | 89 | @Test 90 | public void concatArrays7() { 91 | } 92 | 93 | @Test 94 | public void concatArrays8() { 95 | } 96 | 97 | @Test 98 | public void concatArrays9() { 99 | } 100 | 101 | /** 102 | * 合并两个同序的数组 103 | */ 104 | @Test 105 | public void mergeSortedArrays() { 106 | // 定义一个升序数组 107 | int[] array1 = {1, 3, 6, 9}; 108 | // 再定义一个升序数组,嘿嘿 109 | int[] array2 = {2, 3, 7, 8}; 110 | // 定义一个结果数组 111 | int[] resultArray = {1, 2, 3, 6, 7, 8, 9}; 112 | // 合并两个同序数组,这里我们忽略相等的数字 113 | int[] array = ArrayUtils.mergeSortedArrays(array1, array2, true, false); 114 | assert Arrays.equals(resultArray, array); 115 | } 116 | 117 | @Test 118 | public void mergeSortedArrays1() { 119 | } 120 | 121 | @Test 122 | public void unique() { 123 | } 124 | 125 | @Test 126 | public void unique1() { 127 | } 128 | 129 | @Test 130 | public void unique2() { 131 | } 132 | 133 | @Test 134 | public void unique3() { 135 | } 136 | 137 | @Test 138 | public void unique4() { 139 | } 140 | 141 | @Test 142 | public void heapSort() { 143 | } 144 | 145 | @Test 146 | public void mergeSort() { 147 | } 148 | 149 | @Test 150 | public void shellSort() { 151 | } 152 | 153 | @Test 154 | public void selectSort() { 155 | } 156 | 157 | @Test 158 | public void quickSort() { 159 | } 160 | 161 | @Test 162 | public void insertSort() { 163 | } 164 | 165 | @Test 166 | public void bubbleSort() { 167 | } 168 | } -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/BeanUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.zhazhapan.modules.constant.ValueConsts; 4 | import com.zhazhapan.util.annotation.ToJsonString; 5 | import com.zhazhapan.util.common.interceptor.ToStringMethodInterceptor; 6 | import com.zhazhapan.util.enums.FieldModifier; 7 | import com.zhazhapan.util.enums.JsonMethod; 8 | import com.zhazhapan.util.enums.JsonType; 9 | import com.zhazhapan.util.model.A; 10 | import com.zhazhapan.util.model.B; 11 | import net.sf.cglib.proxy.Enhancer; 12 | import org.junit.Assert; 13 | import org.junit.Test; 14 | 15 | import java.io.File; 16 | import java.io.IOException; 17 | import java.io.Serializable; 18 | import java.lang.reflect.InvocationTargetException; 19 | import java.util.Date; 20 | 21 | /** 22 | * @author pantao 23 | * @since 2018/1/18 24 | */ 25 | public class BeanUtilsTest { 26 | 27 | private String file = ValueConsts.USER_DESKTOP + File.separator + "user.obj"; 28 | 29 | @Test 30 | public void testToJsonString() throws IllegalAccessException { 31 | User user = new User(1, "test", new Date()); 32 | System.out.println(user.toString()); 33 | System.out.println(new User().toString()); 34 | System.out.println(BeanUtils.toJsonStringByAnnotation(user)); 35 | 36 | //测试cglib代理 37 | Enhancer enhancer = new Enhancer(); 38 | enhancer.setSuperclass(User.class); 39 | enhancer.setCallback(new ToStringMethodInterceptor()); 40 | User hello = (User) enhancer.create(); 41 | System.out.println(hello.hashCode()); 42 | } 43 | 44 | @Test 45 | public void deserialize() throws IOException, ClassNotFoundException { 46 | User user = BeanUtils.deserialize(file, User.class); 47 | System.out.println(user); 48 | } 49 | 50 | @Test 51 | public void serialize() throws Exception { 52 | User user = new User(2, "serializable", new Date()); 53 | BeanUtils.serialize(user, file); 54 | } 55 | 56 | @Test 57 | public void beanToJson() { 58 | } 59 | 60 | @Test 61 | public void jsonPutIn() { 62 | } 63 | 64 | @Test 65 | public void toPrettyJson() { 66 | } 67 | 68 | @Test 69 | public void toPrettyJson1() { 70 | } 71 | 72 | @Test 73 | public void toJsonString() { 74 | } 75 | 76 | @Test 77 | public void toJsonString1() { 78 | } 79 | 80 | @Test 81 | public void toJsonString2() { 82 | } 83 | 84 | @Test 85 | public void toJsonStringByAnnotation() { 86 | } 87 | 88 | @Test 89 | public void bean2Another() throws InvocationTargetException, NoSuchMethodException, InstantiationException, 90 | IllegalAccessException { 91 | B b = new B(); 92 | b.setAge(102); 93 | b.setBio("test"); 94 | A a = BeanUtils.bean2Another(b, A.class); 95 | Assert.assertNotNull(a); 96 | System.out.println(a); 97 | a.setGender("男"); 98 | a.setName("pantao"); 99 | b.setAge(24); 100 | A anotherA = null; 101 | a = BeanUtils.bean2Another(b, a); 102 | Assert.assertNotNull(a); 103 | System.out.println(a); 104 | anotherA = BeanUtils.bean2Another(a, anotherA, A.class); 105 | Assert.assertNotNull(anotherA); 106 | System.out.println(anotherA); 107 | } 108 | 109 | @Test 110 | public void toJsonObject() { 111 | } 112 | } 113 | 114 | @ToJsonString(type = JsonType.PRETTY, modifier = FieldModifier.PRIVATE, method = JsonMethod.MANUAL) 115 | class User implements Serializable { 116 | 117 | public int id; 118 | 119 | private String name; 120 | 121 | private Date birth; 122 | 123 | public User() {} 124 | 125 | public User(int id, String name, Date birth) { 126 | this.id = id; 127 | this.name = name; 128 | this.birth = birth; 129 | } 130 | 131 | @Override 132 | public String toString() { 133 | try { 134 | return BeanUtils.toJsonString(this, FieldModifier.ALL); 135 | } catch (IllegalAccessException e) { 136 | return e.getMessage(); 137 | } 138 | } 139 | } -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/FormatterTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.HashMap; 6 | import java.util.Locale; 7 | import java.util.Map; 8 | 9 | public class FormatterTest { 10 | 11 | @Test 12 | public void testToFinancialCharacter() { 13 | assert "壹万陆仟肆佰零玖元零贰分".equals(Formatter.toFinancialCharacter(16409.02)); 14 | assert "壹仟肆佰零玖元伍角".equals(Formatter.toFinancialCharacter(1409.50)); 15 | assert "陆仟零柒元壹角肆分".equals(Formatter.toFinancialCharacter(6007.14)); 16 | assert "壹仟陆佰捌拾元叁角贰分".equals(Formatter.toFinancialCharacter(1680.32)); 17 | assert "叁佰贰拾伍元零肆分".equals(Formatter.toFinancialCharacter(325.04)); 18 | assert "肆仟叁佰贰拾壹元整".equals(Formatter.toFinancialCharacter(4321.00)); 19 | assert "壹分".equals(Formatter.toFinancialCharacter(0.01)); 20 | 21 | assert Formatter.toFinancialCharacter(123456789012.34).equals("壹仟贰佰叁拾肆亿伍仟陆佰柒拾捌万玖仟零壹拾贰元叁角肆分"); 22 | assert Formatter.toFinancialCharacter(100010001000.10).equals("壹仟亿零壹仟万零壹仟元壹角"); 23 | assert Formatter.toFinancialCharacter(900990099009.99).equals("玖仟零玖亿玖仟零玖万玖仟零玖元玖角玖分"); 24 | assert Formatter.toFinancialCharacter(543200010001.01).equals("伍仟肆佰叁拾贰亿零壹万零壹元零壹分"); 25 | assert Formatter.toFinancialCharacter(100000001110.00).equals("壹仟亿零壹仟壹佰壹拾元整"); 26 | assert Formatter.toFinancialCharacter(101000000001.11).equals("壹仟零壹拾亿零壹元壹角壹分"); 27 | assert Formatter.toFinancialCharacter(100000000000.01).equals("壹仟亿元零壹分"); 28 | } 29 | 30 | @Test 31 | public void toFinancialCharacter() { 32 | } 33 | 34 | @Test 35 | public void toCurrency() { 36 | System.out.println(Formatter.toCurrency(Locale.CANADA, 233.23)); 37 | System.out.println(Formatter.toCurrency(566)); 38 | } 39 | 40 | @Test 41 | public void toCurrency1() { 42 | } 43 | 44 | @Test 45 | public void stringToInt() { 46 | } 47 | 48 | @Test 49 | public void formatSize() { 50 | } 51 | 52 | @Test 53 | public void sizeToLong() { 54 | } 55 | 56 | @Test 57 | public void stringToDouble() { 58 | } 59 | 60 | @Test 61 | public void stringToLong() { 62 | } 63 | 64 | @Test 65 | public void customFormatDecimal() { 66 | } 67 | 68 | @Test 69 | public void formatDecimal() { 70 | } 71 | 72 | @Test 73 | public void formatDecimal1() { 74 | } 75 | 76 | @Test 77 | public void timeStampToString() { 78 | } 79 | 80 | @Test 81 | public void mapToJson() { 82 | Map map = new HashMap<>(); 83 | map.put("key", "value"); 84 | map.put("map", "json"); 85 | System.out.println(Formatter.mapToJson(map)); 86 | } 87 | 88 | @Test 89 | public void listToJson() { 90 | } 91 | 92 | @Test 93 | public void formatJson() { 94 | } 95 | 96 | @Test 97 | public void dateToString() { 98 | } 99 | 100 | @Test 101 | public void datetimeToString() { 102 | } 103 | 104 | @Test 105 | public void getFileName() { 106 | } 107 | 108 | @Test 109 | public void stringToFloat() { 110 | } 111 | 112 | @Test 113 | public void stringToInteger() { 114 | } 115 | 116 | @Test 117 | public void stringToShort() { 118 | } 119 | 120 | @Test 121 | public void stringToDate() { 122 | } 123 | 124 | @Test 125 | public void stringToLongTime() { 126 | } 127 | 128 | @Test 129 | public void stringToShortTime() { 130 | } 131 | 132 | @Test 133 | public void stringToCustomDateTime() { 134 | } 135 | 136 | @Test 137 | public void stringToDatetime() { 138 | } 139 | 140 | @Test 141 | public void stringToCustomDateTime1() { 142 | } 143 | 144 | @Test 145 | public void toLocalDate() { 146 | } 147 | 148 | @Test 149 | public void longTimeToString() { 150 | } 151 | 152 | @Test 153 | public void shortTimeToString() { 154 | } 155 | 156 | @Test 157 | public void datetimeToCustomString() { 158 | } 159 | 160 | @Test 161 | public void datetimeToCustomString1() { 162 | } 163 | 164 | @Test 165 | public void numberFormat() { 166 | } 167 | 168 | @Test 169 | public void localDateToDate() { 170 | } 171 | 172 | @Test 173 | public void dateToLocalDate() { 174 | } 175 | 176 | @Test 177 | public void mapToJsonArray() { 178 | } 179 | } -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/web/BaseController.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.web; 2 | 3 | import com.zhazhapan.util.Checker; 4 | import com.zhazhapan.util.LoggerUtils; 5 | import com.zhazhapan.util.annotation.SensitiveData; 6 | import com.zhazhapan.util.model.CheckResult; 7 | import com.zhazhapan.util.model.ResultObject; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | import java.lang.reflect.Field; 11 | import java.util.List; 12 | 13 | /** 14 | * @author pantao 15 | * @since 2018/9/18 16 | */ 17 | public class BaseController { 18 | 19 | private HttpServletRequest request; 20 | 21 | private boolean checkSensitiveData = false; 22 | 23 | public BaseController() {} 24 | 25 | public BaseController(boolean checkSensitiveData) { 26 | this.checkSensitiveData = checkSensitiveData; 27 | } 28 | 29 | public BaseController(HttpServletRequest request) { 30 | this.request = request; 31 | } 32 | 33 | public BaseController(HttpServletRequest request, boolean checkSensitiveData) { 34 | this.request = request; 35 | this.checkSensitiveData = checkSensitiveData; 36 | } 37 | 38 | protected String getToken() { 39 | String token = request.getHeader("token"); 40 | if (Checker.isEmpty(token)) { 41 | token = request.getParameter("token"); 42 | } 43 | return token; 44 | } 45 | 46 | protected ResultObject parseBooleanResult(String okMsg, String errMsg, boolean isOk) { 47 | ResultObject resultObject = new ResultObject<>(isOk); 48 | return isOk ? resultObject.setMessage(okMsg) : resultObject.setMessage(errMsg); 49 | } 50 | 51 | protected ResultObject parseResult(boolean isOk) { 52 | return parseResult("", isOk); 53 | } 54 | 55 | protected ResultObject parseResult(String prefix, boolean isOk) { 56 | if (Checker.isEmpty(prefix)) { 57 | prefix = "操作"; 58 | } 59 | String msg = prefix + (isOk ? "成功" : "失败"); 60 | return parseResult(msg, msg, isOk); 61 | } 62 | 63 | protected ResultObject parseResult(String okMsg, String errMsg, boolean isOk) { 64 | return isOk ? new ResultObject(okMsg) : CheckResult.getErrorResult(errMsg); 65 | } 66 | 67 | protected ResultObject> parseResult(String errMsg, List list) { 68 | return parseResult(errMsg, list, checkSensitiveData); 69 | } 70 | 71 | protected ResultObject> parseResult(String errMsg, List list, boolean checkSensitiveData) { 72 | if (Checker.isEmpty(list)) { 73 | return CheckResult.getErrorResult(errMsg); 74 | } 75 | if (checkSensitiveData) { 76 | list.forEach(this::setSensitiveData); 77 | } 78 | return new ResultObject<>(list); 79 | } 80 | 81 | protected ResultObject parseResult(String okMsg, String errMsg, T t) { 82 | return parseResult(okMsg, errMsg, t, checkSensitiveData); 83 | } 84 | 85 | protected ResultObject parseResult(String okMsg, String errMsg, T t, boolean checkSensitiveData) { 86 | if (Checker.isNull(t)) { 87 | return CheckResult.getErrorResult(errMsg); 88 | } 89 | if (checkSensitiveData) { 90 | setSensitiveData(t); 91 | } 92 | return new ResultObject<>(okMsg, t); 93 | } 94 | 95 | protected void setSensitiveData(T t) { 96 | if (Checker.isNotNull(t)) { 97 | Field[] fields = t.getClass().getDeclaredFields(); 98 | final String sensitiveDataTip = "******"; 99 | try { 100 | for (Field field : fields) { 101 | SensitiveData sensitiveData = field.getAnnotation(SensitiveData.class); 102 | if (Checker.isNotNull(sensitiveData) && field.getType() == String.class) { 103 | field.setAccessible(true); 104 | field.set(t, sensitiveDataTip); 105 | } 106 | } 107 | } catch (IllegalAccessException e) { 108 | LoggerUtils.error("set sensitive data error: {}", e.getMessage()); 109 | } 110 | } 111 | } 112 | 113 | protected ResultObject parseResult(String errMsg, T t) { 114 | return parseResult(errMsg, t, checkSensitiveData); 115 | } 116 | 117 | protected ResultObject parseResult(String errMsg, T t, boolean checkSensitiveData) { 118 | return parseResult("操作成功", errMsg, t, checkSensitiveData); 119 | } 120 | 121 | protected ResultObject parseResult(T t) { 122 | return parseResult("操作失败", t); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/NetUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.xml.parsers.ParserConfigurationException; 6 | import javax.xml.xpath.XPathExpressionException; 7 | import java.io.IOException; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | public class NetUtilsTest { 12 | 13 | @Test 14 | public void getComputerName() { 15 | System.out.println(NetUtils.getComputerName()); 16 | } 17 | 18 | @Test 19 | public void evaluate() throws XPathExpressionException, IOException, ParserConfigurationException { 20 | String html = NetUtils.getHtmlFromUrl("http://ip.chinaz.com/125.69.41.90"); 21 | System.out.println(NetUtils.evaluate("//div[@class='WhoIpWrap jspu']/p[2]/span[4]", html)); 22 | } 23 | 24 | @Test 25 | public void getSystemName() { 26 | } 27 | 28 | @Test 29 | public void getSystemArch() { 30 | } 31 | 32 | @Test 33 | public void getSystemVersion() { 34 | } 35 | 36 | @Test 37 | public void getMacAddress() { 38 | } 39 | 40 | @Test 41 | public void getPublicIpAndLocation() { 42 | } 43 | 44 | @Test 45 | public void getLocalIp() { 46 | } 47 | 48 | @Test 49 | public void urlToString() { 50 | } 51 | 52 | @Test 53 | public void getDataOfUrl() { 54 | } 55 | 56 | @Test 57 | public void getDataOfUrl1() { 58 | } 59 | 60 | @Test 61 | public void getInputStreamOfUrl() { 62 | } 63 | 64 | @Test 65 | public void getInputStreamOfUrl1() { 66 | } 67 | 68 | @Test 69 | public void getInputStreamOfConnection() { 70 | } 71 | 72 | @Test 73 | public void whois() throws ParserConfigurationException, XPathExpressionException, IOException { 74 | System.out.println(NetUtils.whois("zhazhapan.com")); 75 | } 76 | 77 | @Test 78 | public void getLocationByIp() { 79 | } 80 | 81 | @Test 82 | public void getHtmlFromUrl() { 83 | } 84 | 85 | @Test 86 | public void getDocumentFromUrl() { 87 | } 88 | 89 | @Test 90 | public void parseUrl() { 91 | System.out.println(NetUtils.parseUrl("http://127.0.0.1:8080/heart/api/date" + ".html?love-you=forever&coding" + "=everyday")); 92 | System.out.println(NetUtils.parseUrl("http://")); 93 | System.out.println(NetUtils.parseUrl("http")); 94 | System.out.println(NetUtils.parseUrl("https://zhazhapan.com")); 95 | System.out.println(NetUtils.parseUrl("https://github.zhazhapan.com/")); 96 | } 97 | 98 | @Test 99 | public void scriptFilter() { 100 | } 101 | 102 | @Test 103 | public void clearCookie() { 104 | } 105 | 106 | @Test 107 | public void clearCookie1() { 108 | } 109 | 110 | @Test 111 | public void removeCookie() { 112 | } 113 | 114 | @Test 115 | public void removeCookie1() { 116 | } 117 | 118 | @Test 119 | public void removeCookie2() { 120 | } 121 | 122 | @Test 123 | public void addCookie() { 124 | } 125 | 126 | @Test 127 | public void addCookie1() { 128 | } 129 | 130 | @Test 131 | public void addCookie2() { 132 | } 133 | 134 | @Test 135 | public void getCookie() { 136 | } 137 | 138 | @Test 139 | public void getCookie1() { 140 | } 141 | 142 | @Test 143 | public void post() { 144 | Map map = new HashMap<>(); 145 | map.put("email", "tao@zhazhapan.com"); 146 | map.put("method", "1"); 147 | System.out.println(NetUtils.post("http://localhost:49986/common/code", map)); 148 | } 149 | 150 | @Test 151 | public void isAjax() { 152 | } 153 | 154 | @Test 155 | public void generateToken() { 156 | for (int i = 0; i < 100; i++) { 157 | System.out.println(NetUtils.generateToken()); 158 | } 159 | } 160 | 161 | @Test 162 | public void upload() { 163 | } 164 | 165 | @Test 166 | public void delete() { 167 | } 168 | 169 | @Test 170 | public void delete1() { 171 | } 172 | 173 | @Test 174 | public void delete2() { 175 | } 176 | 177 | @Test 178 | public void put() { 179 | } 180 | 181 | @Test 182 | public void put1() { 183 | } 184 | 185 | @Test 186 | public void put2() { 187 | } 188 | 189 | @Test 190 | public void get() { 191 | } 192 | 193 | @Test 194 | public void get1() { 195 | } 196 | 197 | @Test 198 | public void get2() { 199 | } 200 | 201 | @Test 202 | public void request() { 203 | } 204 | 205 | @Test 206 | public void request1() { 207 | } 208 | 209 | @Test 210 | public void replaceLocalHost() { 211 | } 212 | } -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/Downloader.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.zhazhapan.modules.constant.ValueConsts; 4 | import org.apache.log4j.Logger; 5 | 6 | import java.io.File; 7 | import java.io.FileOutputStream; 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.net.HttpURLConnection; 11 | import java.net.URL; 12 | 13 | /** 14 | * @author pantao 15 | */ 16 | public class Downloader { 17 | 18 | private static Logger logger = Logger.getLogger(Downloader.class); 19 | 20 | /** 21 | * 默认下载目录 22 | */ 23 | private static String storageFolder = ValueConsts.USER_HOME + ValueConsts.SEPARATOR + "util"; 24 | 25 | private Downloader() {} 26 | 27 | /** 28 | * 下载 29 | * 30 | * @param storageFolder 下载到指定目录 31 | * @param downloadURL 下载的URL 32 | * 33 | * @return 是否下载成功 34 | */ 35 | public static boolean download(String storageFolder, String downloadURL) { 36 | Downloader.storageFolder = storageFolder; 37 | return download(downloadURL); 38 | } 39 | 40 | /** 41 | * 下载文件 42 | * 43 | * @param downloadURL 下载的URL 44 | * 45 | * @return 是否下载成功 46 | */ 47 | @SuppressWarnings("ResultOfMethodCallIgnored") 48 | public static boolean download(String downloadURL) { 49 | if (Checker.isHyperLink(downloadURL) && checkDownloadPath()) { 50 | logger.info("ready for download url: " + downloadURL + " storage in " + storageFolder); 51 | } else { 52 | logger.info("url or storage path are invalidated, can't download"); 53 | return false; 54 | } 55 | int byteRead; 56 | String log = "download success from url '" + downloadURL + "' to local '"; 57 | try { 58 | String fileName = checkPath(storageFolder + ValueConsts.SEPARATOR + Formatter.getFileName(downloadURL)); 59 | String tmp = fileName + ".tmp"; 60 | File file = new File(tmp); 61 | log += file.getAbsolutePath() + "'"; 62 | URL url = new URL(downloadURL); 63 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 64 | InputStream inStream = NetUtils.getInputStreamOfConnection(conn); 65 | if (conn.getResponseCode() != ValueConsts.RESPONSE_OK || conn.getContentLength() <= 0) { 66 | return false; 67 | } 68 | FileOutputStream fs = new FileOutputStream(file); 69 | if (!file.exists()) { 70 | file.createNewFile(); 71 | } 72 | byte[] buffer = new byte[1024]; 73 | while ((byteRead = inStream.read(buffer)) != -1) { 74 | fs.write(buffer, 0, byteRead); 75 | } 76 | fs.flush(); 77 | inStream.close(); 78 | fs.close(); 79 | file.renameTo(new File(fileName)); 80 | logger.info(log); 81 | return true; 82 | } catch (IOException e) { 83 | log = log.replace("success", "error") + ", message: " + e.getMessage(); 84 | logger.error(log); 85 | return false; 86 | } 87 | } 88 | 89 | /** 90 | * 检查文件路径是否存在 91 | * 92 | * @param path 文件路径 93 | * 94 | * @return 文件的绝对路径,如果文件存在,则生成一个新路径 95 | */ 96 | private static String checkPath(String path) { 97 | File file = new File(path); 98 | int idx = path.lastIndexOf("."); 99 | String post = ""; 100 | if (idx > -1) { 101 | post = path.substring(idx); 102 | path = path.substring(0, idx); 103 | } 104 | int i = 0; 105 | while (file.exists()) { 106 | file = new File(path + "_" + (++i) + post); 107 | } 108 | return file.getAbsolutePath(); 109 | } 110 | 111 | /** 112 | * 检测下载目录是否存在,不存在则创建 113 | * 114 | * @return {@link Boolean} 115 | */ 116 | private static boolean checkDownloadPath() { 117 | if (Checker.isNotEmpty(storageFolder)) { 118 | File file = new File(storageFolder); 119 | if (!file.exists() && file.mkdirs()) { 120 | logger.info("mkdir '" + storageFolder + "' success"); 121 | } 122 | return true; 123 | } 124 | return false; 125 | } 126 | 127 | /** 128 | * 获取下载目录 129 | * 130 | * @return {@link String} 131 | */ 132 | public static String getStorageFolder() { 133 | return storageFolder; 134 | } 135 | 136 | /** 137 | * 设置下载的目录 138 | * 139 | * @param storageFolder 下载目录 140 | */ 141 | public static void setStorageFolder(String storageFolder) { 142 | Downloader.storageFolder = storageFolder; 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/model/ResultObject.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.model; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.zhazhapan.modules.constant.ValueConsts; 5 | 6 | import java.sql.Timestamp; 7 | 8 | /** 9 | * @author pantao 10 | * @since 2018/7/17 11 | */ 12 | public class ResultObject { 13 | 14 | /** 15 | * 校验码 16 | */ 17 | public int code = 200; 18 | 19 | /** 20 | * 提示消息 21 | */ 22 | public String message = "验证通过"; 23 | 24 | /** 25 | * 状态 26 | */ 27 | public String status = ValueConsts.SUCCESS; 28 | 29 | /** 30 | * 自定义Data 31 | */ 32 | public T data = null; 33 | 34 | public Timestamp timestamp = new Timestamp(System.currentTimeMillis()); 35 | 36 | /** 37 | * 无参构造 38 | */ 39 | public ResultObject() {} 40 | 41 | /** 42 | * 构造方法 43 | * 44 | * @param code 返回码 45 | */ 46 | public ResultObject(int code) { 47 | this.code = code; 48 | } 49 | 50 | /** 51 | * 构造方法 52 | * 53 | * @param message 消息 54 | */ 55 | public ResultObject(String message) { 56 | this.message = message; 57 | } 58 | 59 | /** 60 | * 构造方法 61 | * 62 | * @param data 数据对象 63 | */ 64 | public ResultObject(T data) { 65 | this.data = data; 66 | } 67 | 68 | /** 69 | * 构造方法 70 | * 71 | * @param code 返回码 72 | * @param message 消息 73 | */ 74 | public ResultObject(int code, String message) { 75 | this.code = code; 76 | this.message = message; 77 | } 78 | 79 | /** 80 | * 构造方法 81 | * 82 | * @param code 返回码 83 | * @param data 数据对象 84 | */ 85 | public ResultObject(int code, T data) { 86 | this.code = code; 87 | this.data = data; 88 | } 89 | 90 | /** 91 | * 构造方法 92 | * 93 | * @param message 消息 94 | * @param data 数据对象 95 | */ 96 | public ResultObject(String message, T data) { 97 | this.message = message; 98 | this.data = data; 99 | } 100 | 101 | /** 102 | * /** 构造方法 103 | * 104 | * @param code 返回码 105 | * @param message 消息 106 | * @param status 状态 107 | */ 108 | public ResultObject(int code, String message, String status) { 109 | this.message = message; 110 | this.code = code; 111 | this.status = status; 112 | } 113 | 114 | /** 115 | * 构造方法 116 | * 117 | * @param code 返回码 118 | * @param message 消息 119 | * @param data 数据对象 120 | */ 121 | public ResultObject(int code, String message, T data) { 122 | this.code = code; 123 | this.message = message; 124 | this.data = data; 125 | } 126 | 127 | /** 128 | * 构造方法 129 | * 130 | * @param code 返回码 131 | * @param message 消息 132 | * @param status 状态 133 | * @param data 数据对象 134 | */ 135 | public ResultObject(int code, String message, String status, T data) { 136 | this.data = data; 137 | this.code = code; 138 | this.message = message; 139 | this.status = status; 140 | } 141 | 142 | /** 143 | * 设置返回码 144 | * 145 | * @param code 返回码 146 | * 147 | * @return {@link ResultObject} 148 | * 149 | * @since 1.1.0 150 | */ 151 | public ResultObject setCode(int code) { 152 | this.code = code; 153 | return this; 154 | } 155 | 156 | /** 157 | * 设置消息 158 | * 159 | * @param message 消息 160 | * 161 | * @return {@link ResultObject} 162 | * 163 | * @since 1.1.0 164 | */ 165 | public ResultObject setMessage(String message) { 166 | this.message = message; 167 | return this; 168 | } 169 | 170 | /** 171 | * 设置状态 172 | * 173 | * @param status 状态 174 | * 175 | * @return {@link ResultObject} 176 | * 177 | * @since 1.1.0 178 | */ 179 | public ResultObject setStatus(String status) { 180 | this.status = status; 181 | return this; 182 | } 183 | 184 | /** 185 | * 设置数据 186 | * 187 | * @param data {@link T} 188 | * 189 | * @return {@link ResultObject} 190 | * 191 | * @since 1.1.0 192 | */ 193 | public ResultObject setData(T data) { 194 | this.data = data; 195 | return this; 196 | } 197 | 198 | /** 199 | * 从没有使用泛型 {@link ResultObject}中复制数据到泛型类中 200 | * 201 | * @param resultObject 没有使用泛型的 {@link ResultObject} 202 | * 203 | * @return 使用了泛型的 {@link ResultObject} 204 | * 205 | * @since 1.1.0 206 | */ 207 | public ResultObject copyFrom(ResultObject resultObject) { 208 | this.message = resultObject.message; 209 | this.status = resultObject.status; 210 | this.code = resultObject.code; 211 | return this; 212 | } 213 | 214 | @Override 215 | public String toString() { 216 | return JSONObject.toJSONString(this); 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/ThreadPool.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import java.util.concurrent.*; 4 | 5 | /** 6 | * @author pantao 7 | */ 8 | public class ThreadPool { 9 | 10 | /** 11 | * executor 12 | */ 13 | public static ThreadPoolExecutor executor = null; 14 | 15 | private static int corePoolSize = 1; 16 | 17 | private static int maximumPoolSize = 3; 18 | 19 | private static long keepAliveTime = 1000; 20 | 21 | private static TimeUnit timeUnit = TimeUnit.MILLISECONDS; 22 | 23 | private static BlockingQueue workQueue = new LinkedBlockingQueue<>(1); 24 | 25 | private static ThreadFactory threadFactory = Thread::new; 26 | 27 | /** 28 | * 新的线程池 29 | */ 30 | public ThreadPoolExecutor newExecutor; 31 | 32 | /** 33 | * 新建线程池 34 | * 35 | * @param core 初始线程大小 36 | * @param maximum 最大线程数 37 | * @param keep 线程存活时长 38 | * @param unit 存活时长单位 39 | */ 40 | public ThreadPool(int core, int maximum, int keep, TimeUnit unit) { 41 | BlockingQueue queue = new LinkedBlockingQueue<>(1); 42 | ThreadFactory factory = Thread::new; 43 | newExecutor = new ThreadPoolExecutor(core, maximum, keep, unit, queue, factory); 44 | } 45 | 46 | /** 47 | * 初始化线程池 48 | */ 49 | public static void init() { 50 | executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, workQueue, 51 | threadFactory); 52 | } 53 | 54 | /** 55 | * 初始化线程池 56 | * 57 | * @param corePoolSize 初始Size 58 | * @param maximumPoolSize 最大Size 59 | * @param keepAliveTime 存活时长 60 | * @param timeUnit 时间单位 61 | */ 62 | public static void init(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit timeUnit) { 63 | ThreadPool.corePoolSize = corePoolSize; 64 | ThreadPool.maximumPoolSize = maximumPoolSize; 65 | ThreadPool.keepAliveTime = keepAliveTime; 66 | ThreadPool.timeUnit = timeUnit; 67 | init(); 68 | } 69 | 70 | /** 71 | * 获取获取池 72 | * 73 | * @return {@link ThreadPoolExecutor} 74 | */ 75 | public static ThreadPoolExecutor getExecutor() { 76 | if (Checker.isNull(executor)) { 77 | init(); 78 | } 79 | return executor; 80 | } 81 | 82 | /** 83 | * 获取coreSize 84 | * 85 | * @return {@link Integer} 86 | */ 87 | public static int getCorePoolSize() { 88 | return corePoolSize; 89 | } 90 | 91 | /** 92 | * 设置coreSize 93 | * 94 | * @param corePoolSize {@link Integer} 95 | */ 96 | public static void setCorePoolSize(int corePoolSize) { 97 | ThreadPool.corePoolSize = corePoolSize; 98 | } 99 | 100 | /** 101 | * 获取maxSize 102 | * 103 | * @return {@link Integer} 104 | */ 105 | public static int getMaximumPoolSize() { 106 | return maximumPoolSize; 107 | } 108 | 109 | /** 110 | * 设置maxSize 111 | * 112 | * @param maximumPoolSize {@link Integer} 113 | */ 114 | public static void setMaximumPoolSize(int maximumPoolSize) { 115 | ThreadPool.maximumPoolSize = maximumPoolSize; 116 | } 117 | 118 | /** 119 | * 获取aliveTime 120 | * 121 | * @return {@link Long} 122 | */ 123 | public static long getKeepAliveTime() { 124 | return keepAliveTime; 125 | } 126 | 127 | /** 128 | * 设置aliveTime 129 | * 130 | * @param keepAliveTime {@link Integer} 131 | */ 132 | public static void setKeepAliveTime(int keepAliveTime) { 133 | ThreadPool.keepAliveTime = keepAliveTime; 134 | } 135 | 136 | /** 137 | * 获取timeUnit 138 | * 139 | * @return {@link TimeUnit} 140 | */ 141 | public static TimeUnit getTimeUnit() { 142 | return timeUnit; 143 | } 144 | 145 | /** 146 | * 设置timeUnit 147 | * 148 | * @param unit {@link TimeUnit} 149 | */ 150 | public static void setTimeUnit(TimeUnit unit) { 151 | ThreadPool.timeUnit = unit; 152 | } 153 | 154 | /** 155 | * 获取workQueue 156 | * 157 | * @return {@link BlockingQueue} 158 | */ 159 | public static BlockingQueue getWorkQueue() { 160 | return workQueue; 161 | } 162 | 163 | /** 164 | * 设置workQueue 165 | * 166 | * @param workQueue {@link BlockingQueue} 167 | */ 168 | public static void setWorkQueue(BlockingQueue workQueue) { 169 | ThreadPool.workQueue = workQueue; 170 | } 171 | 172 | /** 173 | * 获取threadFactory 174 | * 175 | * @return {@link ThreadFactory} 176 | */ 177 | public static ThreadFactory getThreadFactory() { 178 | return threadFactory; 179 | } 180 | 181 | /** 182 | * 设置threadFactory 183 | * 184 | * @param threadFactory {@link ThreadFactory} 185 | */ 186 | public static void setThreadFactory(ThreadFactory threadFactory) { 187 | ThreadPool.threadFactory = threadFactory; 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/modules/constant/HttpHeaders.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.modules.constant; 2 | 3 | /** 4 | * 参考 5 | * 6 | * @author pantao 7 | * @since 2018/6/12 8 | */ 9 | public class HttpHeaders { 10 | 11 | /** 12 | * 主要用于标识 Ajax 及可扩展标记语言请求 13 | */ 14 | public static final String X_REQUESTED_WITH = "X-Requested-With"; 15 | 16 | /** 17 | * 能够接受的回应内容类型 18 | */ 19 | public static final String ACCEPT = "Accept"; 20 | 21 | /** 22 | * 能够接受的编码方式列表 23 | */ 24 | public static final String ACCEPT_ENCODING = "Accept-Encoding"; 25 | 26 | /** 27 | * 能够接受的回应内容的自然语言列表 28 | */ 29 | public static final String ACCEPT_LANGUAGE = "Accept-Language"; 30 | 31 | /** 32 | * 该浏览器想要优先使用的连接类型 33 | */ 34 | public static final String CONNECTION = "Connection"; 35 | 36 | /** 37 | * 以八位字节数组(8位的字节)表示的请求体的长度 38 | */ 39 | public static final String CONTENT_LENGTH = "Content-Length"; 40 | 41 | /** 42 | * 请求体的多媒体类型 43 | */ 44 | public static final String CONTENT_TYPE = "Content-Type"; 45 | 46 | /** 47 | * 超文本传输协议Cookie 48 | */ 49 | public static final String COOKIE = "Cookie"; 50 | 51 | /** 52 | * 服务器的域名(用于虚拟主机),以及服务器所监听的传输控制协议端口号 53 | */ 54 | public static final String HOST = "Host"; 55 | 56 | /** 57 | * 发起一个针对 跨来源资源共享 的请求 58 | */ 59 | public static final String ORIGIN = "Origin"; 60 | 61 | /** 62 | * 表示浏览器所访问的前一个页面,正是那个页面上的某个链接将浏览器带到了当前所请求的这个页面 63 | */ 64 | public static final String REFERER = "Referer"; 65 | 66 | /** 67 | * 浏览器的浏览器身份标识字符串 68 | */ 69 | public static final String USER_AGENT = "User-Agent"; 70 | 71 | /** 72 | * 能够接受的字符集 73 | */ 74 | public static final String ACCEPT_CHARSET = "Accept-Charset"; 75 | 76 | /** 77 | * 能够接受的按照时间来表示的版本 78 | */ 79 | public static final String ACCEPT_DATETIME = "Accept-Datetime"; 80 | 81 | /** 82 | * 用于超文本传输协议的认证的认证信息 83 | */ 84 | public static final String AUTHORIZATION = "Authorization"; 85 | 86 | /** 87 | * 用来指定在这次的请求/响应链中的所有缓存机制 都必须 遵守的指令 88 | */ 89 | public static final String CACHE_CONTROL = "Cache-Control"; 90 | 91 | /** 92 | * 请求体的内容的二进制 MD5 散列值,以 Base64 编码的结果 93 | */ 94 | public static final String CONTENT_MD5 = "Content-MD5"; 95 | 96 | /** 97 | * 发送该消息的日期和时间 98 | */ 99 | public static final String DATE = "Date"; 100 | 101 | /** 102 | * 表明客户端要求服务器做出特定的行为 103 | */ 104 | public static final String EXPECT = "Expect"; 105 | 106 | /** 107 | * 发起此请求的用户的邮件地址 108 | */ 109 | public static final String FROM = "From"; 110 | 111 | /** 112 | * 仅当客户端提供的实体与服务器上对应的实体相匹配时,才进行对应的操作。主要作用时,用作像 PUT 这样的方法中,仅当从用户上次更新某个资源以来,该资源未被修改的情况下,才更新该资源 113 | */ 114 | public static final String IF_MATCH = "If-Match"; 115 | 116 | /** 117 | * 允许在对应的内容未被修改的情况下返回304未修改 118 | */ 119 | public static final String IF_MODIFIED_SINCE = "If-Modified-Since"; 120 | 121 | /** 122 | * 允许在对应的内容未被修改的情况下返回304未修改 123 | */ 124 | public static final String IF_NONE_MATCH = "If-None-Match"; 125 | 126 | /** 127 | * 如果该实体未被修改过,则向我发送我所缺少的那一个或多个部分;否则,发送整个新的实体 128 | */ 129 | public static final String IF_RANGE = "If-Range"; 130 | 131 | /** 132 | * 仅当该实体自某个特定时间已来未被修改的情况下,才发送回应 133 | */ 134 | public static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since"; 135 | 136 | /** 137 | * 限制该消息可被代理及网关转发的次数 138 | */ 139 | public static final String MAX_FORWARDS = "Max-Forwards"; 140 | 141 | /** 142 | * 与具体的实现相关,这些字段可能在请求/回应链中的任何时候产生多种效果 143 | */ 144 | public static final String PRAGMA = "Pragma"; 145 | 146 | /** 147 | * 用来向代理进行认证的认证信息 148 | */ 149 | public static final String PROXY_AUTHORIZATION = "Proxy-Authorization"; 150 | 151 | /** 152 | * 仅请求某个实体的一部分 153 | */ 154 | public static final String RANGE = "Range"; 155 | 156 | /** 157 | * 浏览器预期接受的传输编码方式:可使用回应协议头 Transfer-Encoding 字段中的值;另外还可用"trailers"(与"分块 "传输方式相关)这个值来表明浏览器希望在最后一个尺寸为0的块之后还接收到一些额外的字段 158 | */ 159 | public static final String TE = "TE"; 160 | 161 | /** 162 | * 要求服务器升级到另一个协议 163 | */ 164 | public static final String UPGRADE = "Upgrade"; 165 | 166 | /** 167 | * 向服务器告知,这个请求是由哪些代理发出的 168 | */ 169 | public static final String VIA = "Via"; 170 | 171 | /** 172 | * 一个一般性的警告,告知,在实体内容体中可能存在错误 173 | */ 174 | public static final String WARNING = "Warning"; 175 | 176 | /** 177 | * 请求某个网页应用程序停止跟踪某个用户 178 | */ 179 | public static final String DNT = "dnt"; 180 | 181 | /** 182 | * 一个事实标准 ,用于标识某个通过超文本传输协议代理或负载均衡连接到某个网页服务器的客户端的原始互联网地址 183 | */ 184 | public static final String X_FORWARDED_FOR = "X-Forwarded-For"; 185 | 186 | /** 187 | * 一个事实标准 ,用于识别客户端原本发出的 Host 请求头部 188 | */ 189 | public static final String X_FORWARDED_HOST = "X-Forwarded-Host"; 190 | 191 | /** 192 | * 一个事实标准,用于标识某个超文本传输协议请求最初所使用的协议 193 | */ 194 | public static final String X_FORWARDED_PROTO = "X-Forwarded-Proto"; 195 | 196 | /** 197 | * 被微软的服务器和负载均衡器所使用的非标准头部字段 198 | */ 199 | public static final String FRONT_END_HTTPS = "Front-End-Https"; 200 | 201 | /** 202 | * 请求某个网页应用程序使用该协议头字段中指定的方法(一般是PUT或DELETE)来覆盖掉在请求中所指定的方法(一般是POST)。当某个浏览器或防火墙阻止直接发送PUT 或DELETE 203 | * 方法时(注意,这可能是因为软件中的某个漏洞,因而需要修复,也可能是因为某个配置选项就是如此要求的,因而不应当设法绕过),可使用这种方式 204 | */ 205 | public static final String X_HTTP_METHOD_OVERRIDE = "X-Http-Method-Override"; 206 | 207 | /** 208 | * 使服务器更容易解读设备User-Agent字段中常见的设备型号、固件信息 209 | */ 210 | public static final String X_ATT_DEVICEID = "X-ATT-DeviceId"; 211 | 212 | /** 213 | * 链接到互联网上的一个XML文件,其完整、仔细地描述了正在连接的设备 214 | */ 215 | public static final String X_WAP_PROFILE = "X-Wap-Profile"; 216 | 217 | /** 218 | * 该字段源于早期超文本传输协议版本实现中的错误 219 | */ 220 | public static final String PROXY_CONNECTION = "Proxy-Connection"; 221 | 222 | /** 223 | * 用于防止跨站请求伪造 224 | */ 225 | public static final String X_CSRF_TOKEN = "X-Csrf-Token"; 226 | } -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import javax.script.ScriptEngine; 4 | import javax.script.ScriptEngineManager; 5 | import javax.script.ScriptException; 6 | import java.math.BigDecimal; 7 | import java.util.Stack; 8 | import java.util.regex.Pattern; 9 | 10 | /** 11 | * 简单计算器 12 | * 13 | * @author pantao 14 | */ 15 | public class Calculator { 16 | 17 | private static final String CALCULATION_FORMULA_PATTERN_STRING = "\\((\\d+(\\.\\d+)?([+\\-*/]))*\\d+(\\.\\d+)?\\)"; 18 | 19 | private static final Pattern FORMULA_REPLACE_PATTERN = Pattern.compile(CALCULATION_FORMULA_PATTERN_STRING); 20 | 21 | private static final Pattern CALCULATION_FORMULA_PATTERN = Pattern.compile(CALCULATION_FORMULA_PATTERN_STRING 22 | .substring(2, CALCULATION_FORMULA_PATTERN_STRING.length() - 2) + "=?"); 23 | 24 | /** 25 | * 默认精度 26 | */ 27 | private static int precision = 100; 28 | 29 | private Calculator() {} 30 | 31 | /** 32 | * 连续计算,使用默认精度(100) 33 | * 34 | * @param formula 表达式 35 | * 36 | * @return {@link BigDecimal} 37 | * 38 | * @throws Exception 异常 39 | */ 40 | public static BigDecimal calculate(String formula) throws Exception { 41 | formula = formula.replaceAll("\\s", ""); 42 | if (isFormula(formula)) { 43 | return doCalculate(formula); 44 | } else { 45 | throw new Exception("calculation formula is not valid, please check up on it carefully."); 46 | } 47 | } 48 | 49 | /** 50 | * 验证计算式是否合法 51 | * 52 | * @param formula 计算式 53 | * 54 | * @return {@link Boolean} 55 | */ 56 | public static boolean isFormula(String formula) { 57 | while (FORMULA_REPLACE_PATTERN.matcher(formula).find()) { 58 | formula = formula.replaceAll(CALCULATION_FORMULA_PATTERN_STRING, "0"); 59 | } 60 | return CALCULATION_FORMULA_PATTERN.matcher(formula).matches(); 61 | } 62 | 63 | /** 64 | * 开始计算 65 | * 66 | * @param formula 表达式 67 | * 68 | * @return {@link BigDecimal} 69 | * 70 | * @throws Exception 异常 71 | */ 72 | private static BigDecimal doCalculate(String formula) throws Exception { 73 | Stack stack = new Stack<>(); 74 | BigDecimal result = BigDecimal.valueOf(0); 75 | BigDecimal number = BigDecimal.valueOf(0); 76 | char sign = '+'; 77 | boolean isDecimal = false; 78 | BigDecimal decimalSign = new BigDecimal(0.1); 79 | 80 | for (int i = 0; i < formula.length(); i++) { 81 | char c = formula.charAt(i); 82 | if (Character.isDigit(c)) { 83 | BigDecimal num = new BigDecimal(c - '0'); 84 | number = isDecimal ? number.add(num.multiply(decimalSign)) : num.add(number.multiply(BigDecimal 85 | .valueOf(10))); 86 | decimalSign = decimalSign.multiply(BigDecimal.valueOf(0.1)); 87 | } else if (c == '.') { 88 | isDecimal = true; 89 | decimalSign = BigDecimal.valueOf(0.1); 90 | } else if (c == '(') { 91 | BigDecimal[] res = calculateInlineFormula(i + 1, formula); 92 | number = number.compareTo(BigDecimal.valueOf(0)) == 0 ? res[0] : number.multiply(res[0]); 93 | i = res[1].intValue(); 94 | } 95 | boolean isNotDigit = (!Character.isDigit(c) && c != '.' && c != '(') || i == formula.length() - 1; 96 | if (isNotDigit) { 97 | if (sign == '+') { 98 | stack.push(number); 99 | } else if (sign == '-') { 100 | stack.push(number.multiply(BigDecimal.valueOf(-1))); 101 | } else if (sign == '*') { 102 | stack.push(number.multiply(stack.pop())); 103 | } else if (sign == '/') { 104 | stack.push(stack.pop().divide(number, getPrecision(), BigDecimal.ROUND_HALF_UP)); 105 | } 106 | sign = c; 107 | isDecimal = false; 108 | number = BigDecimal.valueOf(0); 109 | } 110 | } 111 | for (BigDecimal i : stack) { 112 | result = result.add(i); 113 | } 114 | return result.setScale(getPrecision(), BigDecimal.ROUND_HALF_UP); 115 | } 116 | 117 | /** 118 | * 计算()内的表达式 119 | * 120 | * @param start 开始位置 121 | * @param formula 表达式 122 | * 123 | * @return 长度为2的 {@link BigDecimal}数组,位置0表示计算结果,位置1表示结束位置 124 | * 125 | * @throws Exception 异常 126 | */ 127 | private static BigDecimal[] calculateInlineFormula(int start, String formula) throws Exception { 128 | BigDecimal[] result = new BigDecimal[2]; 129 | StringBuilder inlineFormula = new StringBuilder(); 130 | for (; start < formula.length(); start++) { 131 | char c = formula.charAt(start); 132 | if (c == '(') { 133 | BigDecimal[] res = calculateInlineFormula(start + 1, formula); 134 | inlineFormula.append(res[0]); 135 | start = res[1].intValue(); 136 | } else if (c == ')') { 137 | break; 138 | } else { 139 | inlineFormula.append(c); 140 | } 141 | } 142 | result[0] = calculate(inlineFormula.toString()); 143 | result[1] = BigDecimal.valueOf(start); 144 | return result; 145 | } 146 | 147 | /** 148 | * 连续计算,可能导致数据失真 149 | * 150 | * @param formula 表达式 151 | * 152 | * @return {@link Double} 153 | * 154 | * @throws ScriptException 异常 155 | */ 156 | public static Object calculateUseEval(String formula) throws ScriptException { 157 | ScriptEngineManager mgr = new ScriptEngineManager(); 158 | ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); 159 | return jsEngine.eval(formula); 160 | } 161 | 162 | /** 163 | * 获取当前精度 164 | * 165 | * @return {@link Integer} 166 | */ 167 | public static int getPrecision() { 168 | return precision; 169 | } 170 | 171 | /** 172 | * 设置精度,即精确到多少位小数 173 | * 174 | * @param precision 精度 175 | */ 176 | public static void setPrecision(int precision) { 177 | Calculator.precision = precision; 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/FileExecutorTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.zhazhapan.util.interfaces.SimpleHutoolWatcher; 4 | import org.junit.Test; 5 | 6 | import java.io.File; 7 | import java.io.IOException; 8 | 9 | /** 10 | * @author pantao 11 | */ 12 | public class FileExecutorTest { 13 | 14 | @Test 15 | public void testCreateFolder() { 16 | assert FileExecutor.createFolder("/Users/pantao/Desktop/upload/20180131"); 17 | } 18 | 19 | @Test 20 | public void testScanFolder() { 21 | System.out.println(Formatter.listToJson(FileExecutor.scanFolder("C:\\Users\\pantao\\Downloads\\image"))); 22 | } 23 | 24 | @Test 25 | public void testSplitFile() throws IOException { 26 | FileExecutor.splitFile("/Users/pantao/Desktop/test/Dump20171220.sql", new long[]{1000, 2000, 3000}); 27 | } 28 | 29 | @Test 30 | public void testMergeFile() throws IOException { 31 | String[] files = new String[]{"/Users/pantao/Desktop/test/Dump20171220_1.sql", 32 | "/Users/pantao/Desktop/test" + "/Dump20171220_2.sql"}; 33 | FileExecutor.mergeFiles(files, "/Users/pantao/Desktop/test/test_merge.sql", "/\\*.*?\\*/;\r?\n?"); 34 | } 35 | 36 | @Test 37 | public void testRenameFile() { 38 | FileExecutor.renameFiles("C:\\Users\\pantao\\Downloads\\image", "bg_", ".jpg", 138); 39 | } 40 | 41 | @Test 42 | public void testCopyFiles() throws IOException { 43 | FileExecutor.copyFiles(new String[]{"/Users/pantao/Desktop/qiniu.jar"}, "/Users/pantao/Desktop/test"); 44 | } 45 | 46 | @Test 47 | public void testCopyDirectories() throws IOException { 48 | FileExecutor.copyDirectories(new String[]{"/Users/pantao/Desktop/test"}, "/Users/pantao/Desktop/new"); 49 | } 50 | 51 | @Test 52 | public void renameTo() { 53 | File file = new File("c:\\test\\haha\\lfss.apk"); 54 | System.out.println("" + true); 55 | } 56 | 57 | @Test 58 | public void listFile() { 59 | } 60 | 61 | @Test 62 | public void listFile1() { 63 | } 64 | 65 | @Test 66 | public void getFileSuffix() { 67 | } 68 | 69 | @Test 70 | public void getFileSuffix1() { 71 | } 72 | 73 | @Test 74 | public void scanFolderAsArray() { 75 | } 76 | 77 | @Test 78 | public void scanFolder() { 79 | } 80 | 81 | @Test 82 | public void scanFolder1() { 83 | } 84 | 85 | @Test 86 | public void read() { 87 | } 88 | 89 | @Test 90 | public void read1() { 91 | } 92 | 93 | @Test 94 | public void read2() { 95 | } 96 | 97 | @Test 98 | public void read3() { 99 | } 100 | 101 | @Test 102 | public void copyDirectories() { 103 | } 104 | 105 | @Test 106 | public void copyDirectories1() { 107 | } 108 | 109 | @Test 110 | public void copyDirectories2() { 111 | } 112 | 113 | @Test 114 | public void copyDirectories3() { 115 | } 116 | 117 | @Test 118 | public void copyFiles() { 119 | } 120 | 121 | @Test 122 | public void copyFiles1() { 123 | } 124 | 125 | @Test 126 | public void createFolder() { 127 | } 128 | 129 | @Test 130 | public void createFolder1() { 131 | } 132 | 133 | @Test 134 | public void copyFiles2() { 135 | } 136 | 137 | @Test 138 | public void copyFiles3() { 139 | } 140 | 141 | @Test 142 | public void splitFile() { 143 | } 144 | 145 | @Test 146 | public void splitFile1() { 147 | } 148 | 149 | @Test 150 | public void splitFile2() { 151 | } 152 | 153 | @Test 154 | public void splitFile3() { 155 | } 156 | 157 | @Test 158 | public void renameFiles() { 159 | } 160 | 161 | @Test 162 | public void renameFiles1() { 163 | } 164 | 165 | @Test 166 | public void renameFiles2() { 167 | } 168 | 169 | @Test 170 | public void renameFiles3() { 171 | } 172 | 173 | @Test 174 | public void renameFiles4() { 175 | } 176 | 177 | @Test 178 | public void renameFiles5() { 179 | } 180 | 181 | @Test 182 | public void mergeFiles() { 183 | } 184 | 185 | @Test 186 | public void mergeFiles1() { 187 | } 188 | 189 | @Test 190 | public void mergeFiles2() { 191 | } 192 | 193 | @Test 194 | public void mergeFiles3() { 195 | } 196 | 197 | @Test 198 | public void getFiles() { 199 | } 200 | 201 | @Test 202 | public void getFiles1() { 203 | } 204 | 205 | @Test 206 | public void createFile() { 207 | } 208 | 209 | @Test 210 | public void createFile1() { 211 | } 212 | 213 | @Test 214 | public void createNewFile() { 215 | } 216 | 217 | @Test 218 | public void createNewFile1() { 219 | } 220 | 221 | @Test 222 | public void deleteFile() { 223 | } 224 | 225 | @Test 226 | public void deleteFile1() { 227 | } 228 | 229 | @Test 230 | public void saveFile() { 231 | } 232 | 233 | @Test 234 | public void saveFile1() { 235 | } 236 | 237 | @Test 238 | public void saveFile2() { 239 | } 240 | 241 | @Test 242 | public void readFile() { 243 | } 244 | 245 | @Test 246 | public void readFile1() { 247 | } 248 | 249 | @Test 250 | public void readFile2() { 251 | } 252 | 253 | @Test 254 | public void saveLogFile() { 255 | } 256 | 257 | @Test 258 | public void saveFile3() { 259 | } 260 | 261 | @Test 262 | public void watchFile() { 263 | FileExecutor.watchFile("c:\\", new SimpleHutoolWatcher() { 264 | @Override 265 | public void doSomething() { 266 | // do something 267 | } 268 | }, true); 269 | } 270 | 271 | @Test 272 | public void watchFile1() { 273 | } 274 | 275 | @Test 276 | public void parseJsonArray() { 277 | } 278 | 279 | @Test 280 | public void parseJsonArray1() { 281 | } 282 | 283 | @Test 284 | public void parseJsonArray2() { 285 | } 286 | 287 | @Test 288 | public void parseJsonObject() { 289 | } 290 | 291 | @Test 292 | public void parseJsonObject1() { 293 | } 294 | 295 | @Test 296 | public void parseJsonObject2() { 297 | } 298 | 299 | @Test 300 | public void getSuffix() { 301 | } 302 | 303 | @Test 304 | public void getSuffix1() { 305 | } 306 | } 307 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/office/MsExcelUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.office; 2 | 3 | import com.zhazhapan.modules.constant.ValueConsts; 4 | import com.zhazhapan.util.Checker; 5 | import org.apache.log4j.Logger; 6 | import org.apache.poi.hssf.usermodel.HSSFCellStyle; 7 | import org.apache.poi.xssf.usermodel.XSSFCell; 8 | import org.apache.poi.xssf.usermodel.XSSFRow; 9 | import org.apache.poi.xssf.usermodel.XSSFSheet; 10 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; 11 | 12 | import java.io.IOException; 13 | import java.lang.reflect.InvocationTargetException; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | /** 18 | * @author pantao 19 | * @since 2018/2/28 20 | */ 21 | public class MsExcelUtils { 22 | 23 | private static Logger logger = Logger.getLogger(MsWordUtils.class); 24 | 25 | private static XSSFWorkbook xssfWorkbook = null; 26 | 27 | private static XSSFSheet xssfSheet = null; 28 | 29 | private MsExcelUtils() {} 30 | 31 | /** 32 | * 创建工作簿 33 | */ 34 | public static void createXssfWorkbook() { 35 | xssfWorkbook = new XSSFWorkbook(); 36 | } 37 | 38 | /** 39 | * 创建工作表 40 | * 41 | * @param sheetName 工作表名 42 | */ 43 | public static void createXssfSheet(String sheetName) { 44 | createXssfWorkbookIfNull(); 45 | xssfSheet = xssfWorkbook.createSheet(sheetName); 46 | } 47 | 48 | /** 49 | * 添加一个表格 50 | * 51 | * @param values 值 52 | * @param styles 样式 53 | */ 54 | public static void appendTable(String[][] values, HSSFCellStyle[][] styles) { 55 | createXssfSheetIfNull(); 56 | if (Checker.isNotNull(values)) { 57 | for (int i = 0; i < values.length; i++) { 58 | XSSFRow row = xssfSheet.createRow(i); 59 | for (int j = 0; j < values[i].length; j++) { 60 | XSSFCell cell = row.createCell(j); 61 | cell.setCellValue(values[i][j]); 62 | if (Checker.isNotNull(styles)) { 63 | int rowIndex = i < styles.length ? i : styles.length - 1; 64 | int colIndex = j < styles[rowIndex].length ? j : styles[rowIndex].length - 1; 65 | cell.setCellStyle(styles[rowIndex][colIndex]); 66 | } 67 | } 68 | } 69 | } 70 | } 71 | 72 | /** 73 | * 读取表格 74 | * 75 | * @param sheetIndex 表索性 76 | * 77 | * @return {@link List} 78 | */ 79 | public static List> readTable(int sheetIndex) { 80 | setXssfSheet(xssfWorkbook.getSheetAt(sheetIndex)); 81 | return readTable(); 82 | } 83 | 84 | /** 85 | * 读取表格 86 | * 87 | * @param sheetName 表名 88 | * 89 | * @return {@link List} 90 | */ 91 | public static List> readTable(String sheetName) { 92 | setXssfSheet(xssfWorkbook.getSheet(sheetName)); 93 | return readTable(); 94 | } 95 | 96 | /** 97 | * 读取表格 98 | * 99 | * @return {@link List} 100 | */ 101 | public static List> readTable() { 102 | List> values = new ArrayList<>(ValueConsts.THIRTY_TWO_INT); 103 | for (int i = 0; i < xssfSheet.getLastRowNum() + 1; i++) { 104 | XSSFRow row = xssfSheet.getRow(i); 105 | if (Checker.isNotNull(row)) { 106 | List value = new ArrayList<>(ValueConsts.THIRTY_TWO_INT); 107 | for (int j = 0; j < row.getLastCellNum(); j++) { 108 | XSSFCell cell = row.getCell(j); 109 | if (Checker.isNotNull(cell)) { 110 | value.add(cell.getStringCellValue()); 111 | } 112 | } 113 | if (Checker.isNotEmpty(value)) { 114 | values.add(value); 115 | } 116 | } 117 | } 118 | return values; 119 | } 120 | 121 | /** 122 | * 保存到本地,不关闭工作簿 123 | * 124 | * @param path 路径 125 | * 126 | * @throws IOException 异常 127 | * @throws NoSuchMethodException 异常 128 | * @throws IllegalAccessException 异常 129 | * @throws InvocationTargetException 异常 130 | */ 131 | public static void writeTo(String path) throws IOException, NoSuchMethodException, IllegalAccessException, 132 | InvocationTargetException { 133 | MsUtils.writeTo(xssfWorkbook, path); 134 | } 135 | 136 | /** 137 | * 保存到本地,关闭工作簿,并创建一个新的工作簿 138 | * 139 | * @param path 路径 140 | * 141 | * @throws IOException 异常 142 | * @throws NoSuchMethodException 异常 143 | * @throws IllegalAccessException 异常 144 | * @throws InvocationTargetException 异常 145 | */ 146 | public static void writeAndClose(String path) throws InvocationTargetException, NoSuchMethodException, 147 | IllegalAccessException, IOException { 148 | writeTo(path); 149 | xssfWorkbook.close(); 150 | createXssfWorkbook(); 151 | } 152 | 153 | /** 154 | * 获取当前操作的工作簿 155 | * 156 | * @return 工作簿 157 | */ 158 | public static XSSFWorkbook getXssfWorkbook() { 159 | return xssfWorkbook; 160 | } 161 | 162 | /** 163 | * 修改当前操作的工作簿 164 | * 165 | * @param xssfWorkbook {@link XSSFWorkbook} 166 | */ 167 | public static void setXssfWorkbook(XSSFWorkbook xssfWorkbook) { 168 | MsExcelUtils.xssfWorkbook = xssfWorkbook; 169 | } 170 | 171 | /** 172 | * 修改当前操作的工作簿 173 | * 174 | * @param path 本地工作簿文件路径 175 | * 176 | * @throws IOException 异常 177 | */ 178 | public static void setXssfWorkbook(String path) throws IOException { 179 | MsExcelUtils.xssfWorkbook = new XSSFWorkbook(path); 180 | } 181 | 182 | /** 183 | * 获取当前操作的工作表 184 | * 185 | * @return {@link XSSFSheet} 186 | */ 187 | public static XSSFSheet getXssfSheet() { 188 | return xssfSheet; 189 | } 190 | 191 | /** 192 | * 修改当前操作的工作表 193 | * 194 | * @param xssfSheet {@link XSSFSheet} 195 | */ 196 | public static void setXssfSheet(XSSFSheet xssfSheet) { 197 | MsExcelUtils.xssfSheet = xssfSheet; 198 | } 199 | 200 | private static void createXssfWorkbookIfNull() { 201 | if (Checker.isNull(xssfWorkbook)) { 202 | xssfWorkbook = new XSSFWorkbook(); 203 | } 204 | } 205 | 206 | private static void createXssfSheetIfNull() { 207 | if (Checker.isNull(xssfSheet)) { 208 | createXssfWorkbookIfNull(); 209 | xssfSheet = xssfWorkbook.createSheet(); 210 | } 211 | } 212 | } 213 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/modules/constant/ValueConsts.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.modules.constant; 2 | 3 | import javax.servlet.http.HttpServletResponse; 4 | import java.io.File; 5 | 6 | /** 7 | * @author pantao 8 | */ 9 | public class ValueConsts { 10 | 11 | public static final String CLASSPATH_PREFIX = "classpath:"; 12 | 13 | public static final String ONE_STR = "1"; 14 | 15 | public static final String TWO_STR = "2"; 16 | 17 | public static final String THREE_STR = "3"; 18 | 19 | public static final String FOUR_STR = "4"; 20 | 21 | public static final String FIVE_STR = "5"; 22 | 23 | public static final String SIX_STR = "6"; 24 | 25 | public static final String SEVEN_STR = "7"; 26 | 27 | public static final String EIGHT_STR = "8"; 28 | 29 | public static final String NINE_STR = "9"; 30 | 31 | public static final String COLON = ":"; 32 | 33 | public static final String SUCCESS = "success"; 34 | 35 | public static final String FAILED = "failed"; 36 | 37 | public static final String ERROR_EN = "error"; 38 | 39 | public static final int FORTY_EIGHT = 48; 40 | 41 | /** 42 | * 英文问号 43 | */ 44 | public static final String QUESTION_MARK = "?"; 45 | 46 | /** 47 | * whois查询的xpath表达式 48 | */ 49 | public static final String WHOIS_DOMAIN_XPATH = "//ul[@class='WhoisLeft fl']"; 50 | 51 | /** 52 | * 获取ip归属地的xpath 53 | */ 54 | public static final String IP_REGION_XPATH = "//div[@class='WhoIpWrap jspu']/p[2]/span[4]"; 55 | 56 | /** 57 | * 公网ip查询地址 58 | */ 59 | public static final String URL_OF_PUBLIC_IP_SEARCH = "http://ip.chinaz.com/getip.aspx"; 60 | 61 | /** 62 | * 井号 63 | */ 64 | public static final String SHARP = "#"; 65 | 66 | /** 67 | * 错误 68 | */ 69 | public static final String ERROR = "错误"; 70 | 71 | /** 72 | * 严重错误 73 | */ 74 | public static final String FATAL_ERROR = "严重错误"; 75 | 76 | /** 77 | * 数字9 78 | */ 79 | public static final int NINE_INT = 9; 80 | 81 | /** 82 | * 英文逗号 83 | */ 84 | public static final String COMMA_SIGN = ","; 85 | 86 | /** 87 | * 字符串“value” 88 | */ 89 | public static final String VALUE_STRING = "value"; 90 | 91 | /** 92 | * 字符串“key” 93 | */ 94 | public static final String KEY_STRING = "key"; 95 | 96 | /** 97 | * 数字32 98 | */ 99 | public static final int THIRTY_TWO_INT = 32; 100 | 101 | /** 102 | * 字符串“token” 103 | */ 104 | public static final String TOKEN_STRING = "token"; 105 | 106 | /** 107 | * 分隔符“/” 108 | */ 109 | public static final String SPLASH_STRING = "/"; 110 | 111 | /** 112 | * 数字16 113 | */ 114 | public static final int SIXTEEN_INT = 16; 115 | 116 | /** 117 | * 验证码上限 118 | */ 119 | public static final int VERIFY_CODE_CEIL = 999999; 120 | 121 | /** 122 | * 验证码下限 123 | */ 124 | public static final int VERIFY_CODE_FLOOR = 100000; 125 | 126 | /** 127 | * 字符串“password” 128 | */ 129 | public static final String PASSWORD_STRING = "password"; 130 | 131 | /** 132 | * 字符串“id” 133 | */ 134 | public static final String ID_STRING = "id"; 135 | 136 | /** 137 | * HttpServletResponse null 138 | */ 139 | public static final HttpServletResponse NULL_RESPONSE = null; 140 | 141 | /** 142 | * 字符串null 143 | */ 144 | public static final String NULL_STRING = null; 145 | 146 | /** 147 | * 字符串“user” 148 | */ 149 | public static final String USER_STRING = "user"; 150 | 151 | /** 152 | * 数字0 153 | */ 154 | public static final int ZERO_INT = 0; 155 | 156 | /** 157 | * false 158 | */ 159 | public static final boolean FALSE = false; 160 | 161 | /** 162 | * true 163 | */ 164 | public static final boolean TRUE = true; 165 | 166 | /** 167 | * 空字符串 168 | */ 169 | public static final String EMPTY_STRING = ""; 170 | 171 | /** 172 | * toString 173 | */ 174 | public static final String TO_STRING_METHOD_NAME = "toString"; 175 | 176 | /** 177 | * 用户的主目录 178 | */ 179 | public static final String USER_HOME = System.getProperty("user.home"); 180 | 181 | /** 182 | * 系统分隔符 183 | */ 184 | public static final String SEPARATOR = File.separator; 185 | 186 | /** 187 | * KB大小,相对B 188 | */ 189 | public static final long KB = 1024; 190 | 191 | /** 192 | * MB大小,相对B 193 | */ 194 | public static final long MB = KB * 1024; 195 | 196 | /** 197 | * GB大小,相对B 198 | */ 199 | public static final long GB = MB * 1024; 200 | 201 | /** 202 | * TB大小,相对B 203 | */ 204 | public static final long TB = GB * 1024; 205 | 206 | /** 207 | * 请求头 208 | */ 209 | public static final String[] USER_AGENT = {"mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, " 210 | + "like gecko) chrome/59.0.3071.115 safari/537.36", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47" + 211 | ".0) Gecko/20100101 Firefox/47.0", "Mozilla/5.0 (Macintosh;Intel Mac OS X x.y; rv:42.0) Gecko" + 212 | "/20100101 Firefox/42.0", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)" + 213 | " Chrome/51.0.2704.103 Safari/537.36", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, " + 214 | "like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41", "Mozilla/5.0 (iPhone; CPU iPhone OS " 215 | + "10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari" + 216 | "/602.1", "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)", 217 | "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 " + 218 | "Mobile/1A543a Safari/419.3"}; 219 | 220 | public static final int DEFAULT_DOWNLOAD_REMAINING_CENTER = 1048576; 221 | 222 | /** 223 | * 网页响应200 224 | */ 225 | public static final int RESPONSE_OK = 200; 226 | 227 | /** 228 | * 数字 1 229 | */ 230 | public static final int ONE_INT = 1; 231 | 232 | /** 233 | * 数字 2 234 | */ 235 | public static final int TWO_INT = 2; 236 | 237 | /** 238 | * 数字 3 239 | */ 240 | public static final int THREE_INT = 3; 241 | 242 | /** 243 | * 数字 100 244 | */ 245 | public static final int ONE_HUNDRED_INT = 100; 246 | 247 | /** 248 | * 英文点号 249 | */ 250 | public static final String DOT_SIGN = "."; 251 | 252 | /** 253 | * 美元符号 254 | */ 255 | public static final String DOLLAR_SIGN = "$"; 256 | 257 | /** 258 | * 符号“-.” 259 | */ 260 | public static final String NEGATIVE_DOT_SIGN = "-."; 261 | 262 | /** 263 | * URL本地路径的前缀 264 | */ 265 | public static final String LOCAL_FILE_URL = "file:"; 266 | 267 | /** 268 | * 空格 269 | */ 270 | public static final String SPACE = " "; 271 | 272 | /** 273 | * 脚本匹配模式 274 | */ 275 | public static final String SCRIPT_FILTER_PATTERN = ".*?"; 276 | 277 | /** 278 | * 桌面路径 279 | */ 280 | public static final String USER_DESKTOP = USER_HOME + SEPARATOR + "Desktop"; 281 | 282 | private ValueConsts() {} 283 | } 284 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/RandomUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import cn.hutool.core.util.RandomUtil; 4 | import com.zhazhapan.modules.constant.ValueConsts; 5 | import com.zhazhapan.util.model.SimpleColor; 6 | 7 | import java.math.BigDecimal; 8 | import java.math.RoundingMode; 9 | import java.util.Random; 10 | 11 | /** 12 | * @author pantao 13 | */ 14 | public class RandomUtils extends org.apache.commons.lang3.RandomUtils { 15 | 16 | private RandomUtils() {} 17 | 18 | /** 19 | * 获取一个Uid(简化版UUID) 20 | * 21 | * @return {@link String} 22 | * 23 | * @since 1.0.9 24 | */ 25 | public static String getRandomUid() { 26 | int[] val = Utils.split(System.currentTimeMillis(), ValueConsts.NINE_INT); 27 | String uid = Utils.toUidString(val[0]) + Utils.toUidString(val[1]); 28 | return uid + RandomUtil.simpleUUID().substring(0, ValueConsts.SIXTEEN_INT - uid.length()); 29 | } 30 | 31 | /** 32 | * 随机Email 33 | * 34 | * @return 随机Email 35 | */ 36 | public static String getRandomEmail() { 37 | return getRandomStringOnlyLowerCase(getRandomInteger(3, 10)) + "@" + getRandomStringOnlyLowerCase(getRandomInteger(3, 5)) + "." + getRandomStringOnlyLowerCase(getRandomInteger(1, 5)); 38 | } 39 | 40 | /** 41 | * 获取随机整数 42 | * 43 | * @param length 长度 44 | * 45 | * @return {@link Integer} 46 | */ 47 | public static int getRandomInteger(int length) { 48 | return getRandomInteger((int) Math.pow(10, length - 1), (int) Math.pow(10, length)); 49 | } 50 | 51 | /** 52 | * 获取随机字符串(包括大小写字母,数字和符号) 53 | * 54 | * @param length 长度 55 | * 56 | * @return {@link String} 57 | */ 58 | public static String getRandomString(int length) { 59 | return getRandomText(32, 126, length); 60 | } 61 | 62 | /** 63 | * 获取没有符号的随机字符串(包括大小写字母和数字) 64 | * 65 | * @param length 长度 66 | * 67 | * @return {@link String} 68 | */ 69 | public static String getRandomStringWithoutSymbol(int length) { 70 | return getRandomTextIgnoreRange(48, 122, length, new int[]{58, 64}, new int[]{91, 96}); 71 | } 72 | 73 | /** 74 | * 获取只有大小写字母的随机字符串 75 | * 76 | * @param length 长度 77 | * 78 | * @return {@link String} 79 | */ 80 | public static String getRandomStringOnlyLetter(int length) { 81 | return getRandomTextIgnoreRange(65, 122, length, new int[]{91, 96}); 82 | } 83 | 84 | /** 85 | * 获取只有小写字母的随机字符串 86 | * 87 | * @param length 长度 88 | * 89 | * @return {@link String} 90 | */ 91 | public static String getRandomStringOnlyLowerCase(int length) { 92 | return getRandomText(97, 122, length); 93 | } 94 | 95 | /** 96 | * 获取只有大写字母的随机字符串 97 | * 98 | * @param length 长度 99 | * 100 | * @return {@link String} 101 | */ 102 | public static String getRandomStringOnlyUpperCase(int length) { 103 | return getRandomText(65, 90, length); 104 | } 105 | 106 | /** 107 | * 获取自定义忽略多个区间的随机字符串 108 | * 109 | * @param floor ascii下限 110 | * @param ceil ascii上限 111 | * @param length 长度 112 | * @param ranges 忽略区间(包含下限和上限长度为2的一维数组),可以有多个忽略区间 113 | * 114 | * @return 字符串 115 | */ 116 | public static String getRandomTextIgnoreRange(int floor, int ceil, int length, int[]... ranges) { 117 | StringBuilder builder = new StringBuilder(); 118 | for (int i = 0; i < length; i++) { 119 | builder.append((char) getRandomIntegerIgnoreRange(floor, ceil, ranges)); 120 | } 121 | return builder.toString(); 122 | } 123 | 124 | /** 125 | * 获取自定义随机字符串 126 | * 127 | * @param floor ascii下限 128 | * @param ceil ascii上限 129 | * @param length 长度 130 | * 131 | * @return 字符串 132 | */ 133 | public static String getRandomText(int floor, int ceil, int length) { 134 | StringBuilder builder = new StringBuilder(); 135 | for (int i = 0; i < length; i++) { 136 | builder.append((char) getRandomInteger(floor, ceil)); 137 | } 138 | return builder.toString(); 139 | } 140 | 141 | /** 142 | * 获取随机浮点数,保留2位小数,下限为0,上限为{@link Double#MAX_VALUE} 143 | * 144 | * @return {@link Double} 145 | */ 146 | public static double getRandomDouble() { 147 | return getRandomDouble(0, Double.MAX_VALUE); 148 | } 149 | 150 | /** 151 | * 获取随机浮点数,保留2位小数 152 | * 153 | * @param floor 下限 154 | * @param ceil 上限 155 | * 156 | * @return {@link Double} 157 | */ 158 | public static double getRandomDouble(double floor, double ceil) { 159 | return getRandomDouble(floor, ceil, 2); 160 | } 161 | 162 | /** 163 | * 获取随机浮点数 164 | * 165 | * @param floor 下限 166 | * @param ceil 上限 167 | * @param precision 精度(小数位数) 168 | * 169 | * @return {@link Double} 170 | */ 171 | public static double getRandomDouble(double floor, double ceil, int precision) { 172 | BigDecimal decimal = new BigDecimal(floor + new Random().nextDouble() * (ceil - floor)); 173 | return decimal.setScale(precision, RoundingMode.HALF_UP).doubleValue(); 174 | } 175 | 176 | /** 177 | * 获取随机整数,下限为0,上限为{@link Integer#MAX_VALUE} 178 | * 179 | * @return {@link Integer} 180 | */ 181 | public static int getRandomInteger() { 182 | return getRandomInteger(0, Integer.MAX_VALUE); 183 | } 184 | 185 | /** 186 | * 获取忽略多个区间段的随机整数 187 | * 188 | * @param floor 下限 189 | * @param ceil 上限 190 | * @param ranges 忽略区间(包含下限和上限长度为2的一维数组),可以有多个忽略区间 191 | * 192 | * @return {@link Integer} 193 | */ 194 | public static int getRandomIntegerIgnoreRange(int floor, int ceil, int[]... ranges) { 195 | int result = getRandomInteger(floor, ceil); 196 | for (int[] range : ranges) { 197 | if (range[0] <= result && result <= range[1]) { 198 | if (range[0] > floor) { 199 | result = getRandomIntegerIgnoreRange(floor, range[0], ranges); 200 | } else if (range[1] < ceil) { 201 | result = getRandomIntegerIgnoreRange(range[1], ceil, ranges); 202 | } else { 203 | return -1; 204 | } 205 | } 206 | } 207 | return result; 208 | } 209 | 210 | /** 211 | * 获取随机整数 212 | * 213 | * @param floor 下限 214 | * @param ceil 上限 215 | * 216 | * @return {@link Integer} 217 | */ 218 | public static int getRandomInteger(int floor, int ceil) { 219 | return floor + new Random().nextInt(ceil - floor); 220 | } 221 | 222 | /** 223 | * 获取随机数字 224 | * 225 | * @param length 长度 226 | * 227 | * @return {@link String} 228 | * 229 | * @since 1.1.0 230 | */ 231 | public static String getRandomNumber(int length) { 232 | return getRandomText(48, 58, length); 233 | } 234 | 235 | /** 236 | * 获取随机颜色 237 | * 238 | * @param opacity 不透明度 239 | * 240 | * @return {@link SimpleColor} 241 | */ 242 | public static SimpleColor getRandomColor(double opacity) { 243 | Random ran = new Random(); 244 | int b = ran.nextInt(255); 245 | int r = 255 - b; 246 | return new SimpleColor(b + ran.nextInt(r), b + ran.nextInt(r), b + ran.nextInt(r), opacity); 247 | 248 | } 249 | 250 | /** 251 | * 获取随机颜色,默认不透明 252 | * 253 | * @return {@link SimpleColor} 254 | */ 255 | public static SimpleColor getRandomColor() { 256 | return getRandomColor(1d); 257 | } 258 | } 259 | -------------------------------------------------------------------------------- /src/test/java/com/zhazhapan/util/CheckerTest.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.zhazhapan.modules.constant.ValueConsts; 5 | import com.zhazhapan.util.model.CheckResult; 6 | import com.zhazhapan.util.model.TestBean; 7 | import org.junit.Test; 8 | 9 | /** 10 | * @author pantao 11 | * @since 2018/1/23 12 | */ 13 | public class CheckerTest { 14 | 15 | private final String TRUE = "true"; 16 | 17 | private final String FALSE = "false"; 18 | 19 | @Test 20 | public void testMacOS() { 21 | assert Checker.isMacOS(); 22 | } 23 | 24 | @Test 25 | public void testCheck() { 26 | for (int i = 0; i < 100; i++) { 27 | boolean b = RandomUtils.nextBoolean(); 28 | assert Checker.check(TRUE, FALSE, b).equals(b ? TRUE : FALSE); 29 | } 30 | for (int i = 0; i < 100; i++) { 31 | boolean b = RandomUtils.nextBoolean(); 32 | assert Checker.check(1, 2, b) == (b ? 1 : 2); 33 | } 34 | // 处理复杂的逻辑 35 | for (int i = 0; i < 100; i++) { 36 | // 推荐使用lambda表达式 37 | String res = Checker.check(TRUE, FALSE, () -> { 38 | String kk = RandomUtils.getRandomString(ValueConsts.NINE_INT); 39 | String mm = RandomUtils.getRandomString(ValueConsts.NINE_INT); 40 | return kk.length() / 2 < mm.indexOf(ValueConsts.COMMA_SIGN); 41 | }); 42 | System.out.println(res); 43 | } 44 | } 45 | 46 | @Test 47 | public void objectIn() { 48 | String s = "ss"; 49 | String[] mm = {"ss", "gg"}; 50 | String[] gg = {"gg", "ss"}; 51 | String[] hh = {"s", "g"}; 52 | assert Checker.isIn(s, mm); 53 | assert Checker.isIn(s, gg); 54 | assert !Checker.isIn(s, hh); 55 | } 56 | 57 | @Test 58 | public void isImage() { 59 | } 60 | 61 | @Test 62 | public void isImage1() { 63 | } 64 | 65 | @Test 66 | public void isNotExists() { 67 | } 68 | 69 | @Test 70 | public void isExists() { 71 | } 72 | 73 | @Test 74 | public void isLimited() { 75 | } 76 | 77 | @Test 78 | public void isWindows() { 79 | } 80 | 81 | @Test 82 | public void isMacOS() { 83 | } 84 | 85 | @Test 86 | public void isLinux() { 87 | } 88 | 89 | @Test 90 | public void isSorted() { 91 | } 92 | 93 | @Test 94 | public void isDate() { 95 | } 96 | 97 | @Test 98 | public void replace() { 99 | } 100 | 101 | @Test 102 | public void replace1() { 103 | } 104 | 105 | @Test 106 | public void isEmail() { 107 | } 108 | 109 | @Test 110 | public void isDecimal() { 111 | } 112 | 113 | @Test 114 | public void isNumber() { 115 | } 116 | 117 | @Test 118 | public void isNull() { 119 | } 120 | 121 | @Test 122 | public void isNotNull() { 123 | } 124 | 125 | @Test 126 | public void isNullOrEmpty() { 127 | } 128 | 129 | @Test 130 | public void isEmpty() { 131 | } 132 | 133 | @Test 134 | public void isNotEmpty() { 135 | } 136 | 137 | @Test 138 | public void checkNull() { 139 | } 140 | 141 | @Test 142 | public void checkNull1() { 143 | } 144 | 145 | @Test 146 | public void checkNull2() { 147 | } 148 | 149 | @Test 150 | public void checkNull3() { 151 | } 152 | 153 | @Test 154 | public void checkNull4() { 155 | } 156 | 157 | @Test 158 | public void checkNull5() { 159 | } 160 | 161 | @Test 162 | public void checkNull6() { 163 | } 164 | 165 | @Test 166 | public void checkNull7() { 167 | } 168 | 169 | @Test 170 | public void checkNull8() { 171 | } 172 | 173 | @Test 174 | public void checkNull9() { 175 | } 176 | 177 | @Test 178 | public void checkNull10() { 179 | } 180 | 181 | @Test 182 | public void checkNull11() { 183 | } 184 | 185 | @Test 186 | public void checkNull12() { 187 | } 188 | 189 | @Test 190 | public void checkNull13() { 191 | } 192 | 193 | @Test 194 | public void checkNull14() { 195 | } 196 | 197 | @Test 198 | public void checkNull15() { 199 | } 200 | 201 | @Test 202 | public void checkNull16() { 203 | } 204 | 205 | @Test 206 | public void checkNull17() { 207 | } 208 | 209 | @Test 210 | public void checkNull18() { 211 | } 212 | 213 | @Test 214 | public void checkNull19() { 215 | } 216 | 217 | @Test 218 | public void check() { 219 | } 220 | 221 | @Test 222 | public void check1() { 223 | } 224 | 225 | @Test 226 | public void checkNull20() { 227 | } 228 | 229 | @Test 230 | public void isNotEmpty1() { 231 | } 232 | 233 | @Test 234 | public void isEmpty1() { 235 | } 236 | 237 | @Test 238 | public void isNotEmpty2() { 239 | } 240 | 241 | @Test 242 | public void isEmpty2() { 243 | } 244 | 245 | @Test 246 | public void isHyperLink() { 247 | } 248 | 249 | @Test 250 | public void checkDate() { 251 | } 252 | 253 | @Test 254 | public void checkNull21() { 255 | } 256 | 257 | @Test 258 | public void checkNull22() { 259 | } 260 | 261 | @Test 262 | public void isUpperCase() { 263 | assert Checker.isUpperCase("KSSLAL78A--=A"); 264 | assert !Checker.isUpperCase("KLKLAFA-=AAls"); 265 | } 266 | 267 | @Test 268 | public void isOnlyUpperCase() { 269 | for (int i = 0; i < ValueConsts.ONE_HUNDRED_INT; i++) { 270 | assert Checker.isOnlyUpperCase(RandomUtils.getRandomStringOnlyUpperCase(ValueConsts.NINE_INT)); 271 | } 272 | assert !Checker.isOnlyUpperCase(RandomUtils.getRandomStringOnlyLowerCase(ValueConsts.NINE_INT)); 273 | } 274 | 275 | @Test 276 | public void isAjax() { 277 | } 278 | 279 | @Test 280 | public void isNotNullButEmpty() { 281 | } 282 | 283 | @Test 284 | public void getNotNull() { 285 | } 286 | 287 | @Test 288 | public void getNotNullWithException() { 289 | } 290 | 291 | @Test 292 | public void isIn() { 293 | } 294 | 295 | @Test 296 | public void isIn1() { 297 | } 298 | 299 | @Test 300 | public void isNotIn() { 301 | } 302 | 303 | @Test 304 | public void isNotIn1() { 305 | } 306 | 307 | @Test 308 | public void checkBean() { 309 | // 测试NULL 310 | CheckResult errorResult = Checker.checkBean(null); 311 | assert !errorResult.passed; 312 | 313 | TestBean bean = new TestBean(); 314 | bean.none = false; 315 | 316 | // 测试空对象 317 | CheckResult result = Checker.checkBean(bean); 318 | System.out.println(JSONObject.toJSON(result.resultObject)); 319 | assert !result.passed; 320 | 321 | // 测试默认检查 322 | bean.id = 7; 323 | result = Checker.checkBean(bean); 324 | System.out.println(JSONObject.toJSON(result.resultObject)); 325 | assert !result.passed; 326 | 327 | // 测试用户名 328 | bean.defaultChecking = "god"; 329 | bean.username = "pan"; 330 | result = Checker.checkBean(bean); 331 | System.out.println(JSONObject.toJSON(result.resultObject)); 332 | assert !result.passed; 333 | 334 | // 测试邮箱 335 | bean.username = "code4everything"; 336 | bean.email = "invalid@email"; 337 | result = Checker.checkBean(bean); 338 | System.out.println(JSONObject.toJSON(result.resultObject)); 339 | assert !result.passed; 340 | 341 | // 测试年龄 342 | bean.email = "tao@util.org"; 343 | bean.age = 156; 344 | result = Checker.checkBean(bean); 345 | System.out.println(JSONObject.toJSON(result.resultObject)); 346 | assert !result.passed; 347 | 348 | // 测试通过 349 | bean.age = 118; 350 | result = Checker.checkBean(bean); 351 | System.out.println(JSONObject.toJSON(result.resultObject)); 352 | assert result.passed; 353 | 354 | System.out.println(errorResult.resultObject.setData(bean)); 355 | System.out.println(result.resultObject.setData(bean)); 356 | } 357 | 358 | @Test 359 | public void isLetterAndNumber() { 360 | } 361 | 362 | @Test 363 | public void isLetter() { 364 | } 365 | 366 | @Test 367 | public void isLetter1() { 368 | } 369 | 370 | @Test 371 | public void isLowerCase() { 372 | } 373 | 374 | @Test 375 | public void isOnlyLowerCase() { 376 | } 377 | 378 | @Test 379 | public void isLowerCase1() { 380 | } 381 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | org.sonatype.oss 6 | oss-parent 7 | 9 8 | 9 | 10 | 4.0.0 11 | com.zhazhapan 12 | util 13 | 1.1.2 14 | jar 15 | 16 | util 17 | https://github.com/code4everything/util 18 | 19 | 20 | UTF-8 21 | 22 | 23 | 24 | junit 25 | junit 26 | 4.12 27 | 28 | 29 | log4j 30 | log4j 31 | 1.2.17 32 | 33 | 34 | com.google.code.gson 35 | gson 36 | 2.8.2 37 | 38 | 39 | com.alibaba 40 | fastjson 41 | 1.2.47 42 | 43 | 44 | org.apache.commons 45 | commons-lang3 46 | 3.7 47 | 48 | 49 | javax.mail 50 | mail 51 | 1.5.0-b01 52 | 53 | 54 | commons-io 55 | commons-io 56 | 2.6 57 | 58 | 59 | cglib 60 | cglib 61 | 3.2.6 62 | 63 | 64 | javax.servlet 65 | javax.servlet-api 66 | 4.0.0 67 | 68 | 69 | org.apache.poi 70 | poi-ooxml 71 | 3.17 72 | 73 | 74 | cn.hutool 75 | hutool-all 76 | 4.1.18 77 | 78 | 79 | org.jsoup 80 | jsoup 81 | 1.11.2 82 | 83 | 84 | javax.xml 85 | jaxp-api 86 | 1.4.2 87 | 88 | 89 | net.sourceforge.htmlcleaner 90 | htmlcleaner 91 | 2.21 92 | 93 | 94 | org.apache.commons 95 | commons-jexl3 96 | 3.1 97 | 98 | 99 | com.google.guava 100 | guava 101 | 25.1-jre 102 | 103 | 104 | 105 | 106 | 107 | release 108 | 109 | 110 | 111 | 112 | org.apache.maven.plugins 113 | maven-source-plugin 114 | 3.0.0 115 | 116 | 117 | package 118 | 119 | jar-no-fork 120 | 121 | 122 | 123 | 124 | 125 | 126 | org.apache.maven.plugins 127 | maven-javadoc-plugin 128 | 3.0.0-M1 129 | 130 | 131 | package 132 | 133 | jar 134 | 135 | 136 | 137 | 138 | 139 | 140 | org.apache.maven.plugins 141 | maven-gpg-plugin 142 | 1.6 143 | 144 | 145 | verify 146 | 147 | sign 148 | 149 | 150 | 151 | 152 | 153 | org.apache.maven.plugins 154 | maven-compiler-plugin 155 | 3.7.0 156 | 157 | 1.8 158 | 1.8 159 | 160 | 161 | 162 | 163 | 164 | 165 | util 166 | https://oss.sonatype.org/content/repositories/snapshots/ 167 | 168 | 169 | util 170 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | MIT License 179 | https://choosealicense.com/licenses/mit/ 180 | repo 181 | 182 | 183 | 184 | 185 | https://github.com/zhazhapan/util 186 | https://github.com/zhazhapan/util.git 187 | http://zhazhapan.com 188 | 189 | 190 | 191 | 192 | zhazhapan 193 | tao@zhazhapan.com 194 | http://zhazhapan.com 195 | 196 | 197 | 198 | 199 | 200 | 201 | org.apache.maven.plugins 202 | maven-compiler-plugin 203 | 204 | 1.8 205 | 1.8 206 | 207 | 208 | 209 | org.apache.maven.plugins 210 | maven-surefire-plugin 211 | 2.12.4 212 | 213 | true 214 | 215 | 216 | 217 | org.apache.maven.plugins 218 | maven-deploy-plugin 219 | 2.7 220 | 221 | 222 | 223 | 224 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/LoggerUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import cn.hutool.core.util.StrUtil; 4 | import com.zhazhapan.modules.constant.ValueConsts; 5 | import com.zhazhapan.util.enums.LogLevel; 6 | import org.apache.log4j.Logger; 7 | 8 | /** 9 | * @author pantao 10 | * @since 2018/6/8 11 | */ 12 | public class LoggerUtils { 13 | 14 | private static final Class LOCAL_CLASS = LoggerUtils.class; 15 | 16 | private LoggerUtils() {} 17 | 18 | /** 19 | * 获取 {@link Logger} 20 | * 21 | * @return {@link Logger} 22 | * 23 | * @since 1.0.8 24 | */ 25 | public static Logger getLogger() { 26 | return getLogger(LOCAL_CLASS); 27 | } 28 | 29 | /** 30 | * 获取 {@link Logger} 31 | * 32 | * @param object 指定对象 33 | * 34 | * @return {@link Logger} 35 | * 36 | * @since 1.0.8 37 | */ 38 | public static Logger getLogger(Object object) { 39 | return Checker.isNull(object) ? getLogger() : getLogger(object.getClass()); 40 | } 41 | 42 | /** 43 | * 获取 {@link Logger} 44 | * 45 | * @param clazz 类 46 | * 47 | * @return {@link Logger} 48 | * 49 | * @since 1.0.8 50 | */ 51 | public static Logger getLogger(Class clazz) { 52 | return Logger.getLogger(Checker.isNull(clazz) ? LOCAL_CLASS : clazz); 53 | } 54 | 55 | /** 56 | * 信息 57 | * 58 | * @param message 消息 59 | * @param values 格式化参数 60 | * 61 | * @since 1.0.8 62 | */ 63 | public static void info(String message, String... values) { 64 | info(LOCAL_CLASS, message, values); 65 | } 66 | 67 | /** 68 | * 信息 69 | * 70 | * @param object 指定对象 71 | * @param message 消息 72 | * @param values 格式化参数 73 | * 74 | * @since 1.0.8 75 | */ 76 | public static void info(Object object, String message, String... values) { 77 | if (Checker.isNull(object)) { 78 | info(message, values); 79 | } else { 80 | info(object.getClass(), message, values); 81 | } 82 | } 83 | 84 | /** 85 | * 信息 86 | * 87 | * @param clazz 类 88 | * @param message 消息 89 | * @param values 格式化参数 90 | * 91 | * @since 1.0.8 92 | */ 93 | public static void info(Class clazz, String message, String... values) { 94 | getLogger(clazz).info(formatString(message, values)); 95 | } 96 | 97 | /** 98 | * 警告 99 | * 100 | * @param message 消息 101 | * @param values 格式化参数 102 | * 103 | * @since 1.0.8 104 | */ 105 | public static void warn(String message, String... values) { 106 | warn(LOCAL_CLASS, message, values); 107 | } 108 | 109 | /** 110 | * 警告 111 | * 112 | * @param object 指定对象 113 | * @param message 消息 114 | * @param values 格式化参数 115 | * 116 | * @since 1.0.8 117 | */ 118 | public static void warn(Object object, String message, String... values) { 119 | if (Checker.isNull(object)) { 120 | warn(message, values); 121 | } else { 122 | warn(object.getClass(), message, values); 123 | } 124 | } 125 | 126 | /** 127 | * 警告 128 | * 129 | * @param clazz 类 130 | * @param message 消息 131 | * @param values 格式化参数 132 | * 133 | * @since 1.0.8 134 | */ 135 | public static void warn(Class clazz, String message, String... values) { 136 | getLogger(clazz).warn(formatString(message, values)); 137 | } 138 | 139 | /** 140 | * 错误 141 | * 142 | * @param message 消息 143 | * @param values 格式化参数 144 | * 145 | * @since 1.0.8 146 | */ 147 | public static void error(String message, String... values) { 148 | error(LOCAL_CLASS, message, values); 149 | } 150 | 151 | /** 152 | * 错误 153 | * 154 | * @param object 指定对象 155 | * @param message 消息 156 | * @param values 格式化参数 157 | * 158 | * @since 1.0.8 159 | */ 160 | public static void error(Object object, String message, String... values) { 161 | if (Checker.isNull(object)) { 162 | error(message, values); 163 | } else { 164 | error(object.getClass(), message, values); 165 | } 166 | } 167 | 168 | /** 169 | * 错误 170 | * 171 | * @param clazz 类 172 | * @param message 消息 173 | * @param values 格式化参数 174 | * 175 | * @since 1.0.8 176 | */ 177 | public static void error(Class clazz, String message, String... values) { 178 | getLogger(clazz).error(formatString(message, values)); 179 | } 180 | 181 | /** 182 | * 调试 183 | * 184 | * @param message 消息 185 | * @param values 格式化参数 186 | * 187 | * @since 1.0.8 188 | */ 189 | public static void debug(String message, String... values) { 190 | debug(LOCAL_CLASS, message, values); 191 | } 192 | 193 | /** 194 | * 调试 195 | * 196 | * @param object 指定对象 197 | * @param message 消息 198 | * @param values 格式化参数 199 | * 200 | * @since 1.0.8 201 | */ 202 | public static void debug(Object object, String message, String... values) { 203 | if (Checker.isNull(object)) { 204 | debug(message, values); 205 | } else { 206 | debug(object.getClass(), message, values); 207 | } 208 | } 209 | 210 | /** 211 | * 调试 212 | * 213 | * @param clazz 类 214 | * @param message 消息 215 | * @param values 格式化参数 216 | * 217 | * @since 1.0.8 218 | */ 219 | public static void debug(Class clazz, String message, String... values) { 220 | getLogger(clazz).debug(formatString(message, values)); 221 | } 222 | 223 | /** 224 | * 严重错误 225 | * 226 | * @param message 消息 227 | * @param values 格式化参数 228 | * 229 | * @since 1.0.8 230 | */ 231 | public static void fatal(String message, String... values) { 232 | fatal(LOCAL_CLASS, message, values); 233 | } 234 | 235 | /** 236 | * 严重错误 237 | * 238 | * @param object 指定对象 239 | * @param message 消息 240 | * @param values 格式化参数 241 | * 242 | * @since 1.0.8 243 | */ 244 | public static void fatal(Object object, String message, String... values) { 245 | if (Checker.isNull(object)) { 246 | fatal(message, values); 247 | } else { 248 | fatal(object.getClass(), message, values); 249 | } 250 | } 251 | 252 | /** 253 | * 严重错误 254 | * 255 | * @param clazz 类 256 | * @param message 消息 257 | * @param values 格式化参数 258 | * 259 | * @since 1.0.8 260 | */ 261 | public static void fatal(Class clazz, String message, String... values) { 262 | getLogger(clazz).fatal(formatString(message, values)); 263 | } 264 | 265 | /** 266 | * 打印日志 267 | * 268 | * @param message 消息 269 | * @param values 格式化参数 270 | * 271 | * @since 1.0.9 272 | */ 273 | public static void log(String message, String... values) { 274 | log(LogLevel.INFO, message, values); 275 | } 276 | 277 | /** 278 | * 打印日志 279 | * 280 | * @param logLevel 日志级别 281 | * @param message 消息 282 | * @param values 格式化参数 283 | * 284 | * @since 1.0.9 285 | */ 286 | public static void log(LogLevel logLevel, String message, String... values) { 287 | log(logLevel, LOCAL_CLASS, message, values); 288 | } 289 | 290 | /** 291 | * 打印日志 292 | * 293 | * @param logLevel 日志级别 294 | * @param object 指定对象 295 | * @param message 消息 296 | * @param values 格式化参数 297 | * 298 | * @since 1.0.9 299 | */ 300 | public static void log(LogLevel logLevel, Object object, String message, String... values) { 301 | if (Checker.isNull(object)) { 302 | log(logLevel, message, values); 303 | } else { 304 | log(logLevel, object.getClass(), message, values); 305 | } 306 | } 307 | 308 | /** 309 | * 打印日志 310 | * 311 | * @param logLevel 日志级别 312 | * @param clazz 指定类 313 | * @param message 消息 314 | * @param values 格式化参数 315 | * 316 | * @since 1.0.9 317 | */ 318 | public static void log(LogLevel logLevel, Class clazz, String message, String... values) { 319 | switch (logLevel) { 320 | case WARN: 321 | warn(clazz, message, values); 322 | break; 323 | case ERROR: 324 | error(clazz, message, values); 325 | break; 326 | case FATAL: 327 | fatal(clazz, message, values); 328 | break; 329 | default: 330 | info(clazz, message, values); 331 | break; 332 | } 333 | } 334 | 335 | /** 336 | * 格式化字符串 337 | * 338 | * @param message 消息 339 | * @param values 格式化参数 340 | * 341 | * @return 格式化的字符串 342 | * 343 | * @since 1.0.8 344 | */ 345 | private static String formatString(String message, Object[] values) { 346 | if (Checker.isNotEmpty(message)) { 347 | if (Checker.isNotEmpty(values)) { 348 | return StrUtil.format(String.format(message, values), values); 349 | } 350 | return message; 351 | } 352 | return ValueConsts.EMPTY_STRING; 353 | } 354 | } 355 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/dialog/Alerts.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util.dialog; 2 | 3 | import javafx.scene.control.Alert; 4 | import javafx.scene.control.Alert.AlertType; 5 | import javafx.scene.control.ButtonType; 6 | import javafx.scene.control.TextArea; 7 | import javafx.scene.layout.GridPane; 8 | import javafx.scene.layout.Priority; 9 | import javafx.stage.Modality; 10 | import javafx.stage.StageStyle; 11 | import javafx.stage.Window; 12 | 13 | import java.io.PrintWriter; 14 | import java.io.StringWriter; 15 | import java.util.Optional; 16 | 17 | /** 18 | * @author pantao 对JavaFX对话框进行封装 19 | */ 20 | public class Alerts { 21 | 22 | private Alerts() {} 23 | 24 | /** 25 | * 弹出信息框 26 | * 27 | * @param title 标题 28 | * @param content 内容 29 | * 30 | * @return {@link ButtonType} 31 | */ 32 | public static Optional showInformation(String title, String content) { 33 | return showInformation(title, null, content); 34 | } 35 | 36 | /** 37 | * 弹出信息框 38 | * 39 | * @param title 标题 40 | * @param header 信息头 41 | * @param content 内容 42 | * 43 | * @return {@link ButtonType} 44 | */ 45 | public static Optional showInformation(String title, String header, String content) { 46 | return alert(title, header, content, AlertType.INFORMATION); 47 | } 48 | 49 | /** 50 | * 弹出警告框 51 | * 52 | * @param title 标题 53 | * @param content 内容 54 | * 55 | * @return {@link ButtonType} 56 | */ 57 | public static Optional showWarning(String title, String content) { 58 | return showWarning(title, null, content); 59 | } 60 | 61 | /** 62 | * 弹出警告框 63 | * 64 | * @param title 标题 65 | * @param header 信息头 66 | * @param content 内容 67 | * 68 | * @return {@link ButtonType} 69 | */ 70 | public static Optional showWarning(String title, String header, String content) { 71 | return alert(title, header, content, AlertType.WARNING); 72 | } 73 | 74 | /** 75 | * 弹出错误框 76 | * 77 | * @param title 标题 78 | * @param content 内容 79 | * 80 | * @return {@link ButtonType} 81 | */ 82 | public static Optional showError(String title, String content) { 83 | return showError(title, null, content); 84 | } 85 | 86 | /** 87 | * 弹出错误框 88 | * 89 | * @param title 标题 90 | * @param header 信息头 91 | * @param content 内容 92 | * 93 | * @return {@link ButtonType} 94 | */ 95 | public static Optional showError(String title, String header, String content) { 96 | return alert(title, header, content, AlertType.ERROR); 97 | } 98 | 99 | /** 100 | * 弹出确认框 101 | * 102 | * @param title 标题 103 | * @param content 内容 104 | * 105 | * @return {@link ButtonType} 106 | */ 107 | public static Optional showConfirmation(String title, String content) { 108 | return showConfirmation(title, null, content); 109 | } 110 | 111 | /** 112 | * 弹出确认框 113 | * 114 | * @param title 标题 115 | * @param header 信息头 116 | * @param content 内容 117 | * 118 | * @return {@link ButtonType} 119 | */ 120 | public static Optional showConfirmation(String title, String header, String content) { 121 | return alert(title, header, content, AlertType.CONFIRMATION); 122 | } 123 | 124 | /** 125 | * 弹出异常框 126 | * 127 | * @param title 标题 128 | * @param e 异常 129 | * 130 | * @return {@link ButtonType} 131 | */ 132 | public static Optional showException(String title, Exception e) { 133 | return showException(title, null, e); 134 | } 135 | 136 | /** 137 | * 弹出异常框,并退出程序 138 | * 139 | * @param title 标题 140 | * @param header 信息头 141 | * @param e 异常 142 | */ 143 | public static void showFatalError(String title, String header, Exception e) { 144 | showException(title, header, e); 145 | System.exit(0); 146 | } 147 | 148 | /** 149 | * 弹出异常框 150 | * 151 | * @param title 标题 152 | * @param header 信息头 153 | * @param e 异常 154 | * 155 | * @return {@link ButtonType} 156 | */ 157 | public static Optional showException(String title, String header, Exception e) { 158 | Alert alert = getAlert(title, header, "错误信息追踪:", AlertType.ERROR); 159 | 160 | StringWriter stringWriter = new StringWriter(); 161 | PrintWriter printWriter = new PrintWriter(stringWriter); 162 | e.printStackTrace(printWriter); 163 | String exception = stringWriter.toString(); 164 | 165 | TextArea textArea = new TextArea(exception); 166 | textArea.setEditable(false); 167 | textArea.setWrapText(true); 168 | 169 | textArea.setMaxWidth(Double.MAX_VALUE); 170 | textArea.setMaxHeight(Double.MAX_VALUE); 171 | GridPane.setVgrow(textArea, Priority.ALWAYS); 172 | GridPane.setHgrow(textArea, Priority.ALWAYS); 173 | 174 | GridPane gridPane = new GridPane(); 175 | gridPane.setMaxWidth(Double.MAX_VALUE); 176 | gridPane.add(textArea, 0, 0); 177 | 178 | alert.getDialogPane().setExpandableContent(gridPane); 179 | 180 | return alert.showAndWait(); 181 | } 182 | 183 | /** 184 | * 弹窗,header默认为null,alertType默认为{@link AlertType#INFORMATION},modality默认为{@link 185 | * Modality#NONE},window默认为null,style默认为{@link StageStyle#DECORATED} 186 | * 187 | * @param title 标题 188 | * @param content 内容 189 | * 190 | * @return {@link ButtonType} 191 | */ 192 | public static Optional alert(String title, String content) { 193 | return alert(title, null, content); 194 | } 195 | 196 | /** 197 | * 弹窗,header默认为null,modality默认为{@link Modality#NONE},window默认为null,style默认为{@link StageStyle#DECORATED} 198 | * 199 | * @param title 标题 200 | * @param content 内容 201 | * @param alertType {@link AlertType} 202 | * 203 | * @return {@link ButtonType} 204 | */ 205 | public static Optional alert(String title, String content, AlertType alertType) { 206 | return alert(title, null, content, alertType); 207 | } 208 | 209 | /** 210 | * 弹窗,alertType默认为{@link AlertType#INFORMATION},modality默认为{@link Modality#NONE},window默认为null,style默认为{@link 211 | * StageStyle#DECORATED} 212 | * 213 | * @param title 标题 214 | * @param header 信息头 215 | * @param content 内容 216 | * 217 | * @return {@link ButtonType} 218 | */ 219 | public static Optional alert(String title, String header, String content) { 220 | return alert(title, header, content, AlertType.INFORMATION); 221 | } 222 | 223 | /** 224 | * 弹窗,modality默认为{@link Modality#NONE},window默认为null,style默认为{@link StageStyle#DECORATED} 225 | * 226 | * @param title 标题 227 | * @param header 信息头 228 | * @param content 内容 229 | * @param alertType {@link AlertType} 230 | * 231 | * @return {@link ButtonType} 232 | */ 233 | public static Optional alert(String title, String header, String content, AlertType alertType) { 234 | return alert(title, header, content, alertType, Modality.NONE, null, StageStyle.DECORATED); 235 | } 236 | 237 | /** 238 | * 弹窗 239 | * 240 | * @param title 标题 241 | * @param header 信息头 242 | * @param content 内容 243 | * @param alertType {@link AlertType} 244 | * @param modality {@link Modality} 245 | * @param window {@link Window} 246 | * @param style {@link StageStyle} 247 | * 248 | * @return {@link ButtonType} 249 | */ 250 | public static Optional alert(String title, String header, String content, AlertType alertType, 251 | Modality modality, Window window, StageStyle style) { 252 | return getAlert(title, header, content, alertType, modality, window, style).showAndWait(); 253 | } 254 | 255 | /** 256 | * 获取{@link Alert}对象,modality默认为{@link Modality#APPLICATION_MODAL},window默认为null,style默认为{@link 257 | * StageStyle#DECORATED} 258 | * 259 | * @param title 标题 260 | * @param header 信息头 261 | * @param content 内容 262 | * @param alertType {@link AlertType} 263 | * 264 | * @return {@link Alert} 265 | */ 266 | public static Alert getAlert(String title, String header, String content, AlertType alertType) { 267 | return getAlert(title, header, content, alertType, Modality.APPLICATION_MODAL, null, StageStyle.DECORATED); 268 | } 269 | 270 | /** 271 | * 获取{@link Alert}对象 272 | * 273 | * @param title 标题 274 | * @param header 信息头 275 | * @param content 内容 276 | * @param alertType {@link AlertType} 277 | * @param modality {@link Modality} 278 | * @param window {@link Window} 279 | * @param style {@link StageStyle} 280 | * 281 | * @return {@link Alert} 282 | */ 283 | public static Alert getAlert(String title, String header, String content, AlertType alertType, Modality modality, 284 | Window window, StageStyle style) { 285 | Alert alert = new Alert(alertType); 286 | 287 | alert.setTitle(title); 288 | alert.setHeaderText(header); 289 | alert.setContentText(content); 290 | 291 | alert.initModality(modality); 292 | alert.initOwner(window); 293 | alert.initStyle(style); 294 | 295 | return alert; 296 | } 297 | } 298 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/MailSender.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONObject; 5 | import com.sun.mail.util.MailSSLSocketFactory; 6 | 7 | import javax.mail.*; 8 | import javax.mail.internet.InternetAddress; 9 | import javax.mail.internet.MimeMessage; 10 | import java.util.Date; 11 | import java.util.Properties; 12 | 13 | /** 14 | * 发送邮件需要邮箱账号开启POP3/SMTP服务 15 | * 16 | * @author pantao 17 | */ 18 | public class MailSender { 19 | 20 | /** 21 | * 邮件服务器,默认使用QQ服务器 22 | */ 23 | private static String host = "smtp.qq.com"; 24 | 25 | /** 26 | * 个人名称 27 | */ 28 | private static String personal = "personal"; 29 | 30 | /** 31 | * 收件箱 32 | */ 33 | 34 | private static String from = ""; 35 | 36 | /** 37 | * 邮箱密码 38 | */ 39 | private static String key = ""; 40 | 41 | private static boolean sslEnable = true; 42 | 43 | /** 44 | * 邮件服务器端口 45 | */ 46 | private static int port = 0; 47 | 48 | private MailSender() {} 49 | 50 | /** 51 | * 通过JSON配置 52 | * 53 | * @param jsonString {@link String} 54 | */ 55 | public static void config(String jsonString) { 56 | config(JSON.parseObject(jsonString)); 57 | } 58 | 59 | /** 60 | * 通过JSON配置 61 | * 62 | * @param jsonObject {@link JSONObject} 63 | */ 64 | public static void config(JSONObject jsonObject) { 65 | config(jsonObject.getString("host"), jsonObject.getString("personal"), jsonObject.getString("from"), 66 | jsonObject.getString("key"), jsonObject.getInteger("port")); 67 | setSslEnable(jsonObject.getBoolean("ssl")); 68 | } 69 | 70 | /** 71 | * 配置邮箱 72 | * 73 | * @param host 邮件服务器 74 | * @param personal 个人名称 75 | * @param from 发件箱 76 | * @param key 密码 77 | * @param port 端口 78 | */ 79 | public static void config(String host, String personal, String from, String key, int port) { 80 | config(host, personal, from, key); 81 | setPort(port); 82 | } 83 | 84 | /** 85 | * 配置邮箱 86 | * 87 | * @param mailHost 邮件服务器 88 | * @param personal 个人名称 89 | * @param from 发件箱 90 | * @param key 密码 91 | * @param port 端口 92 | */ 93 | public static void config(MailHost mailHost, String personal, String from, String key, int port) { 94 | config(mailHost, personal, from, key); 95 | setPort(port); 96 | } 97 | 98 | /** 99 | * 配置邮箱 100 | * 101 | * @param mailHost 邮件服务器 102 | * @param personal 个人名称 103 | * @param from 发件箱 104 | * @param key 密码 105 | */ 106 | public static void config(MailHost mailHost, String personal, String from, String key) { 107 | setHost(mailHost); 108 | setPersonal(personal); 109 | setFrom(from); 110 | setKey(key); 111 | } 112 | 113 | /** 114 | * 配置邮箱 115 | * 116 | * @param host 邮件服务器 117 | * @param personal 个人名称 118 | * @param from 发件箱 119 | * @param key 密码 120 | */ 121 | public static void config(String host, String personal, String from, String key) { 122 | setHost(host); 123 | setPersonal(personal); 124 | setFrom(from); 125 | setKey(key); 126 | } 127 | 128 | /** 129 | * 发送邮件 130 | * 131 | * @param mailHost 邮件服务器 132 | * @param personal 个人名称 133 | * @param to 收件箱 134 | * @param title 标题 135 | * @param content 内容 136 | * @param from 收件箱 137 | * @param key 密码 138 | * 139 | * @throws Exception 异常 140 | */ 141 | public static void sendMail(MailHost mailHost, String personal, String to, String title, String content, 142 | final String from, final String key) throws Exception { 143 | setHost(mailHost); 144 | setPersonal(personal); 145 | sendMail(to, title, content, from, key); 146 | } 147 | 148 | /** 149 | * 发送邮件 150 | * 151 | * @param host 邮件服务器 152 | * @param personal 个人名称 153 | * @param to 收件箱 154 | * @param title 标题 155 | * @param content 内容 156 | * @param from 收件箱 157 | * @param key 密码 158 | * 159 | * @throws Exception 异常 160 | */ 161 | public static void sendMail(String host, String personal, String to, String title, String content, 162 | final String from, final String key) throws Exception { 163 | setHost(host); 164 | setPersonal(personal); 165 | sendMail(to, title, content, from, key); 166 | } 167 | 168 | /** 169 | * 发送邮件,调用此方法前请先检查邮件服务器是否已经设置,如果没有设置,请先设置{@link MailSender#setHost(String)}, 如不设置将使用默认的QQ邮件服务器 170 | * 171 | * @param to 收件箱 172 | * @param title 标题 173 | * @param content 内容 174 | * @param from 发件箱 175 | * @param key 密码 176 | * 177 | * @throws Exception 异常 178 | */ 179 | public static void sendMail(String to, String title, String content, String from, String key) throws Exception { 180 | setFrom(from); 181 | setKey(key); 182 | sendMail(to, title, content); 183 | } 184 | 185 | /** 186 | * 发送邮件,调用此方法前请先检查邮件服务器是否已经设置,如果没有设置,请先设置{@link MailSender#setHost(String)}, 如不设置将使用默认的QQ邮件服务器 187 | * 188 | * @param to 收件箱 189 | * @param title 标题 190 | * @param content 内容 191 | * 192 | * @throws Exception 异常 193 | */ 194 | public static void sendMail(String to, String title, String content) throws Exception { 195 | if (!Checker.isEmail(to)) { 196 | throw new Exception("this email address is not valid. please check it again"); 197 | } 198 | // 获取系统属性 199 | Properties properties = System.getProperties(); 200 | // 设置邮件服务器 201 | properties.setProperty("mail.smtp.host", host); 202 | if (port > 0) { 203 | properties.setProperty("mail.smtp.port", String.valueOf(port)); 204 | } 205 | properties.put("mail.smtp.auth", "true"); 206 | MailSSLSocketFactory sf; 207 | sf = new MailSSLSocketFactory(); 208 | sf.setTrustAllHosts(true); 209 | properties.put("mail.smtp.ssl.enable", sslEnable); 210 | properties.put("mail.smtp.ssl.socketFactory", sf); 211 | // 获取session对象 212 | Session session = Session.getInstance(properties, new Authenticator() { 213 | @Override 214 | public PasswordAuthentication getPasswordAuthentication() { 215 | // 发件人邮件用户名、密码 216 | return new PasswordAuthentication(from, key); 217 | } 218 | }); 219 | 220 | // 创建默认的MimeMessage对象 221 | MimeMessage message = new MimeMessage(session); 222 | // Set From:头部头字段 223 | message.setFrom(new InternetAddress(from, personal, "UTF-8")); 224 | // Set To:头部头字段 225 | message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 226 | // Set Subject:头部头字段 227 | message.setSubject(title, "UTF-8"); 228 | // 设置消息体 229 | message.setContent(content, "text/html;charset=UTF-8"); 230 | message.setSentDate(new Date()); 231 | // 发送消息 232 | Transport.send(message); 233 | } 234 | 235 | /** 236 | * 获取当前邮件服务器 237 | * 238 | * @return {@link String} 239 | */ 240 | public static String getHost() { 241 | return host; 242 | } 243 | 244 | /** 245 | * 设置邮件服务器 246 | * 247 | * @param host {@link String} 248 | */ 249 | public static void setHost(String host) { 250 | MailSender.host = host; 251 | } 252 | 253 | /** 254 | * 设置邮件服务器 255 | * 256 | * @param mailHost {@link MailHost} 257 | */ 258 | public static void setHost(MailHost mailHost) { 259 | switch (mailHost) { 260 | case NE163: 261 | host = "smtp.163.com"; 262 | break; 263 | case GMAIL: 264 | host = "smtp.gmail.com"; 265 | break; 266 | case SINA: 267 | host = "smtp.sina.com"; 268 | break; 269 | case OUTLOOK: 270 | host = "smtp-mail.outlook.com"; 271 | break; 272 | default: 273 | host = "smtp.qq.com"; 274 | break; 275 | } 276 | } 277 | 278 | /** 279 | * 获取个人名称 280 | * 281 | * @return {@link String} 282 | */ 283 | public static String getPersonal() { 284 | return personal; 285 | } 286 | 287 | /** 288 | * 设置个人名称 289 | * 290 | * @param personal {@link String} 291 | */ 292 | public static void setPersonal(String personal) { 293 | MailSender.personal = personal; 294 | } 295 | 296 | /** 297 | * 获取发件箱 298 | * 299 | * @return 发件箱 300 | */ 301 | public static String getFrom() { 302 | return from; 303 | } 304 | 305 | /** 306 | * 设置发件箱 307 | * 308 | * @param from 发件箱 309 | */ 310 | public static void setFrom(String from) { 311 | MailSender.from = from; 312 | } 313 | 314 | /** 315 | * 获取邮箱密码 316 | * 317 | * @return 密码 318 | */ 319 | public static String getKey() { 320 | return key; 321 | } 322 | 323 | /** 324 | * 设置邮箱密码 325 | * 326 | * @param key 密码 327 | */ 328 | public static void setKey(String key) { 329 | MailSender.key = key; 330 | } 331 | 332 | /** 333 | * 获取端口 334 | * 335 | * @return {@link Integer} 336 | */ 337 | public static int getPort() { 338 | return port; 339 | } 340 | 341 | /** 342 | * 设置端口 343 | * 344 | * @param port {@link Integer} 345 | */ 346 | public static void setPort(int port) { 347 | MailSender.port = port; 348 | } 349 | 350 | /** 351 | * 是否开启SSL 352 | * 353 | * @param sslEnable 是否开启SSL 354 | */ 355 | public static void setSslEnable(boolean sslEnable) { 356 | MailSender.sslEnable = sslEnable; 357 | } 358 | 359 | public enum MailHost { 360 | /** 361 | * QQ邮件服务器 362 | */ 363 | QQ, 364 | 365 | /** 366 | * 网易163邮件服务器 367 | */ 368 | NE163, 369 | 370 | /** 371 | * 谷歌邮件服务器 372 | */ 373 | GMAIL, 374 | 375 | /** 376 | * 新浪邮件服务器 377 | */ 378 | SINA, 379 | 380 | /** 381 | * OutLook邮件服务器 382 | */ 383 | OUTLOOK 384 | } 385 | } 386 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/Utils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.zhazhapan.modules.constant.ValueConsts; 5 | import javafx.scene.input.Clipboard; 6 | import javafx.scene.input.ClipboardContent; 7 | import org.apache.log4j.Logger; 8 | 9 | import java.awt.*; 10 | import java.io.File; 11 | import java.io.IOException; 12 | import java.net.URI; 13 | import java.net.URISyntaxException; 14 | import java.nio.file.Paths; 15 | import java.util.regex.Pattern; 16 | 17 | /** 18 | * @author pantao 19 | */ 20 | public class Utils { 21 | 22 | /** 23 | * 匹配字符串是否有数字 24 | */ 25 | private static final Pattern HAS_DIGIT_PATTERN = Pattern.compile(".*[0-9]+.*"); 26 | 27 | /** 28 | * UID字符集 29 | */ 30 | private static final char[] UID = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray(); 31 | 32 | private static final int UID_LENGTH = UID.length; 33 | 34 | /** 35 | * 日志输出 36 | */ 37 | private static Logger logger = Logger.getLogger(Utils.class); 38 | 39 | private Utils() {} 40 | 41 | /** 42 | * 清除空格 43 | * 44 | * @param string 字符串 45 | * 46 | * @return 清除空格后的字符串 47 | * 48 | * @since 1.1.0 49 | */ 50 | public static String replaceSpace(String string) { 51 | return string.replace(" ", ""); 52 | } 53 | 54 | /** 55 | * 分割long型 56 | * 57 | * @param value {@link Long} 58 | * @param splitPoints 分割点 59 | * 60 | * @return int数组 61 | * 62 | * @since 1.0.9 63 | */ 64 | public static int[] split(long value, int... splitPoints) { 65 | int[] vars = null; 66 | if (Checker.isNotNull(splitPoints)) { 67 | String val = String.valueOf(value); 68 | int len = splitPoints.length; 69 | vars = new int[len + 1]; 70 | for (int i = 0; i < len; i++) { 71 | vars[i] = Integer.parseInt(val.substring(i, splitPoints[i])); 72 | } 73 | vars[len] = Integer.parseInt(val.substring(splitPoints[len - 1])); 74 | } 75 | return vars; 76 | } 77 | 78 | /** 79 | * 数字转uid字符集 80 | * 81 | * @param number 数字集 82 | * 83 | * @return {@link String} 84 | * 85 | * @since 1.0.9 86 | */ 87 | public static String toUidString(int number) { 88 | StringBuilder builder = new StringBuilder(); 89 | while (number > 0) { 90 | builder.append(UID[number % UID_LENGTH]); 91 | number /= UID_LENGTH; 92 | } 93 | return builder.toString(); 94 | } 95 | 96 | /** 97 | * 加载JSON配置文件,使用系统默认编码 98 | * 99 | * @param jsonPath JSON文件路径 100 | * @param clazz 类 101 | * @param 类型值 102 | * 103 | * @return Bean 104 | * 105 | * @throws IOException 异常 106 | * @since 1.0.8 107 | */ 108 | public static T loadJsonToBean(String jsonPath, Class clazz) throws IOException { 109 | return loadJsonToBean(jsonPath, ValueConsts.NULL_STRING, clazz); 110 | } 111 | 112 | /** 113 | * 加载JSON配置文件 114 | * 115 | * @param jsonPath JSON文件路径 116 | * @param encoding 编码格式,为null时使用系统默认编码 117 | * @param clazz 类 118 | * @param 类型值 119 | * 120 | * @return Bean 121 | * 122 | * @throws IOException 异常 123 | * @since 1.0.8 124 | */ 125 | public static T loadJsonToBean(String jsonPath, String encoding, Class clazz) throws IOException { 126 | JSONObject jsonObject = JSONObject.parseObject(FileExecutor.readFileToString(new File(jsonPath), encoding)); 127 | return JSONObject.toJavaObject(jsonObject, clazz); 128 | } 129 | 130 | /** 131 | * 裁剪字符串 132 | * 133 | * @param value 值 134 | * @param trim 需要裁剪的值 135 | * 136 | * @return 裁剪后的字符串 137 | * 138 | * @since 1.0.8 139 | */ 140 | public static String trim(String value, String trim) { 141 | return leftTrim(rightTrim(value, trim), trim); 142 | } 143 | 144 | /** 145 | * 裁剪字符串 146 | * 147 | * @param value 值 148 | * @param leftTrim 需要裁剪的值 149 | * 150 | * @return 裁剪后的字符串 151 | * 152 | * @since 1.0.8 153 | */ 154 | public static String leftTrim(String value, String leftTrim) { 155 | if (Checker.isEmpty(value) || Checker.isEmpty(leftTrim)) { 156 | return value; 157 | } 158 | return value.indexOf(leftTrim) == 0 ? leftTrim(value.substring(leftTrim.length()), leftTrim) : value; 159 | } 160 | 161 | /** 162 | * 裁剪字符串 163 | * 164 | * @param value 值 165 | * @param rightTrim 需要裁剪的值 166 | * 167 | * @return 裁剪后的字符串 168 | * 169 | * @since 1.0.8 170 | */ 171 | public static String rightTrim(String value, String rightTrim) { 172 | if (Checker.isEmpty(value) || Checker.isEmpty(rightTrim)) { 173 | return value; 174 | } 175 | int idx = value.length() - rightTrim.length(); 176 | return value.lastIndexOf(rightTrim) == idx ? rightTrim(value.substring(0, idx), rightTrim) : value; 177 | } 178 | 179 | /** 180 | * 获取当前系统名称 181 | * 182 | * @return {@link String} 183 | */ 184 | public static String getCurrentOS() { 185 | return System.getProperties().getProperty("os.name").toLowerCase(); 186 | } 187 | 188 | /** 189 | * 抽取字符串的数字,并转换为Double 190 | * 191 | * @param string {@link String} 192 | * 193 | * @return {@link Double} 194 | */ 195 | public static double extractDouble(String string) { 196 | return Double.parseDouble(extractDigit(string)); 197 | } 198 | 199 | /** 200 | * 抽取字符串的数字,并转换为Float 201 | * 202 | * @param string {@link String} 203 | * 204 | * @return {@link Float} 205 | */ 206 | public static float extractFloat(String string) { 207 | return Float.parseFloat(extractDigit(string)); 208 | } 209 | 210 | /** 211 | * 抽取字符串的数字,并转换为Short 212 | * 213 | * @param string {@link String} 214 | * 215 | * @return {@link Short} 216 | */ 217 | public static short extractShort(String string) { 218 | return Short.parseShort(extractDigit(string).replace(".", "")); 219 | } 220 | 221 | /** 222 | * 抽取字符串的数字,并转换为Long 223 | * 224 | * @param string {@link String} 225 | * 226 | * @return {@link Long} 227 | */ 228 | public static long extractLong(String string) { 229 | return Long.parseLong(extractDigit(string).replace(".", "")); 230 | } 231 | 232 | /** 233 | * 抽取字符串的数字,并转换为Integer 234 | * 235 | * @param string {@link String} 236 | * 237 | * @return {@link Integer} 238 | */ 239 | public static int extractInt(String string) { 240 | return Integer.parseInt(extractDigit(string).replace(".", "")); 241 | } 242 | 243 | /** 244 | * 抽取字符串的数字(包括最后一个点号) 245 | * 246 | * @param string {@link String} 247 | * 248 | * @return {@link String} 249 | */ 250 | public static String extractDigit(String string) { 251 | StringBuilder res = new StringBuilder(); 252 | if (HAS_DIGIT_PATTERN.matcher(string).matches()) { 253 | string = string.replaceAll("(\\s|[a-zA-Z])+", ""); 254 | res = new StringBuilder(string.indexOf("-") == 0 ? "-" : ""); 255 | int dotIdx = string.lastIndexOf("."); 256 | for (int i = 0; i < string.length(); i++) { 257 | char c = string.charAt(i); 258 | if (Character.isDigit(c) || i == dotIdx) { 259 | res.append(c); 260 | } 261 | } 262 | if (res.indexOf(ValueConsts.DOT_SIGN) == 0) { 263 | res.insert(0, "0"); 264 | } else if (res.indexOf(ValueConsts.NEGATIVE_DOT_SIGN) == 0) { 265 | res = new StringBuilder("-0." + res.substring(2, res.length())); 266 | } 267 | } 268 | return res.toString(); 269 | } 270 | 271 | /** 272 | * 返回多个字符串中长度最长的字符串 273 | * 274 | * @param strings 多个字符串 275 | * 276 | * @return {@link String} 277 | */ 278 | public static String maxLengthString(String... strings) { 279 | String res = ""; 280 | for (String string : strings) { 281 | if (string.length() > res.length()) { 282 | res = string; 283 | } 284 | } 285 | return res; 286 | } 287 | 288 | /** 289 | * 复制字符串至系统剪贴板 290 | * 291 | * @param string 需要复制的字符串 292 | */ 293 | public static void copyToClipboard(String string) { 294 | ClipboardContent content = new ClipboardContent(); 295 | content.putString(string); 296 | Clipboard.getSystemClipboard().setContent(content); 297 | logger.info("copy '" + string + "' to clipboard"); 298 | } 299 | 300 | /** 301 | * 使用系统默认的浏览器打开超链接 302 | * 303 | * @param url 超链接 304 | * 305 | * @throws URISyntaxException 异常 306 | * @throws IOException 异常 307 | */ 308 | public static void openLink(String url) throws IOException, URISyntaxException { 309 | Desktop.getDesktop().browse(new URI(url)); 310 | } 311 | 312 | /** 313 | * 使用系统默认的方式打开文件 314 | * 315 | * @param path 路径 316 | * 317 | * @throws IOException 异常 318 | */ 319 | public static void openFile(String path) throws IOException { 320 | openFile(new File(path)); 321 | } 322 | 323 | /** 324 | * 使用系统默认的方式打开文件 325 | * 326 | * @param file 文件 327 | * 328 | * @throws IOException 异常 329 | */ 330 | public static void openFile(File file) throws IOException { 331 | Desktop.getDesktop().open(file); 332 | } 333 | 334 | /** 335 | * 执行命令 336 | * 337 | * @param command 命令 338 | * 339 | * @throws IOException 异常 340 | * @since 1.0.8 341 | */ 342 | public static void run(String command) throws IOException { 343 | Runtime.getRuntime().exec(command); 344 | } 345 | 346 | /** 347 | * 获取当前工作路径 348 | * 349 | * @return 路径 350 | * 351 | * @since 1.0.8 352 | */ 353 | public static String getCurrentWorkDir() { 354 | return Paths.get(ValueConsts.DOT_SIGN).toAbsolutePath().normalize().toString(); 355 | } 356 | 357 | /** 358 | * 获取数组中最大值 359 | * 360 | * @param nums 数组 361 | * 362 | * @return {@link Integer} 363 | */ 364 | public static int getMaxValue(int... nums) { 365 | int max = 0; 366 | int last = nums.length - 1; 367 | for (int i = 0; i < last; i += ValueConsts.TWO_INT) { 368 | max = Integer.max(max, Integer.max(nums[i], nums[i + 1])); 369 | } 370 | return Integer.max(max, nums[last]); 371 | } 372 | } 373 | -------------------------------------------------------------------------------- /src/main/java/com/zhazhapan/util/ReflectUtils.java: -------------------------------------------------------------------------------- 1 | package com.zhazhapan.util; 2 | 3 | import org.apache.commons.jexl3.JexlContext; 4 | import org.apache.commons.jexl3.JexlEngine; 5 | import org.apache.commons.jexl3.JexlExpression; 6 | import org.apache.commons.jexl3.MapContext; 7 | import org.apache.commons.jexl3.internal.Engine; 8 | import org.apache.log4j.Logger; 9 | 10 | import java.io.File; 11 | import java.io.IOException; 12 | import java.lang.reflect.InvocationTargetException; 13 | import java.lang.reflect.Method; 14 | import java.net.JarURLConnection; 15 | import java.net.URL; 16 | import java.net.URLDecoder; 17 | import java.util.*; 18 | import java.util.jar.JarEntry; 19 | import java.util.jar.JarFile; 20 | 21 | /** 22 | * @author pantao 23 | * @since 2018/1/20 24 | */ 25 | public class ReflectUtils { 26 | 27 | private static Logger logger = Logger.getLogger(ReflectUtils.class); 28 | 29 | private static JexlEngine jexlEngine = new Engine(); 30 | 31 | private ReflectUtils() {} 32 | 33 | /** 34 | * 获取类的所有方法,并封装成集合 35 | * 36 | * @param clazz 类 37 | * 38 | * @return {@link Map} 39 | * 40 | * @since 1.1.1 41 | */ 42 | public static Map getMethodMap(Class clazz) { 43 | return getMethodMap(clazz, null); 44 | } 45 | 46 | /** 47 | * 获取类的方法,并封装成集合 48 | * 49 | * @param clazz 类 50 | * @param prefix 过滤前缀 51 | * 52 | * @return {@link Map} 53 | * 54 | * @since 1.1.1 55 | */ 56 | public static Map getMethodMap(Class clazz, String prefix) { 57 | Method[] methods = clazz.getMethods(); 58 | Map methodMap = new HashMap<>(32); 59 | for (Method method : methods) { 60 | if (Checker.isEmpty(prefix) || method.getName().startsWith(prefix)) { 61 | methodMap.put(method.getName(), method); 62 | } 63 | } 64 | return methodMap; 65 | } 66 | 67 | /** 68 | * 将字符串转成代码,并执行 69 | *

70 | *
怎么使用 71 | * 72 | * @param jexlExpression 代码表达式 73 | * @param map 参数映射 74 | * 75 | * @return 执行结果 76 | * 77 | * @since 1.0.8 78 | */ 79 | public static Object executeExpression(String jexlExpression, Map map) { 80 | JexlExpression expression = jexlEngine.createExpression(jexlExpression); 81 | JexlContext context = new MapContext(); 82 | if (Checker.isNotEmpty(map)) { 83 | map.forEach(context::set); 84 | } 85 | return expression.evaluate(context); 86 | } 87 | 88 | /** 89 | * 调用方法 90 | * 91 | * @param object 对象 92 | * @param methodName 方法名 93 | * @param parameters 参数 94 | * 95 | * @return 方法返回的结果 96 | * 97 | * @throws NoSuchMethodException 异常 98 | * @throws InvocationTargetException 异常 99 | * @throws IllegalAccessException 异常 100 | */ 101 | public static Object invokeMethod(Object object, String methodName, Object[] parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { 102 | return invokeMethod(object, methodName, getTypes(parameters), parameters); 103 | } 104 | 105 | /** 106 | * 调用方法 107 | * 108 | * @param object 对象 109 | * @param methodName 方法名 110 | * @param parameters 参数 111 | * 112 | * @return 方法返回的结果 113 | * 114 | * @throws NoSuchMethodException 异常 115 | * @throws InvocationTargetException 异常 116 | * @throws IllegalAccessException 异常 117 | */ 118 | public static Object invokeMethodUseBasicType(Object object, String methodName, Object[] parameters) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { 119 | return invokeMethod(object, methodName, getBasicTypes(parameters), parameters); 120 | } 121 | 122 | /** 123 | * 调用方法 124 | * 125 | * @param object 对象 126 | * @param methodName 方法名 127 | * @param parameterTypes 参数类型 128 | * @param parameters 参数 129 | * 130 | * @return 方法返回的结果 131 | * 132 | * @throws NoSuchMethodException 异常 133 | * @throws InvocationTargetException 异常 134 | * @throws IllegalAccessException 异常 135 | */ 136 | public static Object invokeMethod(Object object, String methodName, Class[] parameterTypes, 137 | Object[] parameters) throws NoSuchMethodException, InvocationTargetException, 138 | IllegalAccessException { 139 | if (Checker.isEmpty(parameters)) { 140 | return object.getClass().getMethod(methodName).invoke(object); 141 | } else { 142 | return object.getClass().getMethod(methodName, parameterTypes).invoke(object, parameters); 143 | } 144 | } 145 | 146 | /** 147 | * 获取所有对象的基本类型 148 | * 149 | * @param objects 对象 150 | * 151 | * @return 所有对象类型 152 | */ 153 | public static Class[] getBasicTypes(Object[] objects) { 154 | if (!Checker.isNull(objects)) { 155 | Class[] classes = new Class[objects.length]; 156 | int i = 0; 157 | for (Object object : objects) { 158 | if (Checker.isNull(object)) { 159 | classes[i] = null; 160 | } else if (object instanceof Integer) { 161 | classes[i] = int.class; 162 | } else if (object instanceof Long) { 163 | classes[i] = long.class; 164 | } else if (object instanceof Short) { 165 | classes[i] = short.class; 166 | } else if (object instanceof Byte) { 167 | classes[i] = byte.class; 168 | } else if (object instanceof Float) { 169 | classes[i] = float.class; 170 | } else if (object instanceof Double) { 171 | classes[i] = double.class; 172 | } else if (object instanceof Boolean) { 173 | classes[i] = boolean.class; 174 | } else if (object instanceof Character) { 175 | classes[i] = char.class; 176 | } else { 177 | classes[i] = object.getClass(); 178 | } 179 | i++; 180 | } 181 | return classes; 182 | } 183 | return null; 184 | } 185 | 186 | /** 187 | * 获取所有对象类型 188 | * 189 | * @param objects 对象 190 | * 191 | * @return 所有对象类型 192 | */ 193 | public static Class[] getTypes(Object[] objects) { 194 | if (!Checker.isNull(objects)) { 195 | Class[] classes = new Class[objects.length]; 196 | int i = 0; 197 | for (Object object : objects) { 198 | classes[i++] = Checker.isNull(object) ? null : object.getClass(); 199 | } 200 | return classes; 201 | } 202 | return null; 203 | } 204 | 205 | /** 206 | * 扫描包下面的所有类 207 | * 208 | * @param packages 包名 209 | * 210 | * @return {@link List} 211 | * 212 | * @throws IOException 异常 213 | * @throws ClassNotFoundException 异常 214 | */ 215 | public static List> scanPackage(String... packages) throws IOException, ClassNotFoundException { 216 | List> list = new ArrayList<>(); 217 | for (String pkg : packages) { 218 | list.addAll(getClasses(pkg)); 219 | } 220 | return list; 221 | } 222 | 223 | /** 224 | * 从包中获取所有的类 225 | * 226 | * @param packageName 包名 227 | * 228 | * @return {@link List} 229 | * 230 | * @throws IOException 异常 231 | * @throws ClassNotFoundException 异常 232 | */ 233 | public static List> getClasses(String packageName) throws IOException, ClassNotFoundException { 234 | List> classes = new ArrayList<>(); 235 | String packageDirName = packageName.replace('.', '/'); 236 | Enumeration dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName); 237 | while (dirs.hasMoreElements()) { 238 | URL url = dirs.nextElement(); 239 | String protocol = url.getProtocol(); 240 | if ("file".equals(protocol)) { 241 | String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); 242 | addClassesInPackageByFile(packageName, filePath, classes); 243 | } else if ("jar".equals(protocol)) { 244 | JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile(); 245 | Enumeration entries = jar.entries(); 246 | while (entries.hasMoreElements()) { 247 | JarEntry entry = entries.nextElement(); 248 | String name = entry.getName(); 249 | if (name.charAt(0) == '/') { 250 | name = name.substring(1); 251 | } 252 | if (name.startsWith(packageDirName)) { 253 | int idx = name.lastIndexOf('/'); 254 | if (idx != -1) { 255 | packageName = name.substring(0, idx).replace('/', '.'); 256 | } 257 | if (name.endsWith(".class") && !entry.isDirectory()) { 258 | String className = name.substring(packageName.length() + 1, name.length() - 6); 259 | classes.add(Class.forName(packageName + '.' + className)); 260 | } 261 | } 262 | } 263 | } 264 | } 265 | return classes; 266 | } 267 | 268 | /** 269 | * 以文件的形式来获取包下的所有类 270 | * 271 | * @param packageName 包名 272 | * @param packagePath 包路径 273 | * @param classes 文件列表 274 | * 275 | * @throws ClassNotFoundException 异常 276 | */ 277 | public static void addClassesInPackageByFile(String packageName, String packagePath, List> classes) throws ClassNotFoundException { 278 | File dir = new File(packagePath); 279 | if (!dir.exists() || !dir.isDirectory()) { 280 | return; 281 | } 282 | File[] files = dir.listFiles(file -> (file.isDirectory()) || (file.getName().endsWith(".class"))); 283 | if (Checker.isNotNull(files)) { 284 | for (File file : files) { 285 | if (file.isDirectory()) { 286 | addClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), classes); 287 | } else { 288 | String className = file.getName().substring(0, file.getName().length() - 6); 289 | classes.add(Class.forName(packageName + '.' + className)); 290 | } 291 | } 292 | } 293 | } 294 | } 295 | --------------------------------------------------------------------------------