├── .idea ├── vcs.xml ├── encodings.xml ├── smartfox_info.xml ├── kotlinc.xml ├── modules.xml ├── misc.xml ├── libraries │ ├── Maven__ant_ant_1_6_5.xml │ ├── Maven__junit_junit_4_12.xml │ ├── Maven__regexp_regexp_1_3.xml │ ├── Maven__org_yaml_snakeyaml_1_17.xml │ ├── Maven__redis_clients_jedis_2_8_2.xml │ ├── Maven__commons_io_commons_io_2_4.xml │ ├── Maven__com_alibaba_fastjson_1_2_31.xml │ ├── Maven__commons_net_commons_net_3_3.xml │ ├── Maven__org_slf4j_slf4j_api_1_7_25.xml │ ├── Maven__javax_servlet_servlet_api_2_5.xml │ ├── Maven__org_hamcrest_hamcrest_core_1_3.xml │ ├── Maven__org_slf4j_jul_to_slf4j_1_7_21.xml │ ├── Maven__commons_codec_commons_codec_1_10.xml │ ├── Maven__org_modelmapper_modelmapper_0_7_5.xml │ ├── Maven__ch_qos_logback_logback_core_1_1_7.xml │ ├── Maven__org_slf4j_jcl_over_slf4j_1_7_21.xml │ ├── Maven__org_apache_commons_commons_vfs2_2_0.xml │ ├── Maven__org_apache_commons_commons_lang3_3_4.xml │ ├── Maven__org_slf4j_log4j_over_slf4j_1_7_21.xml │ ├── Maven__com_github_jsqlparser_jsqlparser_0_9_5.xml │ ├── Maven__com_github_pagehelper_pagehelper_5_0_0.xml │ ├── Maven__org_apache_maven_scm_maven_scm_api_1_4.xml │ ├── Maven__ch_qos_logback_logback_classic_1_1_7.xml │ ├── Maven__org_apache_commons_commons_pool2_2_4_2.xml │ ├── Maven__org_codehaus_plexus_plexus_utils_1_5_6.xml │ ├── Maven__commons_logging_commons_logging_1_1_1.xml │ ├── Maven__commons_httpclient_commons_httpclient_3_0.xml │ ├── Maven__org_springframework_spring_tx_4_3_4_RELEASE.xml │ ├── Maven__org_codehaus_jackson_jackson_core_asl_1_9_13.xml │ ├── Maven__org_springframework_spring_aop_4_3_4_RELEASE.xml │ ├── Maven__org_springframework_spring_oxm_4_3_4_RELEASE.xml │ ├── Maven__org_springframework_spring_core_4_3_4_RELEASE.xml │ ├── Maven__org_codehaus_jackson_jackson_mapper_asl_1_9_13.xml │ ├── Maven__org_springframework_spring_beans_4_3_4_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_1_4_2_RELEASE.xml │ ├── Maven__org_springframework_spring_context_4_3_4_RELEASE.xml │ ├── Maven__org_apache_maven_scm_maven_scm_provider_svnexe_1_4.xml │ ├── Maven__org_springframework_spring_expression_4_3_4_RELEASE.xml │ ├── Maven__org_apache_maven_scm_maven_scm_provider_svn_commons_1_4.xml │ ├── Maven__org_springframework_data_spring_data_redis_1_7_5_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_1_4_2_RELEASE.xml │ ├── Maven__org_springframework_spring_context_support_4_3_4_RELEASE.xml │ ├── Maven__org_springframework_data_spring_data_commons_1_12_5_RELEASE.xml │ ├── Maven__org_springframework_data_spring_data_keyvalue_1_1_5_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_autoconfigure_1_4_2_RELEASE.xml │ ├── Maven__org_springframework_boot_spring_boot_starter_redis_1_4_2_RELEASE.xml │ └── Maven__org_springframework_boot_spring_boot_starter_logging_1_4_2_RELEASE.xml ├── compiler.xml ├── inspectionProfiles │ └── Project_Default.xml └── uiDesigner.xml ├── .gitignore ├── src ├── test │ └── java │ │ ├── u4encrypt │ │ └── U4Md5Test.java │ │ ├── u4enum │ │ └── U4EnumTest.java │ │ ├── u4integer │ │ └── U4IntegerUtilsTest.java │ │ ├── u4object │ │ └── U4ObjectTest.java │ │ ├── u4entity │ │ ├── U4EntityTest.java │ │ ├── Person.java │ │ └── Dto.java │ │ ├── u4double │ │ └── U4DoubleTest.java │ │ ├── u4decimal │ │ └── U4BigDecimalTest.java │ │ ├── u4list │ │ └── U4ListTest.java │ │ ├── u4common │ │ └── U4FtpUtils.java │ │ ├── u4datetime │ │ └── U4DatetimeTest.java │ │ ├── u4string │ │ └── U4StringTest.java │ │ ├── u4thread │ │ └── U4ThreadTest.java │ │ └── u4json │ │ └── U4JsonTest.java └── main │ └── java │ └── com │ └── git │ └── comm │ └── utils │ ├── u4entity │ └── EntityUtils.java │ ├── u4string │ ├── NumberUtil.java │ ├── StringUtils.java │ └── ChinaUpperCaseUtil.java │ ├── u4integer │ └── IntegerUtils.java │ ├── xss │ ├── XSSFilter.java │ ├── XssHttpServletRequestWrapper.java │ └── XssShieldUtil.java │ ├── u4object │ └── ObjectUtils.java │ ├── u4file │ ├── FileZipUtils.java │ └── StreamUtils.java │ ├── u4encrypt │ └── MD5Utils.java │ ├── u4json │ ├── WrapMapper.java │ ├── Wrapper.java │ └── JsonUtils.java │ ├── designPattern │ └── Address.java │ ├── u4db │ ├── RedisOperationUtils.java │ └── RedisUtils.java │ ├── u4thread │ └── ThreadLocalMap.java │ ├── u4enum │ └── BusinessEnum.java │ ├── u4net │ ├── IpUtils.java │ ├── CookieUtils.java │ └── HttpClientUtil.java │ ├── u4double │ └── DoubleUtils.java │ ├── u4os │ ├── BareBonesBrowserLaunch.java │ └── LinuxSystemTool.java │ ├── u4list │ └── ListUtils.java │ ├── u4common │ ├── FtpUtils.java │ ├── DingDingMessageUtil.java │ └── PropertiesCacheUtils.java │ ├── u4img │ └── ImageUtils.java │ ├── u4decimal │ └── BigDemicalUtils.java │ └── u4datetime │ └── DatetimeUtils.java ├── LICENSE ├── README.md ├── pom.xml └── commutils4j.iml /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/smartfox_info.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # maven ignore 2 | target/ 3 | .metadata/ 4 | *.jar 5 | *.war 6 | *.zip 7 | *.tar 8 | *.tar.gz 9 | 10 | # eclipse ignore 11 | .settings/ 12 | .project 13 | .classpath 14 | logs 15 | .mvn 16 | .springBeans 17 | 18 | # idea ignore 19 | .idea/ 20 | *.ipr 21 | *.iml 22 | *.iws 23 | mvn* 24 | .apt_generated/ -------------------------------------------------------------------------------- /src/test/java/u4encrypt/U4Md5Test.java: -------------------------------------------------------------------------------- 1 | package u4encrypt; 2 | 3 | import com.git.comm.utils.u4encrypt.MD5Utils; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by dragon on 8/20/2017. 8 | */ 9 | public class U4Md5Test { 10 | 11 | MD5Utils md5Utils = new MD5Utils(); 12 | 13 | @Test 14 | public void test(){ 15 | // 32位密文 16 | String md5Str = md5Utils.encryption("this is pwd"); 17 | System.out.println(md5Str); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/u4enum/U4EnumTest.java: -------------------------------------------------------------------------------- 1 | package u4enum; 2 | 3 | import com.git.comm.utils.u4enum.BusinessEnum; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by dragon on 12/6/2017. 8 | */ 9 | public class U4EnumTest { 10 | @Test 11 | public void test() { 12 | // getList 13 | System.out.println(BusinessEnum.getList().toString()); 14 | // getMap2List 15 | System.out.println(BusinessEnum.getMap2List().toString()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4entity/EntityUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4entity; 2 | 3 | import org.modelmapper.ModelMapper; 4 | 5 | public class EntityUtils { 6 | 7 | /** 8 | * 将dto映射为实体类 9 | * @param dto 10 | * @param entity 11 | * @return 12 | */ 13 | public static Object dtoToEntity(Object dto, Object entity){ 14 | ModelMapper modelMapper = new ModelMapper(); 15 | return modelMapper.map(dto, entity.getClass()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/u4integer/U4IntegerUtilsTest.java: -------------------------------------------------------------------------------- 1 | package u4integer; 2 | 3 | import com.git.comm.utils.u4integer.IntegerUtils; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by dragon on 8/20/2017. 8 | */ 9 | public class U4IntegerUtilsTest { 10 | 11 | @Test 12 | public void test(){ 13 | System.out.println(IntegerUtils.getChinaNumber(6)); 14 | System.out.println(IntegerUtils.parseInt("987")); 15 | System.out.println(IntegerUtils.parseInt("noInt")); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__ant_ant_1_6_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/test/java/u4object/U4ObjectTest.java: -------------------------------------------------------------------------------- 1 | package u4object; 2 | 3 | import com.git.comm.utils.u4object.ObjectUtils; 4 | import org.junit.Test; 5 | import u4entity.Person; 6 | 7 | /** 8 | * Created by dragon on 11/16/2017. 9 | */ 10 | public class U4ObjectTest { 11 | 12 | @Test 13 | public void test() { 14 | Person person = new Person(); 15 | person.setName("dragon"); 16 | person.setAge("22"); 17 | person.setSex("man"); 18 | ObjectUtils.getSonFromObject(person); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__junit_junit_4_12.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__regexp_regexp_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_yaml_snakeyaml_1_17.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__redis_clients_jedis_2_8_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_io_commons_io_2_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_alibaba_fastjson_1_2_31.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_net_commons_net_3_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_25.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/test/java/u4entity/U4EntityTest.java: -------------------------------------------------------------------------------- 1 | package u4entity; 2 | 3 | import com.git.comm.utils.u4entity.EntityUtils; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by dragon on 10/4/2017. 8 | */ 9 | public class U4EntityTest { 10 | 11 | @Test 12 | public void test(){ 13 | // 将dto映射为实体类 14 | Dto dto = new Dto(); 15 | dto.setName("testName"); 16 | dto.setAge("11"); 17 | dto.setSex("man"); 18 | dto.setPhone("133333"); 19 | dto.setEmail("123@123.123"); 20 | System.out.println(EntityUtils.dtoToEntity(dto, new Person()).toString()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__javax_servlet_servlet_api_2_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_slf4j_jul_to_slf4j_1_7_21.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4string/NumberUtil.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4string; 2 | 3 | /** 4 | * Created by dragon on 12/5/2017. 5 | */ 6 | public class NumberUtil { 7 | /** 8 | * 二十以下的转换 9 | * @param i 10 | * @return 11 | */ 12 | public static String getChinaNumber(int i){ 13 | String number1 = "零一二三四五六七八九"; 14 | String result = ""; 15 | if(i < 10){ 16 | result = number1.substring(i, i + 1); 17 | }else if(i >= 10 && i < 20){ 18 | result = "十" + number1.substring(i - 10, i -10 + 1); 19 | } 20 | return result; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_codec_commons_codec_1_10.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_modelmapper_modelmapper_0_7_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__ch_qos_logback_logback_core_1_1_7.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_slf4j_jcl_over_slf4j_1_7_21.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_commons_commons_vfs2_2_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_commons_commons_lang3_3_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_slf4j_log4j_over_slf4j_1_7_21.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_github_jsqlparser_jsqlparser_0_9_5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__com_github_pagehelper_pagehelper_5_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_maven_scm_maven_scm_api_1_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__ch_qos_logback_logback_classic_1_1_7.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_commons_commons_pool2_2_4_2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_codehaus_plexus_plexus_utils_1_5_6.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_logging_commons_logging_1_1_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__commons_httpclient_commons_httpclient_3_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/test/java/u4double/U4DoubleTest.java: -------------------------------------------------------------------------------- 1 | package u4double; 2 | 3 | import com.git.comm.utils.u4double.DoubleUtils; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by dragon on 8/21/2017. 8 | */ 9 | public class U4DoubleTest { 10 | 11 | @Test 12 | public void test(){ 13 | // 将double类型的数字保留两位小数 14 | System.out.println(DoubleUtils.formatNumber(3.1415926)); 15 | // 加法 16 | System.out.println(DoubleUtils.add(1.1,2.2)); 17 | // 减法 18 | System.out.println(DoubleUtils.sub(5,0.9)); 19 | // 乘法 20 | System.out.println(DoubleUtils.mul(3.3,0.1)); 21 | // 除法 22 | System.out.println(DoubleUtils.div(5,0.5)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_tx_4_3_4_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_codehaus_jackson_jackson_core_asl_1_9_13.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_aop_4_3_4_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_oxm_4_3_4_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_core_4_3_4_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_codehaus_jackson_jackson_mapper_asl_1_9_13.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_beans_4_3_4_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/test/java/u4decimal/U4BigDecimalTest.java: -------------------------------------------------------------------------------- 1 | package u4decimal; 2 | 3 | import com.git.comm.utils.u4decimal.BigDemicalUtils; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by dragon on 6/21/2018. 8 | */ 9 | public class U4BigDecimalTest { 10 | @Test 11 | public void test() throws IllegalAccessException { 12 | // 提供精确加法计算的add方法 13 | System.out.println(BigDemicalUtils.add(1.11, 2.22)); 14 | // 提供精确减法运算的sub方法 15 | System.out.println(BigDemicalUtils.sub(1.11, 2.22)); 16 | // 提供精确乘法运算的mul方法 17 | System.out.println(BigDemicalUtils.mul(1.11, 2.22)); 18 | // 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指定精度,以后的数字四舍五入。 19 | System.out.println(BigDemicalUtils.div(1.11, 2.22, 3)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_1_4_2_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_context_4_3_4_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_maven_scm_maven_scm_provider_svnexe_1_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4integer/IntegerUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4integer; 2 | 3 | public class IntegerUtils { 4 | 5 | // 数字转换汉字 6 | public static String getChinaNumber(int i){ 7 | String number1 = "零一二三四五六七八九"; 8 | String result = ""; 9 | if(i < 10){ 10 | result = number1.substring(i, i + 1); 11 | }else if(i >= 10 && i < 20){ 12 | result = "十" + number1.substring(i - 10, i -10 + 1); 13 | } 14 | return result; 15 | } 16 | 17 | // String 类型转换 Integer 18 | public static int parseInt(String str) { 19 | try { 20 | return Integer.parseInt(str); 21 | } catch (Exception e) { 22 | return 0; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_expression_4_3_4_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_apache_maven_scm_maven_scm_provider_svn_commons_1_4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_data_spring_data_redis_1_7_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_1_4_2_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_spring_context_support_4_3_4_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/xss/XSSFilter.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.xss; 2 | 3 | import javax.servlet.*; 4 | import javax.servlet.http.HttpServletRequest; 5 | import java.io.IOException; 6 | 7 | public class XSSFilter implements Filter { 8 | 9 | @Override 10 | public void init(FilterConfig filterConfig) throws ServletException { 11 | } 12 | 13 | @Override 14 | public void doFilter(ServletRequest request, ServletResponse response, 15 | FilterChain chain) throws IOException, ServletException { 16 | XssHttpServletRequestWrapper xssRequest = new XssHttpServletRequestWrapper( 17 | (HttpServletRequest) request); 18 | chain.doFilter(xssRequest, response); 19 | } 20 | 21 | @Override 22 | public void destroy() { 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_data_spring_data_commons_1_12_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_data_spring_data_keyvalue_1_1_5_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_autoconfigure_1_4_2_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_redis_1_4_2_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/test/java/u4list/U4ListTest.java: -------------------------------------------------------------------------------- 1 | package u4list; 2 | 3 | import com.git.comm.utils.u4list.ListUtils; 4 | import org.junit.Test; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * Created by dragon on 8/22/2017. 11 | */ 12 | public class U4ListTest { 13 | 14 | @Test 15 | public void test(){ 16 | // 判断集合是否为空 17 | List stringList = new ArrayList(); 18 | System.out.println(ListUtils.isEmpty(stringList)); 19 | stringList.add("has"); 20 | System.out.println(ListUtils.isEmpty(stringList)); 21 | 22 | // 拆分list,按500条拆分 23 | List strings = new ArrayList(); 24 | for (int i = 0; i < 1000; i++){ 25 | strings.add(String.valueOf(i)); 26 | } 27 | System.out.println(ListUtils.splitList(strings)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__org_springframework_boot_spring_boot_starter_logging_1_4_2_RELEASE.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4object/ObjectUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4object; 2 | 3 | import java.lang.reflect.Field; 4 | import java.lang.reflect.Method; 5 | 6 | public class ObjectUtils { 7 | 8 | /** 9 | * 通过反射,遍历对象中各参数的值 10 | * @param obj 11 | */ 12 | public static void getSonFromObject(Object obj){ 13 | try { 14 | Class objClass = obj.getClass(); 15 | Field[] fields = objClass.getDeclaredFields(); 16 | for (int k = 0; k < fields.length; k++) { 17 | String fieldName = fields[k].getName(); 18 | Method m = obj.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1)); 19 | Object value = m.invoke(obj); 20 | System.out.println(fieldName + " : " + value); 21 | } 22 | }catch (Exception e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/u4entity/Person.java: -------------------------------------------------------------------------------- 1 | package u4entity; 2 | 3 | /** 4 | * Created by dragon on 8/23/2017. 5 | */ 6 | public class Person { 7 | 8 | private String name; 9 | private String age; 10 | private String sex; 11 | 12 | public String getName() { 13 | return name; 14 | } 15 | 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | 20 | public String getAge() { 21 | return age; 22 | } 23 | 24 | public void setAge(String age) { 25 | this.age = age; 26 | } 27 | 28 | public String getSex() { 29 | return sex; 30 | } 31 | 32 | public void setSex(String sex) { 33 | this.sex = sex; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "Person{" + 39 | "name='" + name + '\'' + 40 | ", age='" + age + '\'' + 41 | ", sex='" + sex + '\'' + 42 | '}'; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/u4common/U4FtpUtils.java: -------------------------------------------------------------------------------- 1 | package u4common; 2 | 3 | import com.git.comm.utils.u4common.FtpUtils; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by dragon on 10/16/2017. 8 | */ 9 | public class U4FtpUtils { 10 | 11 | private final String downloadFtpName = ""; 12 | private final String downloadFtpPwd = ""; 13 | private final String downloadFtpUrl = ""; 14 | private final String downloadFtpFolder = ""; 15 | 16 | private final String uploadFtpName = ""; 17 | private final String uploadFtpPwd = ""; 18 | private final String uploadFtpUrl = ""; 19 | private final String uploadFtpFolder = ""; 20 | 21 | @Test 22 | public void test() throws Exception { 23 | String sourcePath = "ftp://" + downloadFtpName + ":" + downloadFtpPwd + "@" + downloadFtpUrl + downloadFtpFolder; 24 | String targetPath = "ftp://" + uploadFtpName + ":" + uploadFtpPwd + "@" + uploadFtpUrl + uploadFtpFolder; 25 | FtpUtils.copyFile(sourcePath, targetPath); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4file/FileZipUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4file; 2 | 3 | import org.apache.tools.ant.Project; 4 | import org.apache.tools.ant.taskdefs.Zip; 5 | import org.apache.tools.ant.types.FileSet; 6 | import java.io.File; 7 | 8 | public class FileZipUtils { 9 | 10 | /** 11 | * 12 | * @param srcPath 源文件 13 | * @param desPath 目标文件 14 | */ 15 | public static void compress(String srcPath, String desPath){ 16 | File srcFile = new File(srcPath);// 源文件 17 | File desFile = new File(desPath);// 目标文件 18 | if (!srcFile.exists()){ 19 | throw new RuntimeException("要压缩的文件不存在"); 20 | } 21 | Project project = new Project(); 22 | Zip zip = new Zip(); 23 | zip.setProject(project); 24 | zip.setDestFile(desFile); 25 | FileSet fileSet = new FileSet(); 26 | fileSet.setProject(project); 27 | fileSet.setDir(srcFile); 28 | zip.addFileset(fileSet); 29 | zip.execute(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/u4entity/Dto.java: -------------------------------------------------------------------------------- 1 | package u4entity; 2 | 3 | /** 4 | * Created by dragon on 10/4/2017. 5 | */ 6 | public class Dto { 7 | 8 | private String name; 9 | private String age; 10 | private String sex; 11 | private String phone; 12 | private String email; 13 | 14 | public String getName() { 15 | return name; 16 | } 17 | 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | 22 | public String getAge() { 23 | return age; 24 | } 25 | 26 | public void setAge(String age) { 27 | this.age = age; 28 | } 29 | 30 | public String getSex() { 31 | return sex; 32 | } 33 | 34 | public void setSex(String sex) { 35 | this.sex = sex; 36 | } 37 | 38 | public String getPhone() { 39 | return phone; 40 | } 41 | 42 | public void setPhone(String phone) { 43 | this.phone = phone; 44 | } 45 | 46 | public String getEmail() { 47 | return email; 48 | } 49 | 50 | public void setEmail(String email) { 51 | this.email = email; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4encrypt/MD5Utils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4encrypt; 2 | 3 | import java.security.MessageDigest; 4 | import java.security.NoSuchAlgorithmException; 5 | 6 | public class MD5Utils { 7 | /** 8 | * 9 | * @param plainText 明文 10 | * @return 32位密文 11 | */ 12 | public String encryption(String plainText) { 13 | String re_md5 = new String(); 14 | try { 15 | MessageDigest md = MessageDigest.getInstance("MD5"); 16 | md.update(plainText.getBytes()); 17 | byte b[] = md.digest(); 18 | int i; 19 | StringBuffer buf = new StringBuffer(""); 20 | for (int offset = 0; offset < b.length; offset++) { 21 | i = b[offset]; 22 | if (i < 0) 23 | i += 256; 24 | if (i < 16) 25 | buf.append("0"); 26 | buf.append(Integer.toHexString(i)); 27 | } 28 | re_md5 = buf.toString(); 29 | } catch (NoSuchAlgorithmException e) { 30 | e.printStackTrace(); 31 | } 32 | return re_md5; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) DragonYuan (http://dragon-yuan.me/) (http://loong.moe/) 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/test/java/u4datetime/U4DatetimeTest.java: -------------------------------------------------------------------------------- 1 | package u4datetime; 2 | 3 | import com.git.comm.utils.u4datetime.DatetimeUtils; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by dragon on 8/21/2017. 8 | */ 9 | public class U4DatetimeTest { 10 | 11 | @Test 12 | public void test(){ 13 | // 获取当前精确时间 yyyy-MM-dd HH:mm:ss 14 | System.out.println(DatetimeUtils.getNowTimeYMDHMS()); 15 | // 获取当前日期 yyyy-MM-dd 16 | System.out.println(DatetimeUtils.getNowTimeYMD()); 17 | // 获取当前日期 HH:mm:ss 18 | System.out.println(DatetimeUtils.getNowTimeHMS()); 19 | // 形如yyyy-MM-dd HH:mm:ss补足转换成yyyMMddHHmmssSS 20 | System.out.println(DatetimeUtils.stringDataPattern(DatetimeUtils.getNowTimeYMDHMS())); 21 | // 形如yyyyMMddHHmmssSS转换成yyyy-MM-dd HH:mm:ss 22 | System.out.println(DatetimeUtils.stringDataPatternSecond(DatetimeUtils.stringDataPattern(DatetimeUtils.getNowTimeYMDHMS()))); 23 | // 计算剩余时间 24 | System.out.println(DatetimeUtils.dateCount(DatetimeUtils.getNowTimeYMDHMS(),DatetimeUtils.getNowTimeYMDHMS())); 25 | // 时间转换中文 26 | System.out.println(DatetimeUtils.getChinaDateSec(DatetimeUtils.getNowTimeYMDHMS())); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4json/WrapMapper.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4json; 2 | 3 | import com.github.pagehelper.Page; 4 | 5 | public class WrapMapper { 6 | private WrapMapper() { 7 | } 8 | 9 | public static Wrapper wrap(int code, String message, E o, Page page) { 10 | return new Wrapper(code, message, o, page); 11 | } 12 | 13 | public static Wrapper wrap(int code, String message, E o) { 14 | return new Wrapper(code, message, o); 15 | } 16 | 17 | public static Wrapper wrap(int code, String message) { 18 | return new Wrapper(code, message); 19 | } 20 | 21 | public static Wrapper wrap(int code) { 22 | return wrap(code, (String)null); 23 | } 24 | 25 | public static Wrapper wrap(Exception e) { 26 | return new Wrapper(500, e.getMessage()); 27 | } 28 | 29 | public static E unWrap(Wrapper wrapper) { 30 | return wrapper.getResult(); 31 | } 32 | 33 | public static Wrapper illegalArgument() { 34 | return wrap(100, "参数非法"); 35 | } 36 | 37 | public static Wrapper error() { 38 | return wrap(500, "内部异常"); 39 | } 40 | 41 | public static Wrapper ok() { 42 | return new Wrapper(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/u4string/U4StringTest.java: -------------------------------------------------------------------------------- 1 | package u4string; 2 | 3 | import com.git.comm.utils.u4string.NumberUtil; 4 | import com.git.comm.utils.u4string.StringUtils; 5 | import org.junit.Test; 6 | 7 | /** 8 | * Created by dragon on 8/20/2017. 9 | */ 10 | public class U4StringTest { 11 | 12 | @Test 13 | public void test(){ 14 | // isEmpty 15 | System.out.println(StringUtils.isEmpty("")); 16 | System.out.println(StringUtils.isEmpty("notNull")); 17 | // isSNullOrEmpty 18 | System.out.println(StringUtils.isSNullOrEmpty("")); 19 | System.out.println(StringUtils.isSNullOrEmpty("notNull")); 20 | // isStrsEmptyOrNull 21 | System.out.println(StringUtils.isStrsEmptyOrNull("first","second")); 22 | System.out.println(StringUtils.isStrsEmptyOrNull("first","")); 23 | // getUUID 24 | System.out.println(StringUtils.getUUID()); 25 | // utf8Encode 26 | System.out.println(StringUtils.utf8Encode("utf8")); 27 | // getHrefInnerHtml 28 | System.out.println(StringUtils.getHrefInnerHtml("http://github.com")); 29 | // trimString 30 | System.out.println(StringUtils.trimString("This is a test string.", 12)); 31 | // NumberUtil-getChinaNumber 32 | System.out.println(NumberUtil.getChinaNumber(98)); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/u4thread/U4ThreadTest.java: -------------------------------------------------------------------------------- 1 | package u4thread; 2 | 3 | import com.git.comm.utils.u4thread.ThreadLocalMap; 4 | import org.junit.*; 5 | 6 | /** 7 | * Created by dragon on 12/8/2017. 8 | */ 9 | public class U4ThreadTest { 10 | 11 | @BeforeClass 12 | public static void beforeClass() { 13 | ThreadLocalMap.put("BeforeClass", "This is BeforeClass"); 14 | System.out.println(ThreadLocalMap.get("BeforeClass")); 15 | } 16 | 17 | @Before 18 | public void before() { 19 | ThreadLocalMap.put("Before", "This is Before"); 20 | System.out.println(ThreadLocalMap.get("Before")); 21 | } 22 | 23 | @Test 24 | public void test() { 25 | ThreadLocalMap.put("Test", "This is Test"); 26 | System.out.println(ThreadLocalMap.get("Test")); 27 | } 28 | 29 | @After 30 | public void after() { 31 | ThreadLocalMap.put("After", "This is After"); 32 | System.out.println(ThreadLocalMap.get("After")); 33 | } 34 | 35 | @AfterClass 36 | public static void afterClass() { 37 | ThreadLocalMap.reset(); 38 | System.out.println(ThreadLocalMap.get("BeforeClass")); 39 | System.out.println(ThreadLocalMap.get("Before")); 40 | System.out.println(ThreadLocalMap.get("Test")); 41 | System.out.println(ThreadLocalMap.get("After")); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4file/StreamUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4file; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import java.io.*; 5 | 6 | /** 7 | * Created by dragon on 1/31/2018. 8 | */ 9 | public class StreamUtils { 10 | /** 11 | * 将InputStream转换成byte[] 12 | * 13 | * @param inputStream inputStream 14 | * @return byte[] 15 | * @throws IOException 16 | */ 17 | public static byte[] convertInputStream2ByteArray(InputStream inputStream) throws IOException { 18 | 19 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 20 | 21 | byte[] buffer = new byte[1024]; 22 | int len; 23 | 24 | while ((len = inputStream.read(buffer)) > -1) { 25 | byteArrayOutputStream.write(buffer, 0, len); 26 | } 27 | 28 | byteArrayOutputStream.flush(); 29 | return byteArrayOutputStream.toByteArray(); 30 | } 31 | 32 | /** 33 | * 将OutPutStream转换成inputStream 34 | * @param outputStream outputStream 35 | * @return ByteArrayInputStream 36 | */ 37 | public static ByteArrayInputStream convertOutputStream2InputStream(OutputStream outputStream) { 38 | ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) outputStream; 39 | return new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/designPattern/Address.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.designPattern; 2 | 3 | /** 4 | * 设计模式-Builder设计模式 5 | */ 6 | public class Address { 7 | private final String province; 8 | private final String city; 9 | private final String area; 10 | private final String street; 11 | 12 | public static class Builder { 13 | private String province; 14 | private String city; 15 | private String area; 16 | private String street; 17 | 18 | public Builder() { 19 | 20 | } 21 | 22 | public Builder province(String province) { 23 | this.province = province; 24 | return this; 25 | } 26 | 27 | public Builder city(String city) { 28 | this.city = city; 29 | return this; 30 | } 31 | 32 | public Builder area(String area) { 33 | this.area = area; 34 | return this; 35 | } 36 | 37 | public Builder street(String street) { 38 | this.street = street; 39 | return this; 40 | } 41 | 42 | public Address build() { 43 | return new Address(this); 44 | } 45 | } 46 | 47 | private Address(Builder builder) { 48 | province = builder.province; 49 | city = builder.city; 50 | area = builder.area; 51 | street = builder.street; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4db/RedisOperationUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4db; 2 | 3 | import org.springframework.data.redis.core.StringRedisTemplate; 4 | import org.springframework.data.redis.core.ValueOperations; 5 | 6 | import javax.annotation.Resource; 7 | import java.util.concurrent.TimeUnit; 8 | 9 | /** 10 | * Created by dragon on 12/24/2017. 11 | */ 12 | public class RedisOperationUtils { 13 | @Resource 14 | private StringRedisTemplate stringRedisTemplate; 15 | 16 | public void set(String key, String value){ 17 | ValueOperations valueOps = stringRedisTemplate.opsForValue(); 18 | valueOps.set(key,value); 19 | } 20 | public void set(String key,String value, Long time, TimeUnit timeUnit){ 21 | ValueOperations valueOps = stringRedisTemplate.opsForValue(); 22 | valueOps.set(key,value,time, timeUnit); 23 | } 24 | 25 | 26 | public void increment(String key, Long value){ 27 | ValueOperations valueOps = stringRedisTemplate.opsForValue(); 28 | valueOps.increment(key,value); 29 | } 30 | 31 | public boolean hasKey(String key){ 32 | return stringRedisTemplate.hasKey(key); 33 | } 34 | 35 | public Long getValue(String key){ 36 | ValueOperations valueOps = stringRedisTemplate.opsForValue(); 37 | return Long.parseLong(valueOps.get(key)); 38 | } 39 | 40 | public void deleteKey(String key){ 41 | stringRedisTemplate.delete(key); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4thread/ThreadLocalMap.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4thread; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class ThreadLocalMap { 7 | 8 | protected final static ThreadLocal> threadContext = new MapThreadLocal(); 9 | 10 | private ThreadLocalMap() { 11 | }; 12 | 13 | public static void put(String key, Object value) { 14 | getContextMap().put(key, value); 15 | } 16 | 17 | public static Object remove(String key) { 18 | return getContextMap().remove(key); 19 | } 20 | 21 | public static Object get(String key) { 22 | return getContextMap().get(key); 23 | } 24 | 25 | private static class MapThreadLocal extends ThreadLocal> { 26 | protected Map initialValue() { 27 | return new HashMap() { 28 | private static final long serialVersionUID = 3637958959138295593L; 29 | public Object put(String key, Object value) { 30 | return super.put(key, value); 31 | } 32 | }; 33 | } 34 | } 35 | 36 | /** 37 | * 取得thread context Map的实例。 38 | * 39 | * @return thread context Map的实例 40 | */ 41 | protected static Map getContextMap() { 42 | return (Map) threadContext.get(); 43 | } 44 | 45 | /** 46 | * 清理线程所有被hold住的对象。以便重用! 47 | */ 48 | public static void reset() { 49 | getContextMap().clear(); 50 | } 51 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## JAVA常用工具类commutils4j 2 | 3 | ### 项目由来 4 | ```` 5 | <1> 项目主要目的是为了整理开发时常用的JAVA工具类,加快效率,使开发业务逻辑更清晰。 6 | <2> 希望此项目能够吸引更多的开发者,一起完善一个好用的工具集。 7 | ```` 8 | 9 | * commutils4j 10 | * u4common 11 | * FtpUtils -> Ftp工具类 12 | * PropertiesCacheUtils -> 配置缓存工具类 13 | * DingDingMessageUtil -> 钉钉消息工具类 14 | * u4db 15 | * RedisOperationUtils -> Redis工具类 16 | * RedisUtils -> Redis业务序列号 17 | * u4datetime 18 | * DatetimeUtils -> 时间类型工具类 19 | * u4double 20 | * DoubleUtils -> Double工具类 21 | * u4decimal 22 | * BigDemicalUtils -> BigDemical工具类 23 | * u4encrypt 24 | * MD5Utils -> 加密工具类 25 | * u4entity 26 | * EntityUtils -> 实体类工具类 27 | * u4enum 28 | * BusinessEnum -> 枚举类 29 | * u4file 30 | * FileZipUtils -> 压缩文件工具类 31 | * StreamUtils -> 文件流处理工具类 32 | * u4img 33 | * ImageUtils ->图片处理工具类 34 | * u4integer 35 | * IntegerUtils -> Integer工具类 36 | * u4json 37 | * Wrapper -> Json工具类 38 | * u4list 39 | * ListUtils -> List工具类 40 | * u4object 41 | * ObjectUtils -> Object工具类 42 | * u4os 43 | * LinuxSystemTool -> 系统工具类 44 | * BareBonesBrowserLaunch -> 打开浏览器 45 | * u4net 46 | * CookieUtils -> Cookie工具类 47 | * IpUtils -> Ip工具类 48 | * HttpClientUtil -> Http连接工具类 49 | * u4string 50 | * ChinaUpperCaseUtil -> 中文工具类 51 | * StringUtils -> String工具类 52 | * u4thread 53 | * ThreadLocalMap -> 线程自身携带参数 54 | 55 | ### LICENSE 56 | commutils4j belongs to MIT license (http://opensource.org/licenses/MIT) 57 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4enum/BusinessEnum.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4enum; 2 | 3 | import com.google.common.collect.Lists; 4 | import com.google.common.collect.Maps; 5 | import org.apache.commons.lang3.StringUtils; 6 | 7 | import java.util.Arrays; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | /** 12 | * Created by dragon on 12/6/2017. 13 | */ 14 | public enum BusinessEnum { 15 | 16 | FIRST("FIRST", "1"), 17 | SECOND("SECOND", "2"), 18 | THIRD("THIRD", "3"); 19 | 20 | BusinessEnum(String code, String desc) { 21 | this.code = code; 22 | this.desc = desc; 23 | } 24 | 25 | private String code; 26 | 27 | private String desc; 28 | 29 | public String getCode() { 30 | return code; 31 | } 32 | 33 | public void setCode(String code) { 34 | this.code = code; 35 | } 36 | 37 | public String getDesc() { 38 | return desc; 39 | } 40 | 41 | public void setDesc(String desc) { 42 | this.desc = desc; 43 | } 44 | 45 | public static List getList() { 46 | return Arrays.asList(BusinessEnum.values()); 47 | } 48 | 49 | public static String getBusinessTypeDescByCode(String code) { 50 | for (BusinessEnum typeEnum : getList()) { 51 | if(StringUtils.equals(typeEnum.getCode(), code)){ 52 | return typeEnum.getDesc(); 53 | } 54 | } 55 | return code; 56 | } 57 | 58 | public static List> getMap2List() { 59 | List> list = Lists.newArrayList(); 60 | for (BusinessEnum ele : BusinessEnum.values()) { 61 | Map map = Maps.newHashMap(); 62 | map.put("desc", ele.getDesc()); 63 | map.put("code", ele.getCode()); 64 | list.add(map); 65 | } 66 | return list; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4net/IpUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4net; 2 | 3 | 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | 8 | /** 9 | * ip工具类 10 | */ 11 | public final class IpUtils { 12 | 13 | public static String getClientIp(HttpServletRequest request){ 14 | String ip = request.getHeader("x-forwarded-for"); 15 | if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ 16 | ip = request.getHeader("Proxy-Client-IP"); 17 | } 18 | if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ 19 | ip = request.getHeader("WL-Proxy-Client-IP"); 20 | } 21 | if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ 22 | ip = request.getRemoteAddr(); 23 | } 24 | return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip; 25 | } 26 | 27 | /** 28 | * 获得用户远程地址 29 | */ 30 | public static String getRemoteAddr(HttpServletRequest request){ 31 | String remoteAddr = request.getHeader("X-Real-IP"); 32 | if (StringUtils.isNotBlank(remoteAddr)) { 33 | remoteAddr = request.getHeader("X-Forwarded-For"); 34 | }else if (StringUtils.isNotBlank(remoteAddr)) { 35 | remoteAddr = request.getHeader("Proxy-Client-IP"); 36 | }else if (StringUtils.isNotBlank(remoteAddr)) { 37 | remoteAddr = request.getHeader("WL-Proxy-Client-IP"); 38 | } 39 | if(StringUtils.isEmpty(remoteAddr)){ 40 | remoteAddr = request.getRemoteAddr(); 41 | if(StringUtils.isNotEmpty(remoteAddr) && "0:0:0:0:0:0:0:1".equals(remoteAddr)){ 42 | remoteAddr = "127.0.0.1"; 43 | }else { 44 | remoteAddr = request.getRemoteAddr(); 45 | } 46 | } 47 | return remoteAddr; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/u4json/U4JsonTest.java: -------------------------------------------------------------------------------- 1 | package u4json; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.git.comm.utils.u4json.WrapMapper; 5 | import com.git.comm.utils.u4json.Wrapper; 6 | import org.junit.Test; 7 | import u4entity.Person; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * Created by dragon on 8/23/2017. 14 | */ 15 | public class U4JsonTest { 16 | 17 | @Test 18 | public void test(){ 19 | // String - List 20 | List stringList = new ArrayList(); 21 | stringList.add("first"); 22 | stringList.add("second"); 23 | stringList.add("third"); 24 | Wrapper> wrapString = WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, stringList); 25 | System.out.println("wrapString = " + wrapString.getResult()); 26 | 27 | // Object - Person 28 | Person person = new Person(); 29 | person.setName("dragon"); 30 | person.setAge("22"); 31 | person.setSex("man"); 32 | Wrapper wrapPerson = WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, person); 33 | System.out.println("wrapPerson = " + wrapPerson.getResult()); 34 | 35 | // Object - Person - List 36 | List personList = new ArrayList(); 37 | Person person1 = new Person(); 38 | person1.setName("dragon"); 39 | person1.setAge("22"); 40 | person1.setSex("man"); 41 | personList.add(person1); 42 | Person person2 = new Person(); 43 | person2.setName("loong"); 44 | person2.setAge("18"); 45 | person2.setSex("man"); 46 | personList.add(person2); 47 | Wrapper> wrapPersonList = WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, personList); 48 | System.out.println("wrapPersonList = " + wrapPersonList.getResult()); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4double/DoubleUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4double; 2 | 3 | 4 | import java.math.BigDecimal; 5 | import java.text.DecimalFormat; 6 | 7 | public class DoubleUtils { 8 | /** 9 | * 将double类型的数字保留两位小数(四舍五入) 10 | * 11 | * @param number 12 | * @return 13 | */ 14 | public static String formatNumber(double number) { 15 | DecimalFormat df = new DecimalFormat(); 16 | df.applyPattern("#0.00"); 17 | return df.format(number); 18 | } 19 | 20 | /** 21 | * 加法 22 | * @param v1 23 | * @param v2 24 | * @return 25 | */ 26 | public static double add(double v1, double v2) { 27 | BigDecimal b1 = new BigDecimal(Double.toString(v1)); 28 | BigDecimal b2 = new BigDecimal(Double.toString(v2)); 29 | return b1.add(b2).doubleValue(); 30 | } 31 | 32 | /** 33 | * 减法 34 | * @param v1 35 | * @param v2 36 | * @return 37 | */ 38 | public static double sub(double v1, double v2) { 39 | BigDecimal b1 = new BigDecimal(Double.toString(v1)); 40 | BigDecimal b2 = new BigDecimal(Double.toString(v2)); 41 | return b1.subtract(b2).doubleValue(); 42 | } 43 | 44 | /** 45 | * 乘法 46 | * @param v1 47 | * @param v2 48 | * @return 49 | */ 50 | public static double mul(double v1, double v2) { 51 | BigDecimal b1 = new BigDecimal(Double.toString(v1)); 52 | BigDecimal b2 = new BigDecimal(Double.toString(v2)); 53 | return b1.multiply(b2).doubleValue(); 54 | } 55 | 56 | /** 57 | * 除法 58 | * @param v1 59 | * @param v2 60 | * @return 61 | */ 62 | public static double div(double v1, double v2) { 63 | BigDecimal b1 = new BigDecimal(Double.toString(v1)); 64 | BigDecimal b2 = new BigDecimal(Double.toString(v2)); 65 | return b1.divide(b2, 3, BigDecimal.ROUND_HALF_UP).doubleValue(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4db/RedisUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4db; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.data.redis.core.StringRedisTemplate; 7 | import org.springframework.data.redis.core.ValueOperations; 8 | import org.springframework.stereotype.Component; 9 | 10 | import java.text.SimpleDateFormat; 11 | import java.util.Date; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | /** 15 | * Created by dragon on 11/7/2017. 16 | */ 17 | @Component 18 | public class RedisUtils { 19 | protected Logger logger = LoggerFactory.getLogger(this.getClass()); 20 | 21 | @Autowired 22 | private StringRedisTemplate rt; 23 | 24 | public String getNowCode(String envLock, String prefix, int prefixLen){ 25 | SimpleDateFormat df = new SimpleDateFormat("yyMMdd"); 26 | String dateStr = df.format(new Date()); 27 | String key = envLock + prefix +dateStr; 28 | String valuePrefix = prefix +dateStr; 29 | Long newValue =1L; 30 | try { 31 | synchronized (this) { 32 | ValueOperations ops = rt.opsForValue(); 33 | if (!rt.hasKey(key)) { 34 | ops.set(key, String.valueOf(1L)); 35 | rt.expire(key, 1L, TimeUnit.DAYS); 36 | } else { 37 | newValue = ops.increment(key, 1L); 38 | } 39 | } 40 | } catch (Exception ex) { 41 | logger.error("Redis生成序列号发生错误", ex); 42 | } 43 | String zeroStr = ""; 44 | if (null != newValue && prefixLen > 0 && String.valueOf(newValue).length() < prefixLen) { 45 | for (int i = 0; i < prefixLen - String.valueOf(newValue).length(); i++) { 46 | zeroStr += "0"; 47 | } 48 | } 49 | return valuePrefix + zeroStr + newValue; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4os/BareBonesBrowserLaunch.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4os; 2 | 3 | import javax.swing.*; 4 | import java.lang.reflect.Method; 5 | 6 | /** 7 | *

Title: BareBonesBrowserLaunch.

8 | *

Description 根据系统打开浏览器

9 | * @author net ( thanks to Internet & author) 10 | * @date 2018/8/14 下午11:06 11 | */ 12 | public class BareBonesBrowserLaunch { 13 | public static void openURL(String url) { 14 | try { 15 | browse(url); 16 | } catch (Exception e) { 17 | JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:\n" + e.getLocalizedMessage()); 18 | } 19 | } 20 | 21 | private static void browse(String url) throws Exception { 22 | String osName = System.getProperty("os.name", ""); 23 | if (osName.startsWith("Mac OS")) { 24 | Class fileMgr = Class.forName("com.apple.eio.FileManager"); 25 | Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class }); 26 | openURL.invoke(null, new Object[] { url }); 27 | } else if (osName.startsWith("Windows")) { 28 | Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); 29 | } else { 30 | // assume Unix or Linux 31 | String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; 32 | String browser = null; 33 | for (int count = 0; count < browsers.length && browser == null; count++) { 34 | if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) { 35 | browser = browsers[count]; 36 | } 37 | if (browser == null) { 38 | throw new NoSuchMethodException("Could not find web browser"); 39 | } else { 40 | Runtime.getRuntime().exec(new String[] { browser, url }); 41 | } 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4list/ListUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4list; 2 | 3 | import java.util.*; 4 | 5 | public class ListUtils { 6 | 7 | /** 8 | * 判断对象数组是否为空并且数量大于0 9 | * 10 | * @param value 11 | * @return 12 | */ 13 | public static Boolean isNotNullAndNotEmpty(Object[] value) { 14 | boolean bl = false; 15 | if (null != value && 0 < value.length) { 16 | bl = true; 17 | } 18 | return bl; 19 | } 20 | 21 | /** 22 | * 判断对象集合(List,Set)是否为空并且数量大于0 23 | * 24 | * @param value 25 | * @return 26 | */ 27 | public static Boolean isNotNullAndNotEmpty(Collection value) { 28 | boolean bl = false; 29 | if (null != value && 0 < value.size()) { 30 | bl = true; 31 | } 32 | return bl; 33 | } 34 | 35 | /** 36 | * 判断集合是否为空 37 | * @param coll 38 | * @return 39 | */ 40 | public static boolean isEmpty(Collection coll) { 41 | return coll == null || coll.isEmpty(); 42 | } 43 | 44 | /** 45 | * 拆分list,按500条拆分 46 | * @param list 47 | * @return 48 | */ 49 | public static List> splitList(List list){ 50 | List> lists = new ArrayList>(); 51 | List subList = new ArrayList(); 52 | int size = list.size(); 53 | int sum = 500; 54 | int count = size / sum; 55 | int yu = size % sum; 56 | if (count == 0) { 57 | lists.add(list); 58 | } else { 59 | if(size % sum != 0){ 60 | count ++; 61 | } 62 | for (int i = 0; i < count; i++) { 63 | if(sum*(i+1) <= size){ 64 | subList = list.subList(sum*i, sum*(i+1)); 65 | }else{ 66 | subList = list.subList(sum*i, sum*(i)+yu); 67 | } 68 | lists.add(subList); 69 | } 70 | } 71 | return lists; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/xss/XssHttpServletRequestWrapper.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.xss; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import javax.servlet.http.HttpServletRequestWrapper; 5 | 6 | public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper { 7 | HttpServletRequest orgRequest = null; 8 | 9 | public XssHttpServletRequestWrapper(HttpServletRequest request) { 10 | super(request); 11 | orgRequest = request; 12 | } 13 | 14 | @Override 15 | public String getParameter(String name) { 16 | // 返回值之前 先进行过滤 17 | return XssShieldUtil.stripXss(super.getParameter(XssShieldUtil.stripXss(name))); 18 | } 19 | 20 | @Override 21 | public String[] getParameterValues(String name) { 22 | // 返回值之前 先进行过滤 23 | String[] values = super.getParameterValues(XssShieldUtil.stripXss(name)); 24 | if(values != null){ 25 | for (int i = 0; i < values.length; i++) { 26 | values[i] = XssShieldUtil.stripXss(values[i]); 27 | } 28 | } 29 | return values; 30 | } 31 | 32 | /** 33 | * 覆盖getHeader方法,将参数名和参数值都做xss过滤。
34 | * 如果需要获得原始的值,则通过super.getHeaders(name)来获取
35 | * getHeaderNames 也可能需要覆盖 36 | */ 37 | @Override 38 | public String getHeader(String name) { 39 | 40 | String value = super.getHeader(xssEncode(name)); 41 | if (value != null) { 42 | value = xssEncode(value); 43 | } 44 | return value; 45 | } 46 | 47 | /** 48 | * 将容易引起xss漏洞的半角字符直接替换成全角字符 49 | * @param s 50 | * @return 51 | */ 52 | private static String xssEncode(String s) { 53 | if (s == null || s.isEmpty()) { 54 | return s; 55 | } 56 | StringBuilder sb = new StringBuilder(s.length() + 16); 57 | for (int i = 0; i < s.length(); i++) { 58 | char c = s.charAt(i); 59 | switch (c) { 60 | case '>': 61 | sb.append(">"); 62 | break; 63 | case '<': 64 | sb.append("<"); 65 | break; 66 | case '\'': 67 | sb.append("'"); 68 | break; 69 | case '\"': 70 | sb.append("\""); 71 | break; 72 | case '&': 73 | sb.append("&"); 74 | break; 75 | default: 76 | sb.append(c); 77 | break; 78 | } 79 | } 80 | return sb.toString(); 81 | } 82 | } -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4json/Wrapper.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4json; 2 | 3 | import com.github.pagehelper.Page; 4 | import java.io.Serializable; 5 | import org.codehaus.jackson.annotate.JsonIgnore; 6 | import org.codehaus.jackson.map.annotate.JsonSerialize; 7 | import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion; 8 | 9 | @JsonSerialize( 10 | include = Inclusion.NON_NULL 11 | ) 12 | public class Wrapper implements Serializable { 13 | public static final int SUCCESS_CODE = 200; 14 | public static final String SUCCESS_MESSAGE = "操作成功"; 15 | public static final int CAPTCHA_CODE_ERROR = 600; 16 | public static final int ERROR_CODE = 500; 17 | public static final String ERROR_MESSAGE = "内部异常"; 18 | public static final int ILLEGAL_ARGUMENT_CODE_ = 100; 19 | public static final String ILLEGAL_ARGUMENT_MESSAGE = "参数非法"; 20 | private int code; 21 | private String message; 22 | private T result; 23 | 24 | public Wrapper() { 25 | this(200, "操作成功"); 26 | } 27 | 28 | public Wrapper(int code, String message) { 29 | this.code(code).message(message); 30 | } 31 | 32 | public Wrapper(int code, String message, T result) { 33 | this.code(code).message(message).result(result); 34 | } 35 | 36 | public Wrapper(int code, String message, T result, Page page) { 37 | this.code(code).message(message).result(result); 38 | } 39 | 40 | public int getCode() { 41 | return this.code; 42 | } 43 | 44 | public void setCode(int code) { 45 | this.code = code; 46 | } 47 | 48 | public String getMessage() { 49 | return this.message; 50 | } 51 | 52 | public void setMessage(String message) { 53 | this.message = message; 54 | } 55 | 56 | public T getResult() { 57 | return this.result; 58 | } 59 | 60 | public void setResult(T result) { 61 | this.result = result; 62 | } 63 | 64 | public Wrapper code(int code) { 65 | this.setCode(code); 66 | return this; 67 | } 68 | 69 | public Wrapper message(String message) { 70 | this.setMessage(message); 71 | return this; 72 | } 73 | 74 | public Wrapper result(T result) { 75 | this.setResult(result); 76 | return this; 77 | } 78 | 79 | @JsonIgnore 80 | public boolean isSuccess() { 81 | return 200 == this.code; 82 | } 83 | 84 | @JsonIgnore 85 | public boolean isFail() { 86 | return 200 != this.code; 87 | } 88 | 89 | public String toString() { 90 | return "Wrapper{code=" + this.code + ", message=\'" + this.message + '\'' + ", result=" + this.result + '}'; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4os/LinuxSystemTool.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4os; 2 | 3 | import java.io.*; 4 | import java.util.StringTokenizer; 5 | 6 | public final class LinuxSystemTool { 7 | /** 8 | * @return int[] result 9 | * result.length==4;int[0]=MemTotal;int[1]=MemFree;int[2]=SwapTotal;int[3]=SwapFree; 10 | */ 11 | public static int[] getMemInfo() throws IOException, InterruptedException { 12 | File file = new File("/proc/meminfo"); 13 | BufferedReader br = new BufferedReader(new InputStreamReader( 14 | new FileInputStream(file))); 15 | int[] result = new int[4]; 16 | String str = null; 17 | StringTokenizer token = null; 18 | while((str = br.readLine()) != null) { 19 | token = new StringTokenizer(str); 20 | if(!token.hasMoreTokens()) 21 | continue; 22 | 23 | str = token.nextToken(); 24 | if(!token.hasMoreTokens()) 25 | continue; 26 | 27 | if(str.equalsIgnoreCase("MemTotal:")) 28 | result[0] = Integer.parseInt(token.nextToken()); 29 | else if(str.equalsIgnoreCase("MemFree:")) 30 | result[1] = Integer.parseInt(token.nextToken()); 31 | else if(str.equalsIgnoreCase("SwapTotal:")) 32 | result[2] = Integer.parseInt(token.nextToken()); 33 | else if(str.equalsIgnoreCase("SwapFree:")) 34 | result[3] = Integer.parseInt(token.nextToken()); 35 | } 36 | return result; 37 | } 38 | 39 | public static float getCpuInfo() throws IOException, InterruptedException { 40 | File file = new File("/proc/stat"); 41 | BufferedReader br = new BufferedReader(new InputStreamReader( 42 | new FileInputStream(file))); 43 | StringTokenizer token = new StringTokenizer(br.readLine()); 44 | token.nextToken(); 45 | int user1 = Integer.parseInt(token.nextToken()); 46 | int nice1 = Integer.parseInt(token.nextToken()); 47 | int sys1 = Integer.parseInt(token.nextToken()); 48 | int idle1 = Integer.parseInt(token.nextToken()); 49 | 50 | Thread.sleep(1000); 51 | 52 | br = new BufferedReader( 53 | new InputStreamReader(new FileInputStream(file))); 54 | token = new StringTokenizer(br.readLine()); 55 | token.nextToken(); 56 | int user2 = Integer.parseInt(token.nextToken()); 57 | int nice2 = Integer.parseInt(token.nextToken()); 58 | int sys2 = Integer.parseInt(token.nextToken()); 59 | int idle2 = Integer.parseInt(token.nextToken()); 60 | 61 | return (float)((user2 + sys2 + nice2) - (user1 + sys1 + nice1)) / (float)((user2 + nice2 + sys2 + idle2) - (user1 + nice1 + sys1 + idle1)); 62 | } 63 | } 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4common/FtpUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4common; 2 | 3 | import org.apache.commons.vfs2.*; 4 | import org.apache.commons.vfs2.provider.ftp.FtpFileSystemConfigBuilder; 5 | import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder; 6 | 7 | import java.io.IOException; 8 | 9 | /** 10 | * FtpUtils 11 | */ 12 | public class FtpUtils { 13 | 14 | /** 15 | * 得到远程文件列表 16 | */ 17 | public static FileObject[] getFilesByFtp(String url){ 18 | FileObject[] fileObjects = null; 19 | try { 20 | FileSystemManager fsManager = VFS.getManager(); 21 | FileObject fo = fsManager.resolveFile(url); 22 | FileSystemOptions options = new FileSystemOptions(); 23 | FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, false); 24 | fileObjects = fo.getChildren(); 25 | } catch (FileSystemException e) { 26 | e.printStackTrace(); 27 | } catch (IOException e) { 28 | e.printStackTrace(); 29 | } 30 | return fileObjects; 31 | } 32 | 33 | /** 34 | * 筛选所需文件 35 | */ 36 | public static FileObject[] getNeedFiles(String url){ 37 | FileObject[] fileObjects = getFilesByFtp(url); 38 | FileObject[] newFileObjects = null; 39 | for (int i = 0; i < fileObjects.length; i++){ 40 | // 在此处做文件筛选 41 | } 42 | return newFileObjects; 43 | } 44 | 45 | /** 46 | * ftp/sftp 47 | * 通过前置服务器无跳板机上传至所需服务器 48 | * @param sourcePath 前置服务器地址 49 | * @param targetPath 上传服务器地址 50 | * @throws FileSystemException 51 | */ 52 | public static void copyFile(String sourcePath, String targetPath) throws FileSystemException { 53 | try { 54 | FileObject[] getDownloadFiles = null; 55 | // 获取需要上传的文件 56 | FileSystemManager fsManager = VFS.getManager(); 57 | // 全部文件 58 | getDownloadFiles = getFilesByFtp(sourcePath); 59 | // 如需筛选,选用下面方法 60 | // getDownloadFiles = getNeedFiles(sourcePath); 61 | // 上传文件至服务器 62 | FileSystemOptions opts = new FileSystemOptions();; 63 | SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); 64 | FileObject getUploadFiles = fsManager.resolveFile(targetPath, opts); 65 | for (int i = 0; i < getDownloadFiles.length; i++) { 66 | FileObject tmp = fsManager.resolveFile(getUploadFiles, getDownloadFiles[i].getName().getBaseName()); 67 | if (!tmp.exists()) { 68 | tmp.copyFrom(getDownloadFiles[i], Selectors.SELECT_SELF); 69 | } 70 | } 71 | } catch (FileSystemException e) { 72 | e.printStackTrace(); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4common/DingDingMessageUtil.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4common; 2 | 3 | import com.git.comm.utils.u4json.JsonUtils; 4 | import org.springframework.beans.factory.annotation.Value; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.io.InputStream; 8 | import java.io.OutputStream; 9 | import java.net.HttpURLConnection; 10 | import java.net.URL; 11 | 12 | /** 13 | * 钉钉机器人发送消息 14 | * https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1 15 | * 16 | * @author github -> https://github.com/yinjihuan 17 | */ 18 | @Component 19 | public class DingDingMessageUtil { 20 | 21 | @Value("${ding-access-token}") 22 | public String dingAccessToken; 23 | @Value("${ding-open}") 24 | private Boolean dingOpen; 25 | 26 | public void sendTextMessage(String msg) { 27 | try { 28 | if (dingOpen) { 29 | Message message = new Message(); 30 | message.setMsgtype("text"); 31 | message.setText(new MessageInfo(msg)); 32 | URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + dingAccessToken); 33 | // 建立http连接 34 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 35 | conn.setDoOutput(true); 36 | conn.setDoInput(true); 37 | conn.setUseCaches(false); 38 | conn.setRequestMethod("POST"); 39 | conn.setRequestProperty("Charset", "UTF-8"); 40 | conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8"); 41 | conn.connect(); 42 | OutputStream out = conn.getOutputStream(); 43 | String textMessage = JsonUtils.toJson(message); 44 | byte[] data = textMessage.getBytes(); 45 | out.write(data); 46 | out.flush(); 47 | out.close(); 48 | InputStream in = conn.getInputStream(); 49 | byte[] data1 = new byte[in.available()]; 50 | in.read(data1); 51 | } 52 | } catch (Exception e) { 53 | // 不处理异常 54 | } 55 | } 56 | } 57 | 58 | class Message { 59 | private String msgtype; 60 | private MessageInfo text; 61 | 62 | public String getMsgtype() { 63 | return msgtype; 64 | } 65 | 66 | public MessageInfo getText() { 67 | return text; 68 | } 69 | 70 | public void setMsgtype(String msgtype) { 71 | this.msgtype = msgtype; 72 | } 73 | 74 | public void setText(MessageInfo text) { 75 | this.text = text; 76 | } 77 | } 78 | 79 | class MessageInfo { 80 | private String content; 81 | 82 | public MessageInfo(String content) { 83 | this.content = content; 84 | } 85 | } -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4net/CookieUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4net; 2 | 3 | import javax.servlet.http.Cookie; 4 | import javax.servlet.http.HttpServletRequest; 5 | import javax.servlet.http.HttpServletResponse; 6 | import java.io.UnsupportedEncodingException; 7 | import java.net.URLDecoder; 8 | import java.net.URLEncoder; 9 | 10 | /** 11 | * Cookie 12 | */ 13 | public class CookieUtils { 14 | 15 | /** 16 | * 设置 Cookie(生成时间为1天) 17 | * @param name 名称 18 | * @param value 值 19 | */ 20 | public static void setCookie(HttpServletResponse response, String name, String value) { 21 | setCookie(response, name, value, 60*60*24); 22 | } 23 | 24 | /** 25 | * 设置 Cookie 26 | * @param name 名称 27 | * @param value 值 28 | */ 29 | public static void setCookie(HttpServletResponse response, String name, String value, String path) { 30 | setCookie(response, name, value, path, 60*60*24); 31 | } 32 | 33 | /** 34 | * 设置 Cookie 35 | * @param name 名称 36 | * @param value 值 37 | * @param maxAge 生存时间(单位秒) 38 | */ 39 | public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) { 40 | setCookie(response, name, value, "/", maxAge); 41 | } 42 | 43 | /** 44 | * 设置 Cookie 45 | * @param name 名称 46 | * @param value 值 47 | * @param maxAge 生存时间(单位秒) 48 | */ 49 | public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) { 50 | Cookie cookie = new Cookie(name, null); 51 | cookie.setPath(path); 52 | cookie.setMaxAge(maxAge); 53 | try { 54 | cookie.setValue(URLEncoder.encode(value, "utf-8")); 55 | } catch (UnsupportedEncodingException e) { 56 | e.printStackTrace(); 57 | } 58 | response.addCookie(cookie); 59 | } 60 | 61 | /** 62 | * 获得指定Cookie的值 63 | * @param name 名称 64 | * @return 值 65 | */ 66 | public static String getCookie(HttpServletRequest request, String name) { 67 | return getCookie(request, null, name, false); 68 | } 69 | /** 70 | * 获得指定Cookie的值,并删除。 71 | * @param name 名称 72 | * @return 值 73 | */ 74 | public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name) { 75 | return getCookie(request, response, name, true); 76 | } 77 | /** 78 | * 获得指定Cookie的值 79 | * @param request 请求对象 80 | * @param response 响应对象 81 | * @param name 名字 82 | * @param isRemove 是否移除 83 | * @return 值 84 | */ 85 | public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name, boolean isRemove) { 86 | String value = null; 87 | Cookie[] cookies = request.getCookies(); 88 | if (cookies != null) { 89 | for (Cookie cookie : cookies) { 90 | if (cookie.getName().equals(name)) { 91 | try { 92 | value = URLDecoder.decode(cookie.getValue(), "utf-8"); 93 | } catch (UnsupportedEncodingException e) { 94 | e.printStackTrace(); 95 | } 96 | if (isRemove) { 97 | cookie.setMaxAge(0); 98 | response.addCookie(cookie); 99 | } 100 | } 101 | } 102 | } 103 | return value; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4common/PropertiesCacheUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4common; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import java.io.IOException; 7 | import java.io.InputStream; 8 | import java.util.Map; 9 | import java.util.Properties; 10 | import java.util.concurrent.ConcurrentHashMap; 11 | 12 | /** 13 | * 所有properties配置缓存 14 | */ 15 | public class PropertiesCacheUtils { 16 | 17 | private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesCacheUtils.class); 18 | private static Map propertiesAttrMap = new ConcurrentHashMap(); 19 | 20 | //缓存properties文件 21 | private static Map propertiesCache = new ConcurrentHashMap(); 22 | 23 | public static String getValue(String key){ 24 | String value = propertiesAttrMap.get(key); 25 | if(value == null){ 26 | try { 27 | throw new Exception("没有配置" + key); 28 | } catch (Exception e) { 29 | e.printStackTrace(); 30 | } 31 | } 32 | return value; 33 | } 34 | 35 | public static Integer getInteger(String key){ 36 | return Integer.valueOf(getValue(key)); 37 | 38 | } 39 | 40 | public static Long getLong(String key){ 41 | return Long.valueOf(getValue(key)); 42 | 43 | } 44 | 45 | public static Boolean getBoolean(String key){ 46 | return Boolean.valueOf(getValue(key)); 47 | 48 | } 49 | 50 | public static Double getDouble(String key){ 51 | return Double.valueOf(getValue(key)); 52 | 53 | } 54 | 55 | public static Map getPropertiesMap(){ 56 | return propertiesAttrMap; 57 | } 58 | 59 | 60 | public static Map getProperties() { 61 | return propertiesCache; 62 | } 63 | 64 | 65 | /** 66 | * 获取properties 67 | * @param propertiesName 68 | * @return 69 | */ 70 | public static Properties getProperties(String propertiesName){ 71 | InputStream inputStream; 72 | Properties properties =propertiesCache.get(propertiesName); 73 | if(properties == null){ 74 | inputStream = PropertiesCacheUtils.class.getResourceAsStream("/" + propertiesName); 75 | properties = new Properties(); 76 | try { 77 | properties.load(inputStream); 78 | inputStream.close(); 79 | } catch (IOException e) { 80 | LOGGER.warn(e.getMessage(),e); 81 | //某些情况下释放资源;比如LOAD报错 82 | inputStream = null; 83 | } 84 | if(properties != null){ 85 | propertiesCache.put(propertiesName, properties); 86 | prorsToMap(properties); 87 | } 88 | } 89 | return properties; 90 | } 91 | 92 | public static void prorsToMap(Properties properties){ 93 | for(Object key:properties.keySet()){ 94 | String strKey = key.toString(); 95 | String value = properties.getProperty(strKey); 96 | propertiesAttrMap.put(strKey, value); 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4json/JsonUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4json; 2 | 3 | import org.codehaus.jackson.map.ObjectMapper; 4 | 5 | import java.io.StringWriter; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | /** 10 | * Json 工具类 11 | */ 12 | public class JsonUtils { 13 | private static ObjectMapper mapper = new ObjectMapper(); 14 | 15 | public static String toString(Object obj) { 16 | return toJson(obj); 17 | } 18 | 19 | public static String toJson(Object obj) { 20 | try { 21 | StringWriter writer = new StringWriter(); 22 | mapper.writeValue(writer, obj); 23 | return writer.toString(); 24 | } catch (Exception e) { 25 | throw new RuntimeException("序列化对象【" + obj + "】时出错", e); 26 | } 27 | } 28 | 29 | public static T toBean(Class entityClass, String jsonString) { 30 | try { 31 | return mapper.readValue(jsonString, entityClass); 32 | } catch (Exception e) { 33 | throw new RuntimeException("JSON【" + jsonString + "】转对象时出错", e); 34 | } 35 | } 36 | 37 | /** 38 | * 用于对象通过其他工具已转为JSON的字符形式,这里不需要再加上引号 39 | * 40 | * @param obj 41 | * @param isObject 42 | */ 43 | public static String getJsonSuccess(String obj, boolean isObject) { 44 | String jsonString = null; 45 | if (obj == null) { 46 | jsonString = "{\"success\":true}"; 47 | } else { 48 | jsonString = "{\"success\":true,\"data\":" + obj + "}"; 49 | } 50 | return jsonString; 51 | } 52 | 53 | public static String getJsonSuccess(Object obj) { 54 | return getJsonSuccess(obj, null); 55 | } 56 | 57 | public static String getJsonSuccess(Object obj, String message) { 58 | if (obj == null) { 59 | return "{\"success\":true,\"message\":\"" + message + "\"}"; 60 | } else { 61 | try { 62 | Map map = new HashMap(); 63 | map.put("success", true); 64 | return "{\"success\":true," + toString(obj) + ",\"message\":\"" + message + "\"}"; 65 | } catch (Exception e) { 66 | throw new RuntimeException("序列化对象【" + obj + "】时出错", e); 67 | } 68 | } 69 | } 70 | 71 | public static String getJsonError(Object obj) { 72 | return getJsonError(obj, null); 73 | } 74 | 75 | public static String getJsonError(Object obj, String message) { 76 | if (obj == null) { 77 | return "{\"success\":false,\"message\":\"" + message + "\"}"; 78 | } else { 79 | try { 80 | obj = parseIfException(obj); 81 | return "{\"success\":false,\"data\":" + toString(obj) + ",\"message\":\"" + message + "\"}"; 82 | } catch (Exception e) { 83 | throw new RuntimeException("序列化对象【" + obj + "】时出错", e); 84 | } 85 | } 86 | } 87 | 88 | public static Object parseIfException(Object obj) { 89 | if (obj instanceof Exception) { 90 | return getErrorMessage((Exception) obj, null); 91 | } 92 | return obj; 93 | } 94 | 95 | public static String getErrorMessage(Exception e, String defaultMessage) { 96 | return defaultMessage != null ? defaultMessage : null; 97 | } 98 | 99 | public static ObjectMapper getMapper() { 100 | return mapper; 101 | } 102 | } -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4img/ImageUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4img; 2 | 3 | import com.git.comm.utils.u4string.StringUtils; 4 | 5 | import javax.imageio.ImageIO; 6 | import javax.imageio.stream.ImageOutputStream; 7 | import java.awt.*; 8 | import java.awt.geom.Ellipse2D; 9 | import java.awt.image.BufferedImage; 10 | import java.io.*; 11 | 12 | /** 13 | * Created by dragon on 1/21/2018. 14 | */ 15 | public class ImageUtils { 16 | 17 | /** 18 | * 将文字生成图片 19 | * @param text 20 | * @param width 21 | * @param height 22 | * @param fontSize 23 | * @return 24 | */ 25 | public String textToPic(String text, int width, int height, int fontSize) { 26 | String textPath = ""; 27 | try { 28 | String fileName = StringUtils.getUUID() + ".png"; 29 | // 修改filePath 30 | String filePath = "" + fileName; 31 | File file = new File(filePath); 32 | Font font = new Font("华文隶书", Font.BOLD, fontSize); 33 | BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); 34 | Graphics2D g2 = (Graphics2D) bi.getGraphics(); 35 | g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 36 | g2.setFont(font); 37 | g2.setPaint(Color.red); 38 | paintString(g2, text,1,270, fontSize); 39 | g2.dispose(); 40 | ImageIO.write(bi, "png", file); 41 | // BufferedImage 转换 InputStream 42 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 43 | ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream); 44 | ImageIO.write(bi, "png", imageOutput); 45 | } catch (Exception e) { 46 | e.printStackTrace(); 47 | } 48 | return textPath; 49 | } 50 | 51 | public static void paintString(Graphics2D g2d, String str, int x, int y, int fontSize) { 52 | FontMetrics metrics = g2d.getFontMetrics(); 53 | for (char ca : str.toCharArray()) { 54 | int px = metrics.stringWidth("" + ca); 55 | g2d.drawString("" + ca, x + (fontSize - px) / 2, y); 56 | x += fontSize; 57 | } 58 | } 59 | 60 | /** 61 | * 对图片进行成比例缩小 62 | * @param originalImage 原始图片 63 | * @param times 缩小倍数 64 | * @return 缩小后的Image 65 | */ 66 | public static BufferedImage zoomOutImage(BufferedImage originalImage, Integer times){ 67 | // 50是自己新增的,可删除 68 | int width = originalImage.getWidth() / times + 50; 69 | int height = originalImage.getHeight() / times + 50; 70 | BufferedImage newImage = new BufferedImage(width, height, originalImage.getType()); 71 | Graphics g = newImage.getGraphics(); 72 | g.drawImage(originalImage, 0, 0, width, height,null); 73 | g.dispose(); 74 | return newImage; 75 | 76 | } 77 | 78 | /** 79 | * 传入的图像必须是正方形的 才会 圆形 如果是长方形的比例则会变成椭圆的 80 | * @return 81 | * @throws IOException 82 | */ 83 | public static BufferedImage convertCircular(BufferedImage bi1, String url) throws IOException{ 84 | //透明底的图片 85 | BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); 86 | // 第一个值x 和第二个值y 是圈定图片选中的位置 87 | Ellipse2D.Double shape = new Ellipse2D.Double(0, 0,bi1.getWidth(), bi1.getHeight()); 88 | Graphics2D g2 = bi2.createGraphics(); 89 | g2.setClip(shape); 90 | // 使用 setRenderingHint 设置抗锯齿 91 | g2.drawImage(bi1,0,0,null); 92 | g2.dispose(); 93 | return bi2; 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4decimal/BigDemicalUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4decimal; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public class BigDemicalUtils { 6 | /** 7 | * 提供精确加法计算的add方法 8 | * 9 | * @param value1 被加数 10 | * @param value2 加数 11 | * @return 两个参数的和 12 | */ 13 | public static double add(double value1, double value2) { 14 | BigDecimal b1 = new BigDecimal(Double.valueOf(value1)); 15 | BigDecimal b2 = new BigDecimal(Double.valueOf(value2)); 16 | return b1.add(b2).doubleValue(); 17 | } 18 | 19 | /** 20 | * 提供精确减法运算的sub方法 21 | * 22 | * @param value1 被减数 23 | * @param value2 减数 24 | * @return 两个参数的差 25 | */ 26 | public static double sub(double value1, double value2) { 27 | BigDecimal b1 = new BigDecimal(Double.valueOf(value1)); 28 | BigDecimal b2 = new BigDecimal(Double.valueOf(value2)); 29 | return b1.subtract(b2).doubleValue(); 30 | } 31 | 32 | /** 33 | * 提供精确乘法运算的mul方法 34 | * 35 | * @param value1 被乘数 36 | * @param value2 乘数 37 | * @return 两个参数的积 38 | */ 39 | public static double mul(double value1, double value2) { 40 | BigDecimal b1 = new BigDecimal(Double.valueOf(value1)); 41 | BigDecimal b2 = new BigDecimal(Double.valueOf(value2)); 42 | return b1.multiply(b2).doubleValue(); 43 | } 44 | 45 | /** 46 | * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指定精度,以后的数字四舍五入。 47 | * 48 | * @param value1 被除数 49 | * @param value2 除数 50 | * @param scale 表示表示需要精确到小数点以后几位。 51 | * @return 两个参数的商 52 | */ 53 | public static BigDecimal div(double value1, double value2, int scale) throws IllegalAccessException { 54 | if (scale < 0) { 55 | //如果精确范围小于0,抛出异常信息。 56 | throw new IllegalArgumentException("精确度不能小于0"); 57 | } else if (value2 == 0) { 58 | //如果除数为0,抛出异常信息。 59 | throw new IllegalArgumentException("除数不能为0"); 60 | } 61 | BigDecimal b1 = new BigDecimal(Double.valueOf(value1)); 62 | BigDecimal b2 = new BigDecimal(Double.valueOf(value2)); 63 | return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP); 64 | } 65 | 66 | /** 67 | * 提供精确的小数位四舍五入处理。 68 | * 69 | * @param v 需要四舍五入的数字 70 | * @param scale 小数点后保留几位 71 | * @return 四舍五入后的结果 72 | */ 73 | public static BigDecimal round(double v, int scale) { 74 | if (scale < 0) { 75 | throw new IllegalArgumentException("精确度不能小于0"); 76 | } 77 | BigDecimal b = new BigDecimal(Double.toString(v)); 78 | BigDecimal one = new BigDecimal("1"); 79 | return b.divide(one, scale, BigDecimal.ROUND_HALF_UP); 80 | } 81 | 82 | /** 83 | * 提供精确加法计算的add方法,确认精确度 84 | * 85 | * @param value1 被加数 86 | * @param value2 加数 87 | * @param scale 小数点后保留几位 88 | * @return 两个参数求和之后,按精度四舍五入的结果 89 | */ 90 | public static BigDecimal add(double value1, double value2, int scale) { 91 | return round(add(value1, value2), scale); 92 | } 93 | 94 | /** 95 | * 提供精确减法运算的sub方法,确认精确度 96 | * 97 | * @param value1 被减数 98 | * @param value2 减数 99 | * @param scale 小数点后保留几位 100 | * @return 两个参数的求差之后,按精度四舍五入的结果 101 | */ 102 | public static BigDecimal sub(double value1, double value2, int scale) { 103 | return round(sub(value1, value2), scale); 104 | } 105 | 106 | /** 107 | * 提供精确乘法运算的mul方法,确认精确度 108 | * 109 | * @param value1 被乘数 110 | * @param value2 乘数 111 | * @param scale 小数点后保留几位 112 | * @return 两个参数的乘积之后,按精度四舍五入的结果 113 | */ 114 | public static BigDecimal mul(double value1, double value2, int scale) { 115 | return round(mul(value1, value2), scale); 116 | } 117 | 118 | 119 | /** 120 | * 比较两个 BigDecimal 类型的数值 是否相等 121 | * 如果 其中一个 为null 返回 false 122 | * 如果 都为 null 返回 false 123 | * 如果 相等 返回 true 124 | * 如果 不等 返回 false 125 | */ 126 | public static boolean equals(BigDecimal b1, BigDecimal b2) { 127 | if (b1 == null || b2 == null) { 128 | return false; 129 | } 130 | int i = b1.compareTo(b2); 131 | if (i != 0) { 132 | return false; 133 | } 134 | return true; 135 | } 136 | 137 | public static BigDecimal add(BigDecimal... values) { 138 | if (values.length == 0) { 139 | return null; 140 | } 141 | BigDecimal sum = BigDecimal.valueOf(0); 142 | for (BigDecimal value : values) { 143 | if (value == null) { 144 | value = BigDecimal.valueOf(0); 145 | } 146 | sum = sum.add(value); 147 | } 148 | return sum; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | com.git.comm.utils 7 | commutils4j 8 | 0.1 9 | 10 | org.springframework.boot 11 | spring-boot-starter-parent 12 | 1.4.2.RELEASE 13 | 14 | 15 | 16 | 17 | 18 | junit 19 | junit 20 | 4.13.1 21 | 22 | 23 | 24 | com.github.pagehelper 25 | pagehelper 26 | 5.3.1 27 | 28 | 29 | 30 | org.modelmapper 31 | modelmapper 32 | 0.7.5 33 | 34 | 35 | 36 | org.slf4j 37 | slf4j-api 38 | 1.7.25 39 | 40 | 41 | 42 | org.codehaus.jackson 43 | jackson-core-asl 44 | 1.9.13 45 | 46 | 47 | org.codehaus.jackson 48 | jackson-mapper-asl 49 | 1.9.13 50 | 51 | 52 | 53 | com.alibaba 54 | fastjson 55 | 1.2.83 56 | 57 | 58 | 59 | ant 60 | ant 61 | 1.6.5 62 | 63 | 64 | 65 | javax.servlet 66 | servlet-api 67 | 2.5 68 | provided 69 | 70 | 71 | 72 | org.apache.commons 73 | commons-vfs2 74 | 2.0 75 | 76 | 77 | 78 | commons-io 79 | commons-io 80 | 2.7 81 | 82 | 83 | 84 | commons-httpclient 85 | commons-httpclient 86 | 3.0 87 | 88 | 89 | 90 | commons-net 91 | commons-net 92 | 3.9.0 93 | 94 | 95 | 96 | org.apache.commons 97 | commons-lang3 98 | 3.4 99 | 100 | 101 | 102 | com.google.collections 103 | google-collections 104 | 1.0 105 | 106 | 107 | 108 | org.apache.commons 109 | commons-lang3 110 | 3.4 111 | 112 | 113 | 114 | org.springframework.boot 115 | spring-boot-starter-redis 116 | 117 | 118 | 119 | org.apache.httpcomponents 120 | httpclient 121 | 4.5.13 122 | 123 | 124 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4string/StringUtils.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4string; 2 | 3 | import com.git.comm.utils.u4list.ListUtils; 4 | 5 | import java.io.UnsupportedEncodingException; 6 | import java.net.URLEncoder; 7 | import java.util.Collection; 8 | import java.util.UUID; 9 | import java.util.regex.Matcher; 10 | import java.util.regex.Pattern; 11 | 12 | public class StringUtils { 13 | 14 | /** 15 | * 判断一个或多个对象是否为空 16 | * 17 | * @param values 18 | * 可变参数,要判断的一个或多个对象 19 | * @return 只有要判断的一个对象都为空则返回true,否则返回false 20 | */ 21 | public static boolean isNull(Object... values) { 22 | if (!ListUtils.isNotNullAndNotEmpty(values)) { 23 | return true; 24 | } 25 | for (Object value : values) { 26 | boolean flag = false; 27 | if (value instanceof Object[]) { 28 | flag = !ListUtils.isNotNullAndNotEmpty((Object[]) value); 29 | } else if (value instanceof Collection) { 30 | flag = !ListUtils.isNotNullAndNotEmpty((Collection) value); 31 | } else if (value instanceof String) { 32 | flag = isOEmptyOrNull(value); 33 | } else { 34 | flag = (null == value); 35 | } 36 | if (flag) { 37 | return true; 38 | } 39 | } 40 | return false; 41 | } 42 | 43 | public static boolean isOEmptyOrNull(Object o) { 44 | return o == null ? true : isSEmptyOrNull(o.toString()); 45 | } 46 | 47 | public static boolean isSEmptyOrNull(String s) { 48 | return trimAndNullAsEmpty(s).length() <= 0 ? true : false; 49 | } 50 | 51 | public static String trimAndNullAsEmpty(String s) { 52 | if (s != null && !s.trim().equals("-")) { 53 | return s.trim(); 54 | } else { 55 | return ""; 56 | } 57 | } 58 | 59 | 60 | /** 61 | * 判断字符串是否为空 62 | * @param str 63 | * @return 64 | */ 65 | public static boolean isEmpty(String str) { 66 | return str == null || str.length() == 0; 67 | } 68 | 69 | /** 70 | * 判断字符串是否为空 71 | * @param str 72 | * @return 73 | */ 74 | public static boolean isSNullOrEmpty(String str) { 75 | return str == null || "".equals(str.trim()); 76 | } 77 | 78 | /** 79 | * 判断字符串组是否为空 80 | * @param strs 81 | * @return 82 | */ 83 | public static boolean isStrsEmptyOrNull(String... strs) { 84 | if(strs != null && strs.length != 0) { 85 | String[] arr$ = strs; 86 | int len$ = strs.length; 87 | for(int i$ = 0; i$ < len$; ++i$) { 88 | String str = arr$[i$]; 89 | if(str == null || str.trim().equals("")) { 90 | return true; 91 | } 92 | } 93 | return false; 94 | } else { 95 | return true; 96 | } 97 | } 98 | 99 | /** 100 | * 随机获取32位UUID字符串(无中划线) 101 | * 102 | * @return UUID字符串 103 | */ 104 | public static String getUUID() { 105 | String uuid = UUID.randomUUID().toString(); 106 | return uuid.substring(0, 8) + uuid.substring(9, 13) + uuid.substring(14, 18) + uuid.substring(19, 23) + uuid.substring(24); 107 | } 108 | 109 | /** 110 | * 返回一个utf8的字符串 111 | * @param str 112 | * @return str 113 | */ 114 | public static String utf8Encode(String str) { 115 | if (!isEmpty(str) && str.getBytes().length != str.length()) { 116 | try { 117 | return URLEncoder.encode(str, "UTF-8"); 118 | } catch (UnsupportedEncodingException e) { 119 | throw new RuntimeException("UnsupportedEncodingException occurred.", e); 120 | } 121 | } 122 | return str; 123 | } 124 | 125 | /** 126 | * 返回一个html 127 | * @param href 128 | * @return href 129 | */ 130 | public static String getHrefInnerHtml(String href) { 131 | if (isEmpty(href)) { 132 | return ""; 133 | } 134 | String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*"; 135 | Pattern hrefPattern = Pattern.compile(hrefReg, 136 | Pattern.CASE_INSENSITIVE); 137 | Matcher hrefMatcher = hrefPattern.matcher(href); 138 | if (hrefMatcher.matches()) { 139 | return hrefMatcher.group(1); 140 | } 141 | return href; 142 | } 143 | 144 | /** 145 | * 如果字符串没有超过最长显示长度返回原字符串,否则从开头截取指定长度并加...返回。 146 | * @param str 147 | * @param length 148 | * @return str 149 | */ 150 | public static String trimString(String str, int length) { 151 | if (str == null) { 152 | return ""; 153 | } else if (str.length() > length) { 154 | return str.substring(0, length - 3) + "..."; 155 | } else { 156 | return str; 157 | } 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/u4string/ChinaUpperCaseUtil.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.u4string; 2 | 3 | /** 4 | * Double类型金额 转换 大写中文 5 | * 6 | * @see "调用方法getTurnMoneys"({@String}) 7 | */ 8 | public class ChinaUpperCaseUtil { 9 | 10 | private static String moneyD = new String(); 11 | private static String moneyBefore = new String(); 12 | private static String moneyAfter = new String(); 13 | 14 | 15 | public static String getTurns(int i) { 16 | switch (i) { 17 | case 2: 18 | moneyD = "拾"; 19 | break; 20 | case 3: 21 | moneyD = "佰"; 22 | break; 23 | case 4: 24 | moneyD = "仟"; 25 | break; 26 | case 5: 27 | moneyD = "万"; 28 | break; 29 | case 6: 30 | moneyD = "拾"; 31 | break; 32 | case 7: 33 | moneyD = "佰"; 34 | break; 35 | case 8: 36 | moneyD = "仟"; 37 | break; 38 | case 9: 39 | moneyD = "亿"; 40 | break; 41 | case 10: 42 | moneyD = "拾"; 43 | break; 44 | 45 | case 11: 46 | moneyD = "佰"; 47 | break; 48 | case 12: 49 | moneyD = "仟"; 50 | break; 51 | } 52 | return moneyD.toString(); 53 | } 54 | 55 | public static String getTurnMoneyWord(String s) { 56 | switch (Integer.parseInt(s)) { 57 | case 0: 58 | return "零"; 59 | case 1: 60 | return "壹"; 61 | case 2: 62 | return "贰"; 63 | case 3: 64 | return "叁"; 65 | case 4: 66 | return "肆"; 67 | case 5: 68 | return "伍"; 69 | case 6: 70 | return "陆"; 71 | case 7: 72 | return "柒"; 73 | case 8: 74 | return "捌"; 75 | } 76 | return "玖"; 77 | } 78 | 79 | public static String getTurnMoneys(String money) { 80 | StringBuffer moneyA = new StringBuffer(); 81 | try { 82 | if (money.indexOf("-") != -1) { 83 | moneyA.append("负"); 84 | money = money.substring(money.indexOf("-") + 1, money.length()); 85 | } 86 | if (money.indexOf(".") != -1) { 87 | moneyBefore = money.substring(0, money.indexOf(".")); 88 | moneyAfter = money.substring(money.indexOf(".") + 1, money.length()); 89 | } else { 90 | moneyBefore = money; 91 | moneyAfter = "none"; 92 | } 93 | if (moneyBefore.length() == 1 && moneyBefore.charAt(0) == '0' && moneyAfter == "none") { 94 | moneyA.append("零"); 95 | moneyA.append("圆"); 96 | } else { 97 | for (int i = 0; i < moneyBefore.length(); i++) { 98 | if (moneyBefore.charAt(i) != '0') { 99 | moneyA.append( 100 | getTurnMoneyWord(String.valueOf(String.valueOf(moneyBefore.charAt(i))).concat(""))); 101 | if (moneyBefore.length() != i + 1) 102 | moneyA.append(getTurns(moneyBefore.length() - i)); 103 | continue; 104 | } 105 | if (moneyBefore.length() - i == 5 && (moneyBefore.length() > 5 && moneyBefore.length() <= 9)) 106 | moneyA.append("万"); 107 | if (moneyBefore.length() - i == 9 && moneyBefore.length() > 9) 108 | moneyA.append("亿"); 109 | if (moneyBefore.length() != i + 1 && moneyBefore.charAt(i + 1) != '0') 110 | moneyA.append("零"); 111 | } 112 | if (moneyBefore.length() != 1 || moneyBefore.charAt(0) != '0') { 113 | moneyA.append("圆"); 114 | } 115 | } 116 | if (!moneyAfter.equals("none")) { 117 | for (int i = 0; i < moneyAfter.length(); i++) { 118 | if (i == 0) 119 | if (moneyAfter.charAt(i) != '0') { 120 | moneyA.append( 121 | getTurnMoneyWord(String.valueOf(String.valueOf(moneyAfter.charAt(i))).concat(""))); 122 | 123 | moneyA.append("角"); 124 | } else if (moneyBefore.charAt(0) != '0' && moneyAfter.length() > 1 125 | && (!moneyAfter.equals("00"))) 126 | moneyA.append("零"); 127 | if (i == 1 && moneyAfter.charAt(i) != '0') { 128 | moneyA.append( 129 | getTurnMoneyWord(String.valueOf(String.valueOf(moneyAfter.charAt(i))).concat(""))); 130 | moneyA.append("分"); 131 | } 132 | } 133 | 134 | } 135 | if (moneyAfter.equals("none") || moneyAfter.length() != 2 || moneyAfter.equals("00")) 136 | moneyA.append("整"); 137 | } catch (Exception exception) { 138 | } 139 | return moneyA.toString(); 140 | } 141 | 142 | } 143 | 144 | -------------------------------------------------------------------------------- /src/main/java/com/git/comm/utils/xss/XssShieldUtil.java: -------------------------------------------------------------------------------- 1 | package com.git.comm.utils.xss; 2 | 3 | 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | import java.util.regex.Matcher; 9 | import java.util.regex.Pattern; 10 | 11 | /** 12 | * 处理非法字符 13 | * Thanks: http://blog.csdn.net/catoop/ 14 | */ 15 | public class XssShieldUtil { 16 | 17 | private static List patterns = null; 18 | 19 | private static List getXssPatternList() { 20 | List ret = new ArrayList(); 21 | ret.add(new Object[]{"<(no)?script[^>]*>.*?", Pattern.CASE_INSENSITIVE}); 22 | ret.add(new Object[]{"eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL}); 23 | ret.add(new Object[]{"expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL}); 24 | ret.add(new Object[]{"(javascript:|vbscript:|view-source:)*", Pattern.CASE_INSENSITIVE}); 25 | ret.add(new Object[]{"<(\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL}); 26 | ret.add(new Object[]{"(window\\.location|window\\.|\\.location|document\\.cookie|document\\.|alert\\(.*?\\)|window\\.open\\()*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL}); 27 | ret.add(new Object[]{"<+\\s*\\w*\\s*(oncontrolselect|oncopy|oncut|ondataavailable|ondatasetchanged|ondatasetcomplete|ondblclick|ondeactivate|ondrag|ondragend|ondragenter|ondragleave|ondragover|ondragstart|ondrop|onerror=|onerroupdate|onfilterchange|onfinish|onfocus|onfocusin|onfocusout|onhelp|onkeydown|onkeypress|onkeyup|onlayoutcomplete|onload|onlosecapture|onmousedown|onmouseenter|onmouseleave|onmousemove|onmousout|onmouseover|onmouseup|onmousewheel|onmove|onmoveend|onmovestart|onabort|onactivate|onafterprint|onafterupdate|onbefore|onbeforeactivate|onbeforecopy|onbeforecut|onbeforedeactivate|onbeforeeditocus|onbeforepaste|onbeforeprint|onbeforeunload|onbeforeupdate|onblur|onbounce|oncellchange|onchange|onclick|oncontextmenu|onpaste|onpropertychange|onreadystatechange|onreset|onresize|onresizend|onresizestart|onrowenter|onrowexit|onrowsdelete|onrowsinserted|onscroll|onselect|onselectionchange|onselectstart|onstart|onstop|onsubmit|onunload)+\\s*=+", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL}); 28 | return ret; 29 | } 30 | 31 | private static List getPatterns() { 32 | if (patterns == null) { 33 | List list = new ArrayList(); 34 | String regex = null; 35 | Integer flag = null; 36 | int arrLength = 0; 37 | for(Object[] arr : getXssPatternList()) { 38 | arrLength = arr.length; 39 | for(int i = 0; i < arrLength; i++) { 40 | regex = (String)arr[0]; 41 | flag = (Integer)arr[1]; 42 | list.add(Pattern.compile(regex, flag)); 43 | } 44 | } 45 | patterns = list; 46 | } 47 | return patterns; 48 | } 49 | 50 | public static String stripXss(String value) { 51 | if(StringUtils.isNotBlank(value)) { 52 | Matcher matcher = null; 53 | for(Pattern pattern : getPatterns()) { 54 | matcher = pattern.matcher(value); 55 | if(matcher.find()) { 56 | value = matcher.replaceAll(""); 57 | } 58 | } 59 | value = value.replaceAll("<", "<").replaceAll(">", ">"); 60 | } 61 | return value; 62 | } 63 | 64 | private static void testXss() { 65 | String value = null; 66 | value = XssShieldUtil.stripXss(""); 67 | System.out.println("type-1: '" + value + "'"); 68 | 69 | value = XssShieldUtil.stripXss(""); 70 | System.out.println("type-2: '" + value + "'"); 71 | 72 | value = XssShieldUtil.stripXss(""); 73 | System.out.println("type-3: '" + value + "'"); 74 | 75 | value = XssShieldUtil.stripXss(" eval(abc);"); 76 | System.out.println("type-4: '" + value + "'"); 77 | 78 | value = XssShieldUtil.stripXss(" expression(abc);"); 79 | System.out.println("type-5: '" + value + "'"); 80 | 81 | value = XssShieldUtil.stripXss(""); 82 | System.out.println("type-6: '" + value + "'"); 83 | 84 | value = XssShieldUtil.stripXss(""); 85 | System.out.println("type-7: '" + value + "'"); 86 | 87 | value = XssShieldUtil.stripXss(""); 88 | System.out.println("type-8: '" + value + "'"); 89 | 90 | value = XssShieldUtil.stripXss("