| | |
| | | <version>${tomcat.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- Springdoc webmvc ä¾èµé
ç½® --> |
| | | <dependency> |
| | | <groupId>org.springdoc</groupId> |
| | | <artifactId>springdoc-openapi-ui</artifactId> |
| | | <version>${springdoc.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- pagehelper å页æä»¶ --> |
| | | <dependency> |
| | | <groupId>com.github.pagehelper</groupId> |
| | | <artifactId>pagehelper-spring-boot-starter</artifactId> |
| | | <version>${pagehelper.boot.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- io常ç¨å·¥å
·ç±» --> |
| | | <dependency> |
| | | <groupId>commons-io</groupId> |
| | | <artifactId>commons-io</artifactId> |
| | | <version>${commons.io.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- excelå·¥å
· --> |
| | | <dependency> |
| | | <groupId>org.apache.poi</groupId> |
| | | <artifactId>poi-ooxml</artifactId> |
| | | <version>${poi.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- 代ç çæä½¿ç¨æ¨¡æ¿ --> |
| | | <dependency> |
| | | <groupId>org.apache.velocity</groupId> |
| | | <artifactId>velocity-engine-core</artifactId> |
| | | <version>${velocity.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- JSON è§£æå¨åçæå¨ --> |
| | | <dependency> |
| | | <groupId>com.alibaba.fastjson2</groupId> |
| | |
| | | <version>${fastjson.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- JWT --> |
| | | <dependency> |
| | | <groupId>io.jsonwebtoken</groupId> |
| | | <artifactId>jjwt</artifactId> |
| | | <version>${jjwt.version}</version> |
| | | </dependency> |
| | | |
| | | <!-- 线ç¨ä¼ éå¼ --> |
| | | <dependency> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>transmittable-thread-local</artifactId> |
| | | <version>${transmittable-thread-local.version}</version> |
| | | </dependency> |
| | | </dependencies> |
| | | </dependencyManagement> |
| | | |
| | | <modules> |
| | | <module>se-common</module> |
| | | <module>se-gateway</module> |
| | | <module>se-system</module> |
| | | </modules> |
¶Ô±ÈÐÂÎļþ |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| | | xmlns="http://maven.apache.org/POM/4.0.0" |
| | | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| | | <parent> |
| | | <groupId>com.terra</groupId> |
| | | <artifactId>terra</artifactId> |
| | | <version>1.0.2</version> |
| | | </parent> |
| | | <modelVersion>4.0.0</modelVersion> |
| | | |
| | | <artifactId>se-common</artifactId> |
| | | |
| | | <description> |
| | | éç¨æ¨¡å |
| | | </description> |
| | | |
| | | <dependencies> |
| | | |
| | | <!-- SpringCloud Openfeign --> |
| | | <dependency> |
| | | <groupId>org.springframework.cloud</groupId> |
| | | <artifactId>spring-cloud-starter-openfeign</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- SpringCloud Loadbalancer --> |
| | | <dependency> |
| | | <groupId>org.springframework.cloud</groupId> |
| | | <artifactId>spring-cloud-starter-loadbalancer</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Spring Context Support --> |
| | | <dependency> |
| | | <groupId>org.springframework</groupId> |
| | | <artifactId>spring-context-support</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Spring Web --> |
| | | <dependency> |
| | | <groupId>org.springframework</groupId> |
| | | <artifactId>spring-web</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Transmittable ThreadLocal --> |
| | | <dependency> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>transmittable-thread-local</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Pagehelper --> |
| | | <dependency> |
| | | <groupId>com.github.pagehelper</groupId> |
| | | <artifactId>pagehelper-spring-boot-starter</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Hibernate Validator --> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-validation</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Jackson --> |
| | | <dependency> |
| | | <groupId>com.fasterxml.jackson.core</groupId> |
| | | <artifactId>jackson-databind</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Alibaba Fastjson --> |
| | | <dependency> |
| | | <groupId>com.alibaba.fastjson2</groupId> |
| | | <artifactId>fastjson2</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Jwt --> |
| | | <dependency> |
| | | <groupId>io.jsonwebtoken</groupId> |
| | | <artifactId>jjwt</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Jaxb --> |
| | | <dependency> |
| | | <groupId>javax.xml.bind</groupId> |
| | | <artifactId>jaxb-api</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Apache Lang3 --> |
| | | <dependency> |
| | | <groupId>org.apache.commons</groupId> |
| | | <artifactId>commons-lang3</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Commons Io --> |
| | | <dependency> |
| | | <groupId>commons-io</groupId> |
| | | <artifactId>commons-io</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- excelå·¥å
· --> |
| | | <dependency> |
| | | <groupId>org.apache.poi</groupId> |
| | | <artifactId>poi-ooxml</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- Java Servlet --> |
| | | <dependency> |
| | | <groupId>javax.servlet</groupId> |
| | | <artifactId>javax.servlet-api</artifactId> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | </project> |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.annotation; |
| | | |
| | | import java.lang.annotation.ElementType; |
| | | import java.lang.annotation.Retention; |
| | | import java.lang.annotation.RetentionPolicy; |
| | | import java.lang.annotation.Target; |
| | | import java.math.BigDecimal; |
| | | import org.apache.poi.ss.usermodel.HorizontalAlignment; |
| | | import org.apache.poi.ss.usermodel.IndexedColors; |
| | | import com.terra.common.utils.poi.ExcelHandlerAdapter; |
| | | |
| | | /** |
| | | * èªå®ä¹å¯¼åºExcelæ°æ®æ³¨è§£ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Target(ElementType.FIELD) |
| | | public @interface Excel |
| | | { |
| | | /** |
| | | * å¯¼åºæ¶å¨excelä¸æåº |
| | | */ |
| | | public int sort() default Integer.MAX_VALUE; |
| | | |
| | | /** |
| | | * 导åºå°Excelä¸çåå. |
| | | */ |
| | | public String name() default ""; |
| | | |
| | | /** |
| | | * æ¥ææ ¼å¼, å¦: yyyy-MM-dd |
| | | */ |
| | | public String dateFormat() default ""; |
| | | |
| | | /** |
| | | * 读åå
å®¹è½¬è¡¨è¾¾å¼ (å¦: 0=ç·,1=女,2=æªç¥) |
| | | */ |
| | | public String readConverterExp() default ""; |
| | | |
| | | /** |
| | | * åé符ï¼è¯»åå符串ç»å
容 |
| | | */ |
| | | public String separator() default ","; |
| | | |
| | | /** |
| | | * BigDecimal 精度 é»è®¤:-1(é»è®¤ä¸å¼å¯BigDecimalæ ¼å¼å) |
| | | */ |
| | | public int scale() default -1; |
| | | |
| | | /** |
| | | * BigDecimal èå
¥è§å é»è®¤:BigDecimal.ROUND_HALF_EVEN |
| | | */ |
| | | public int roundingMode() default BigDecimal.ROUND_HALF_EVEN; |
| | | |
| | | /** |
| | | * å¯¼åºæ¶å¨excel䏿¯ä¸ªåçé«åº¦ |
| | | */ |
| | | public double height() default 14; |
| | | |
| | | /** |
| | | * å¯¼åºæ¶å¨excel䏿¯ä¸ªåç宽度 |
| | | */ |
| | | public double width() default 16; |
| | | |
| | | /** |
| | | * æååç¼,å¦% 90 åæ90% |
| | | */ |
| | | public String suffix() default ""; |
| | | |
| | | /** |
| | | * å½å¼ä¸ºç©ºæ¶,åæ®µçé»è®¤å¼ |
| | | */ |
| | | public String defaultValue() default ""; |
| | | |
| | | /** |
| | | * æç¤ºä¿¡æ¯ |
| | | */ |
| | | public String prompt() default ""; |
| | | |
| | | /** |
| | | * æ¯å¦å
许å
容æ¢è¡ |
| | | */ |
| | | public boolean wrapText() default false; |
| | | |
| | | /** |
| | | * 设置åªè½éæ©ä¸è½è¾å
¥çåå
容. |
| | | */ |
| | | public String[] combo() default {}; |
| | | |
| | | /** |
| | | * æ¯å¦éè¦çºµååå¹¶åå
æ ¼,åºå¯¹éæ±:嫿listéååå
æ ¼) |
| | | */ |
| | | public boolean needMerge() default false; |
| | | |
| | | /** |
| | | * æ¯å¦å¯¼åºæ°æ®,åºå¯¹éæ±:ææ¶æä»¬éè¦å¯¼åºä¸ä»½æ¨¡æ¿,è¿æ¯æ é¢éè¦ä½å
容éè¦ç¨æ·æå·¥å¡«å. |
| | | */ |
| | | public boolean isExport() default true; |
| | | |
| | | /** |
| | | * å¦ä¸ä¸ªç±»ä¸ç屿§åç§°,æ¯æå¤çº§è·å,以尿°ç¹éå¼ |
| | | */ |
| | | public String targetAttr() default ""; |
| | | |
| | | /** |
| | | * æ¯å¦èªå¨ç»è®¡æ°æ®,卿å追å ä¸è¡ç»è®¡æ°æ®æ»å |
| | | */ |
| | | public boolean isStatistics() default false; |
| | | |
| | | /** |
| | | * 导åºç±»åï¼0æ°å 1åç¬¦ä¸²ï¼ |
| | | */ |
| | | public ColumnType cellType() default ColumnType.STRING; |
| | | |
| | | /** |
| | | * 导åºåå¤´èæ¯é¢è² |
| | | */ |
| | | public IndexedColors headerBackgroundColor() default IndexedColors.GREY_50_PERCENT; |
| | | |
| | | /** |
| | | * 导åºå头åä½é¢è² |
| | | */ |
| | | public IndexedColors headerColor() default IndexedColors.WHITE; |
| | | |
| | | /** |
| | | * 导åºåå
æ ¼èæ¯é¢è² |
| | | */ |
| | | public IndexedColors backgroundColor() default IndexedColors.WHITE; |
| | | |
| | | /** |
| | | * 导åºåå
æ ¼åä½é¢è² |
| | | */ |
| | | public IndexedColors color() default IndexedColors.BLACK; |
| | | |
| | | /** |
| | | * 导åºåæ®µå¯¹é½æ¹å¼ |
| | | */ |
| | | public HorizontalAlignment align() default HorizontalAlignment.CENTER; |
| | | |
| | | /** |
| | | * èªå®ä¹æ°æ®å¤çå¨ |
| | | */ |
| | | public Class<?> handler() default ExcelHandlerAdapter.class; |
| | | |
| | | /** |
| | | * èªå®ä¹æ°æ®å¤çå¨åæ° |
| | | */ |
| | | public String[] args() default {}; |
| | | |
| | | /** |
| | | * åæ®µç±»åï¼0ï¼å¯¼åºå¯¼å
¥ï¼1ï¼ä»
导åºï¼2ï¼ä»
导å
¥ï¼ |
| | | */ |
| | | Type type() default Type.ALL; |
| | | |
| | | public enum Type |
| | | { |
| | | ALL(0), EXPORT(1), IMPORT(2); |
| | | private final int value; |
| | | |
| | | Type(int value) |
| | | { |
| | | this.value = value; |
| | | } |
| | | |
| | | public int value() |
| | | { |
| | | return this.value; |
| | | } |
| | | } |
| | | |
| | | public enum ColumnType |
| | | { |
| | | NUMERIC(0), STRING(1), IMAGE(2), TEXT(3); |
| | | private final int value; |
| | | |
| | | ColumnType(int value) |
| | | { |
| | | this.value = value; |
| | | } |
| | | |
| | | public int value() |
| | | { |
| | | return this.value; |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.annotation; |
| | | |
| | | import java.lang.annotation.ElementType; |
| | | import java.lang.annotation.Retention; |
| | | import java.lang.annotation.RetentionPolicy; |
| | | import java.lang.annotation.Target; |
| | | |
| | | /** |
| | | * Excel注解é |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Target(ElementType.FIELD) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | public @interface Excels |
| | | { |
| | | Excel[] value(); |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.constant; |
| | | |
| | | /** |
| | | * ç¼å常éä¿¡æ¯ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class CacheConstants |
| | | { |
| | | /** |
| | | * ç¼åæææï¼é»è®¤720ï¼åéï¼ |
| | | */ |
| | | public final static long EXPIRATION = 720; |
| | | |
| | | /** |
| | | * ç¼åå·æ°æ¶é´ï¼é»è®¤120ï¼åéï¼ |
| | | */ |
| | | public final static long REFRESH_TIME = 120; |
| | | |
| | | /** |
| | | * å¯ç æå¤§éè¯¯æ¬¡æ° |
| | | */ |
| | | public final static int PASSWORD_MAX_RETRY_COUNT = 5; |
| | | |
| | | /** |
| | | * å¯ç é宿¶é´ï¼é»è®¤10ï¼åéï¼ |
| | | */ |
| | | public final static long PASSWORD_LOCK_TIME = 10; |
| | | |
| | | /** |
| | | * æéç¼ååç¼ |
| | | */ |
| | | public final static String LOGIN_TOKEN_KEY = "login_tokens:"; |
| | | |
| | | /** |
| | | * éªè¯ç redis key |
| | | */ |
| | | public static final String CAPTCHA_CODE_KEY = "captcha_codes:"; |
| | | |
| | | /** |
| | | * åæ°ç®¡ç cache key |
| | | */ |
| | | public static final String SYS_CONFIG_KEY = "sys_config:"; |
| | | |
| | | /** |
| | | * åå
¸ç®¡ç cache key |
| | | */ |
| | | public static final String SYS_DICT_KEY = "sys_dict:"; |
| | | |
| | | /** |
| | | * ç»å½è´¦æ·å¯ç éè¯¯æ¬¡æ° redis key |
| | | */ |
| | | public static final String PWD_ERR_CNT_KEY = "pwd_err_cnt:"; |
| | | |
| | | /** |
| | | * ç»å½IPé»åå cache key |
| | | */ |
| | | public static final String SYS_LOGIN_BLACKIPLIST = SYS_CONFIG_KEY + "sys.login.blackIPList"; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.constant; |
| | | |
| | | /** |
| | | * éç¨å¸¸éä¿¡æ¯ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class Constants |
| | | { |
| | | /** |
| | | * UTF-8 å符é |
| | | */ |
| | | public static final String UTF8 = "UTF-8"; |
| | | |
| | | /** |
| | | * GBK å符é |
| | | */ |
| | | public static final String GBK = "GBK"; |
| | | |
| | | /** |
| | | * www主å |
| | | */ |
| | | public static final String WWW = "www."; |
| | | |
| | | /** |
| | | * RMI è¿ç¨æ¹æ³è°ç¨ |
| | | */ |
| | | public static final String LOOKUP_RMI = "rmi:"; |
| | | |
| | | /** |
| | | * LDAP è¿ç¨æ¹æ³è°ç¨ |
| | | */ |
| | | public static final String LOOKUP_LDAP = "ldap:"; |
| | | |
| | | /** |
| | | * LDAPS è¿ç¨æ¹æ³è°ç¨ |
| | | */ |
| | | public static final String LOOKUP_LDAPS = "ldaps:"; |
| | | |
| | | /** |
| | | * httpè¯·æ± |
| | | */ |
| | | public static final String HTTP = "http://"; |
| | | |
| | | /** |
| | | * httpsè¯·æ± |
| | | */ |
| | | public static final String HTTPS = "https://"; |
| | | |
| | | /** |
| | | * æåæ è®° |
| | | */ |
| | | public static final Integer SUCCESS = 200; |
| | | |
| | | /** |
| | | * 失败æ è®° |
| | | */ |
| | | public static final Integer FAIL = 500; |
| | | |
| | | /** |
| | | * ç»å½æåç¶æ |
| | | */ |
| | | public static final String LOGIN_SUCCESS_STATUS = "0"; |
| | | |
| | | /** |
| | | * ç»å½å¤±è´¥ç¶æ |
| | | */ |
| | | public static final String LOGIN_FAIL_STATUS = "1"; |
| | | |
| | | /** |
| | | * ç»å½æå |
| | | */ |
| | | public static final String LOGIN_SUCCESS = "Success"; |
| | | |
| | | /** |
| | | * 注é |
| | | */ |
| | | public static final String LOGOUT = "Logout"; |
| | | |
| | | /** |
| | | * 注å |
| | | */ |
| | | public static final String REGISTER = "Register"; |
| | | |
| | | /** |
| | | * ç»å½å¤±è´¥ |
| | | */ |
| | | public static final String LOGIN_FAIL = "Error"; |
| | | |
| | | /** |
| | | * å½åè®°å½èµ·å§ç´¢å¼ |
| | | */ |
| | | public static final String PAGE_NUM = "pageNum"; |
| | | |
| | | /** |
| | | * æ¯é¡µæ¾ç¤ºè®°å½æ° |
| | | */ |
| | | public static final String PAGE_SIZE = "pageSize"; |
| | | |
| | | /** |
| | | * æåºå |
| | | */ |
| | | public static final String ORDER_BY_COLUMN = "orderByColumn"; |
| | | |
| | | /** |
| | | * æåºçæ¹å "desc" æè
"asc". |
| | | */ |
| | | public static final String IS_ASC = "isAsc"; |
| | | |
| | | /** |
| | | * éªè¯ç æææï¼åéï¼ |
| | | */ |
| | | public static final long CAPTCHA_EXPIRATION = 2; |
| | | |
| | | /** |
| | | * èµæºæ å°è·¯å¾ åç¼ |
| | | */ |
| | | public static final String RESOURCE_PREFIX = "/profile"; |
| | | |
| | | /** |
| | | * èªå¨è¯å«json对象ç½ååé
ç½®ï¼ä»
å
许解æçå
åï¼èå´è¶å°è¶å®å
¨ï¼ |
| | | */ |
| | | public static final String[] JSON_WHITELIST_STR = { "org.springframework", "com.ruoyi" }; |
| | | |
| | | /** |
| | | * 宿¶ä»»å¡ç½ååé
ç½®ï¼ä»
å
许访é®çå
åï¼å¦å
¶ä»éè¦å¯ä»¥èªè¡æ·»å ï¼ |
| | | */ |
| | | public static final String[] JOB_WHITELIST_STR = { "com.ruoyi.job.task" }; |
| | | |
| | | /** |
| | | * 宿¶ä»»å¡è¿è§çå符 |
| | | */ |
| | | public static final String[] JOB_ERROR_STR = { "java.net.URL", "javax.naming.InitialContext", "org.yaml.snakeyaml", |
| | | "org.springframework", "org.apache", "com.terra.common.utils.file" }; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.constant; |
| | | |
| | | /** |
| | | * 代ç çæéç¨å¸¸é |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class GenConstants |
| | | { |
| | | /** å表ï¼å¢å æ¹æ¥ï¼ */ |
| | | public static final String TPL_CRUD = "crud"; |
| | | |
| | | /** æ 表ï¼å¢å æ¹æ¥ï¼ */ |
| | | public static final String TPL_TREE = "tree"; |
| | | |
| | | /** 主å表ï¼å¢å æ¹æ¥ï¼ */ |
| | | public static final String TPL_SUB = "sub"; |
| | | |
| | | /** æ ç¼ç åæ®µ */ |
| | | public static final String TREE_CODE = "treeCode"; |
| | | |
| | | /** æ ç¶ç¼ç åæ®µ */ |
| | | public static final String TREE_PARENT_CODE = "treeParentCode"; |
| | | |
| | | /** æ åç§°åæ®µ */ |
| | | public static final String TREE_NAME = "treeName"; |
| | | |
| | | /** ä¸çº§èåIDåæ®µ */ |
| | | public static final String PARENT_MENU_ID = "parentMenuId"; |
| | | |
| | | /** ä¸çº§èååç§°åæ®µ */ |
| | | public static final String PARENT_MENU_NAME = "parentMenuName"; |
| | | |
| | | /** æ°æ®åºå符串类å */ |
| | | public static final String[] COLUMNTYPE_STR = { "char", "varchar", "nvarchar", "varchar2" }; |
| | | |
| | | /** æ°æ®åºææ¬ç±»å */ |
| | | public static final String[] COLUMNTYPE_TEXT = { "tinytext", "text", "mediumtext", "longtext" }; |
| | | |
| | | /** æ°æ®åºæ¶é´ç±»å */ |
| | | public static final String[] COLUMNTYPE_TIME = { "datetime", "time", "date", "timestamp" }; |
| | | |
| | | /** æ°æ®åºæ°åç±»å */ |
| | | public static final String[] COLUMNTYPE_NUMBER = { "tinyint", "smallint", "mediumint", "int", "number", "integer", |
| | | "bit", "bigint", "float", "double", "decimal" }; |
| | | |
| | | /** 页é¢ä¸éè¦ç¼è¾å段 */ |
| | | public static final String[] COLUMNNAME_NOT_EDIT = { "id", "create_by", "create_time", "del_flag" }; |
| | | |
| | | /** 页é¢ä¸éè¦æ¾ç¤ºçåè¡¨åæ®µ */ |
| | | public static final String[] COLUMNNAME_NOT_LIST = { "id", "create_by", "create_time", "del_flag", "update_by", |
| | | "update_time" }; |
| | | |
| | | /** 页é¢ä¸éè¦æ¥è¯¢å段 */ |
| | | public static final String[] COLUMNNAME_NOT_QUERY = { "id", "create_by", "create_time", "del_flag", "update_by", |
| | | "update_time", "remark" }; |
| | | |
| | | /** Entityåºç±»å段 */ |
| | | public static final String[] BASE_ENTITY = { "createBy", "createTime", "updateBy", "updateTime", "remark" }; |
| | | |
| | | /** Treeåºç±»å段 */ |
| | | public static final String[] TREE_ENTITY = { "parentName", "parentId", "orderNum", "ancestors" }; |
| | | |
| | | /** ææ¬æ¡ */ |
| | | public static final String HTML_INPUT = "input"; |
| | | |
| | | /** ææ¬å */ |
| | | public static final String HTML_TEXTAREA = "textarea"; |
| | | |
| | | /** ä¸ææ¡ */ |
| | | public static final String HTML_SELECT = "select"; |
| | | |
| | | /** åéæ¡ */ |
| | | public static final String HTML_RADIO = "radio"; |
| | | |
| | | /** å¤éæ¡ */ |
| | | public static final String HTML_CHECKBOX = "checkbox"; |
| | | |
| | | /** æ¥ææ§ä»¶ */ |
| | | public static final String HTML_DATETIME = "datetime"; |
| | | |
| | | /** å¾çä¸ä¼ æ§ä»¶ */ |
| | | public static final String HTML_IMAGE_UPLOAD = "imageUpload"; |
| | | |
| | | /** æä»¶ä¸ä¼ æ§ä»¶ */ |
| | | public static final String HTML_FILE_UPLOAD = "fileUpload"; |
| | | |
| | | /** å¯ææ¬æ§ä»¶ */ |
| | | public static final String HTML_EDITOR = "editor"; |
| | | |
| | | /** å符串类å */ |
| | | public static final String TYPE_STRING = "String"; |
| | | |
| | | /** æ´å */ |
| | | public static final String TYPE_INTEGER = "Integer"; |
| | | |
| | | /** é¿æ´å */ |
| | | public static final String TYPE_LONG = "Long"; |
| | | |
| | | /** æµ®ç¹å */ |
| | | public static final String TYPE_DOUBLE = "Double"; |
| | | |
| | | /** é«ç²¾åº¦è®¡ç®ç±»å */ |
| | | public static final String TYPE_BIGDECIMAL = "BigDecimal"; |
| | | |
| | | /** æ¶é´ç±»å */ |
| | | public static final String TYPE_DATE = "Date"; |
| | | |
| | | /** æ¨¡ç³æ¥è¯¢ */ |
| | | public static final String QUERY_LIKE = "LIKE"; |
| | | |
| | | /** ç¸çæ¥è¯¢ */ |
| | | public static final String QUERY_EQ = "EQ"; |
| | | |
| | | /** éè¦ */ |
| | | public static final String REQUIRE = "1"; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.constant; |
| | | |
| | | /** |
| | | * è¿åç¶æç |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class HttpStatus |
| | | { |
| | | /** |
| | | * æä½æå |
| | | */ |
| | | public static final int SUCCESS = 200; |
| | | |
| | | /** |
| | | * 对象å建æå |
| | | */ |
| | | public static final int CREATED = 201; |
| | | |
| | | /** |
| | | * 请æ±å·²ç»è¢«æ¥å |
| | | */ |
| | | public static final int ACCEPTED = 202; |
| | | |
| | | /** |
| | | * æä½å·²ç»æ§è¡æåï¼ä½æ¯æ²¡æè¿åæ°æ® |
| | | */ |
| | | public static final int NO_CONTENT = 204; |
| | | |
| | | /** |
| | | * èµæºå·²è¢«ç§»é¤ |
| | | */ |
| | | public static final int MOVED_PERM = 301; |
| | | |
| | | /** |
| | | * éå®å |
| | | */ |
| | | public static final int SEE_OTHER = 303; |
| | | |
| | | /** |
| | | * èµæºæ²¡æè¢«ä¿®æ¹ |
| | | */ |
| | | public static final int NOT_MODIFIED = 304; |
| | | |
| | | /** |
| | | * åæ°å表é误ï¼ç¼ºå°ï¼æ ¼å¼ä¸å¹é
ï¼ |
| | | */ |
| | | public static final int BAD_REQUEST = 400; |
| | | |
| | | /** |
| | | * æªææ |
| | | */ |
| | | public static final int UNAUTHORIZED = 401; |
| | | |
| | | /** |
| | | * 访é®åéï¼ææè¿æ |
| | | */ |
| | | public static final int FORBIDDEN = 403; |
| | | |
| | | /** |
| | | * èµæºï¼æå¡æªæ¾å° |
| | | */ |
| | | public static final int NOT_FOUND = 404; |
| | | |
| | | /** |
| | | * ä¸å
许çhttpæ¹æ³ |
| | | */ |
| | | public static final int BAD_METHOD = 405; |
| | | |
| | | /** |
| | | * èµæºå²çªï¼æè
èµæºè¢«é |
| | | */ |
| | | public static final int CONFLICT = 409; |
| | | |
| | | /** |
| | | * 䏿¯æçæ°æ®ï¼åªä½ç±»å |
| | | */ |
| | | public static final int UNSUPPORTED_TYPE = 415; |
| | | |
| | | /** |
| | | * ç³»ç»å
é¨é误 |
| | | */ |
| | | public static final int ERROR = 500; |
| | | |
| | | /** |
| | | * æ¥å£æªå®ç° |
| | | */ |
| | | public static final int NOT_IMPLEMENTED = 501; |
| | | |
| | | /** |
| | | * ç³»ç»è¦åæ¶æ¯ |
| | | */ |
| | | public static final int WARN = 601; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.constant; |
| | | |
| | | /** |
| | | * ä»»å¡è°åº¦éç¨å¸¸é |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class ScheduleConstants |
| | | { |
| | | public static final String TASK_CLASS_NAME = "TASK_CLASS_NAME"; |
| | | |
| | | /** æ§è¡ç®æ key */ |
| | | public static final String TASK_PROPERTIES = "TASK_PROPERTIES"; |
| | | |
| | | /** é»è®¤ */ |
| | | public static final String MISFIRE_DEFAULT = "0"; |
| | | |
| | | /** ç«å³è§¦åæ§è¡ */ |
| | | public static final String MISFIRE_IGNORE_MISFIRES = "1"; |
| | | |
| | | /** 触å䏿¬¡æ§è¡ */ |
| | | public static final String MISFIRE_FIRE_AND_PROCEED = "2"; |
| | | |
| | | /** ä¸è§¦åç«å³æ§è¡ */ |
| | | public static final String MISFIRE_DO_NOTHING = "3"; |
| | | |
| | | public enum Status |
| | | { |
| | | /** |
| | | * æ£å¸¸ |
| | | */ |
| | | NORMAL("0"), |
| | | /** |
| | | * æå |
| | | */ |
| | | PAUSE("1"); |
| | | |
| | | private String value; |
| | | |
| | | private Status(String value) |
| | | { |
| | | this.value = value; |
| | | } |
| | | |
| | | public String getValue() |
| | | { |
| | | return value; |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.constant; |
| | | |
| | | /** |
| | | * æéç¸å
³éç¨å¸¸é |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class SecurityConstants |
| | | { |
| | | /** |
| | | * ç¨æ·IDåæ®µ |
| | | */ |
| | | public static final String DETAILS_USER_ID = "user_id"; |
| | | |
| | | /** |
| | | * ç¨æ·ååæ®µ |
| | | */ |
| | | public static final String DETAILS_USERNAME = "username"; |
| | | |
| | | /** |
| | | * ææä¿¡æ¯å段 |
| | | */ |
| | | public static final String AUTHORIZATION_HEADER = "Authorization"; |
| | | |
| | | /** |
| | | * è¯·æ±æ¥æº |
| | | */ |
| | | public static final String FROM_SOURCE = "from-source"; |
| | | |
| | | /** |
| | | * å
é¨è¯·æ± |
| | | */ |
| | | public static final String INNER = "inner"; |
| | | |
| | | /** |
| | | * ç¨æ·æ è¯ |
| | | */ |
| | | public static final String USER_KEY = "user_key"; |
| | | |
| | | /** |
| | | * ç»å½ç¨æ· |
| | | */ |
| | | public static final String LOGIN_USER = "login_user"; |
| | | |
| | | /** |
| | | * è§è²æé |
| | | */ |
| | | public static final String ROLE_PERMISSION = "role_permission"; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.constant; |
| | | |
| | | /** |
| | | * æå¡åç§° |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class ServiceNameConstants |
| | | { |
| | | /** |
| | | * è®¤è¯æå¡çserviceid |
| | | */ |
| | | public static final String AUTH_SERVICE = "ruoyi-auth"; |
| | | |
| | | /** |
| | | * ç³»ç»æ¨¡åçserviceid |
| | | */ |
| | | public static final String SYSTEM_SERVICE = "ruoyi-system"; |
| | | |
| | | /** |
| | | * æä»¶æå¡çserviceid |
| | | */ |
| | | public static final String FILE_SERVICE = "ruoyi-file"; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.constant; |
| | | |
| | | /** |
| | | * TokençKey常é |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class TokenConstants |
| | | { |
| | | /** |
| | | * 令çåç¼ |
| | | */ |
| | | public static final String PREFIX = "Bearer "; |
| | | |
| | | /** |
| | | * 令çç§é¥ |
| | | */ |
| | | public final static String SECRET = "abcdefghijklmnopqrstuvwxyz"; |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.constant; |
| | | |
| | | /** |
| | | * ç¨æ·å¸¸éä¿¡æ¯ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class UserConstants |
| | | { |
| | | /** |
| | | * å¹³å°å
ç³»ç»ç¨æ·çå¯ä¸æ å¿ |
| | | */ |
| | | public static final String SYS_USER = "SYS_USER"; |
| | | |
| | | /** æ£å¸¸ç¶æ */ |
| | | public static final String NORMAL = "0"; |
| | | |
| | | /** å¼å¸¸ç¶æ */ |
| | | public static final String EXCEPTION = "1"; |
| | | |
| | | /** ç¨æ·å°ç¦ç¶æ */ |
| | | public static final String USER_DISABLE = "1"; |
| | | |
| | | /** è§è²æ£å¸¸ç¶æ */ |
| | | public static final String ROLE_NORMAL = "0"; |
| | | |
| | | /** è§è²å°ç¦ç¶æ */ |
| | | public static final String ROLE_DISABLE = "1"; |
| | | |
| | | /** é¨é¨æ£å¸¸ç¶æ */ |
| | | public static final String DEPT_NORMAL = "0"; |
| | | |
| | | /** é¨é¨åç¨ç¶æ */ |
| | | public static final String DEPT_DISABLE = "1"; |
| | | |
| | | /** åå
¸æ£å¸¸ç¶æ */ |
| | | public static final String DICT_NORMAL = "0"; |
| | | |
| | | /** æ¯å¦ä¸ºç³»ç»é»è®¤ï¼æ¯ï¼ */ |
| | | public static final String YES = "Y"; |
| | | |
| | | /** æ¯å¦èåå¤é¾ï¼æ¯ï¼ */ |
| | | public static final String YES_FRAME = "0"; |
| | | |
| | | /** æ¯å¦èåå¤é¾ï¼å¦ï¼ */ |
| | | public static final String NO_FRAME = "1"; |
| | | |
| | | /** èåç±»åï¼ç®å½ï¼ */ |
| | | public static final String TYPE_DIR = "M"; |
| | | |
| | | /** èåç±»åï¼èåï¼ */ |
| | | public static final String TYPE_MENU = "C"; |
| | | |
| | | /** èåç±»åï¼æé®ï¼ */ |
| | | public static final String TYPE_BUTTON = "F"; |
| | | |
| | | /** Layoutç»ä»¶æ è¯ */ |
| | | public final static String LAYOUT = "Layout"; |
| | | |
| | | /** ParentViewç»ä»¶æ è¯ */ |
| | | public final static String PARENT_VIEW = "ParentView"; |
| | | |
| | | /** InnerLinkç»ä»¶æ è¯ */ |
| | | public final static String INNER_LINK = "InnerLink"; |
| | | |
| | | /** æ ¡éªæ¯å¦å¯ä¸çè¿åæ è¯ */ |
| | | public final static boolean UNIQUE = true; |
| | | public final static boolean NOT_UNIQUE = false; |
| | | |
| | | /** |
| | | * ç¨æ·åé¿åº¦éå¶ |
| | | */ |
| | | public static final int USERNAME_MIN_LENGTH = 2; |
| | | |
| | | public static final int USERNAME_MAX_LENGTH = 20; |
| | | |
| | | /** |
| | | * å¯ç é¿åº¦éå¶ |
| | | */ |
| | | public static final int PASSWORD_MIN_LENGTH = 5; |
| | | |
| | | public static final int PASSWORD_MAX_LENGTH = 20; |
| | | |
| | | public static boolean isAdmin(Long userId) |
| | | { |
| | | return userId != null && 1L == userId; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.context; |
| | | |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import com.alibaba.ttl.TransmittableThreadLocal; |
| | | import com.terra.common.constant.SecurityConstants; |
| | | import com.terra.common.text.Convert; |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * è·åå½å线ç¨åéä¸ç ç¨æ·idãç¨æ·åç§°ãTokençä¿¡æ¯ |
| | | * 注æï¼ å¿
é¡»å¨ç½å
³éè¿è¯·æ±å¤´çæ¹æ³ä¼ å
¥ï¼åæ¶å¨HeaderInterceptoræ¦æªå¨è®¾ç½®å¼ã å¦åè¿éæ æ³è·å |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class SecurityContextHolder |
| | | { |
| | | private static final TransmittableThreadLocal<Map<String, Object>> THREAD_LOCAL = new TransmittableThreadLocal<>(); |
| | | |
| | | public static void set(String key, Object value) |
| | | { |
| | | Map<String, Object> map = getLocalMap(); |
| | | map.put(key, value == null ? StringUtils.EMPTY : value); |
| | | } |
| | | |
| | | public static String get(String key) |
| | | { |
| | | Map<String, Object> map = getLocalMap(); |
| | | return Convert.toStr(map.getOrDefault(key, StringUtils.EMPTY)); |
| | | } |
| | | |
| | | public static <T> T get(String key, Class<T> clazz) |
| | | { |
| | | Map<String, Object> map = getLocalMap(); |
| | | return StringUtils.cast(map.getOrDefault(key, null)); |
| | | } |
| | | |
| | | public static Map<String, Object> getLocalMap() |
| | | { |
| | | Map<String, Object> map = THREAD_LOCAL.get(); |
| | | if (map == null) |
| | | { |
| | | map = new ConcurrentHashMap<String, Object>(); |
| | | THREAD_LOCAL.set(map); |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | public static void setLocalMap(Map<String, Object> threadLocalMap) |
| | | { |
| | | THREAD_LOCAL.set(threadLocalMap); |
| | | } |
| | | |
| | | public static Long getUserId() |
| | | { |
| | | return Convert.toLong(get(SecurityConstants.DETAILS_USER_ID), 0L); |
| | | } |
| | | |
| | | public static void setUserId(String account) |
| | | { |
| | | set(SecurityConstants.DETAILS_USER_ID, account); |
| | | } |
| | | |
| | | public static String getUserName() |
| | | { |
| | | return get(SecurityConstants.DETAILS_USERNAME); |
| | | } |
| | | |
| | | public static void setUserName(String username) |
| | | { |
| | | set(SecurityConstants.DETAILS_USERNAME, username); |
| | | } |
| | | |
| | | public static String getUserKey() |
| | | { |
| | | return get(SecurityConstants.USER_KEY); |
| | | } |
| | | |
| | | public static void setUserKey(String userKey) |
| | | { |
| | | set(SecurityConstants.USER_KEY, userKey); |
| | | } |
| | | |
| | | public static String getPermission() |
| | | { |
| | | return get(SecurityConstants.ROLE_PERMISSION); |
| | | } |
| | | |
| | | public static void setPermission(String permissions) |
| | | { |
| | | set(SecurityConstants.ROLE_PERMISSION, permissions); |
| | | } |
| | | |
| | | public static void remove() |
| | | { |
| | | THREAD_LOCAL.remove(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.domain; |
| | | |
| | | import java.io.Serializable; |
| | | import com.terra.common.constant.Constants; |
| | | |
| | | /** |
| | | * ååºä¿¡æ¯ä¸»ä½ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class R<T> implements Serializable |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** æå */ |
| | | public static final int SUCCESS = Constants.SUCCESS; |
| | | |
| | | /** 失败 */ |
| | | public static final int FAIL = Constants.FAIL; |
| | | |
| | | private int code; |
| | | |
| | | private String msg; |
| | | |
| | | private T data; |
| | | |
| | | public static <T> R<T> ok() |
| | | { |
| | | return restResult(null, SUCCESS, null); |
| | | } |
| | | |
| | | public static <T> R<T> ok(T data) |
| | | { |
| | | return restResult(data, SUCCESS, null); |
| | | } |
| | | |
| | | public static <T> R<T> ok(T data, String msg) |
| | | { |
| | | return restResult(data, SUCCESS, msg); |
| | | } |
| | | |
| | | public static <T> R<T> fail() |
| | | { |
| | | return restResult(null, FAIL, null); |
| | | } |
| | | |
| | | public static <T> R<T> fail(String msg) |
| | | { |
| | | return restResult(null, FAIL, msg); |
| | | } |
| | | |
| | | public static <T> R<T> fail(T data) |
| | | { |
| | | return restResult(data, FAIL, null); |
| | | } |
| | | |
| | | public static <T> R<T> fail(T data, String msg) |
| | | { |
| | | return restResult(data, FAIL, msg); |
| | | } |
| | | |
| | | public static <T> R<T> fail(int code, String msg) |
| | | { |
| | | return restResult(null, code, msg); |
| | | } |
| | | |
| | | private static <T> R<T> restResult(T data, int code, String msg) |
| | | { |
| | | R<T> apiResult = new R<>(); |
| | | apiResult.setCode(code); |
| | | apiResult.setData(data); |
| | | apiResult.setMsg(msg); |
| | | return apiResult; |
| | | } |
| | | |
| | | public int getCode() |
| | | { |
| | | return code; |
| | | } |
| | | |
| | | public void setCode(int code) |
| | | { |
| | | this.code = code; |
| | | } |
| | | |
| | | public String getMsg() |
| | | { |
| | | return msg; |
| | | } |
| | | |
| | | public void setMsg(String msg) |
| | | { |
| | | this.msg = msg; |
| | | } |
| | | |
| | | public T getData() |
| | | { |
| | | return data; |
| | | } |
| | | |
| | | public void setData(T data) |
| | | { |
| | | this.data = data; |
| | | } |
| | | |
| | | public static <T> Boolean isError(R<T> ret) |
| | | { |
| | | return !isSuccess(ret); |
| | | } |
| | | |
| | | public static <T> Boolean isSuccess(R<T> ret) |
| | | { |
| | | return R.SUCCESS == ret.getCode(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.enums; |
| | | |
| | | /** |
| | | * ç¨æ·ç¶æ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public enum UserStatus |
| | | { |
| | | OK("0", "æ£å¸¸"), DISABLE("1", "åç¨"), DELETED("2", "å é¤"); |
| | | |
| | | private final String code; |
| | | private final String info; |
| | | |
| | | UserStatus(String code, String info) |
| | | { |
| | | this.code = code; |
| | | this.info = info; |
| | | } |
| | | |
| | | public String getCode() |
| | | { |
| | | return code; |
| | | } |
| | | |
| | | public String getInfo() |
| | | { |
| | | return info; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception; |
| | | |
| | | /** |
| | | * éªè¯ç é误å¼å¸¸ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class CaptchaException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public CaptchaException(String msg) |
| | | { |
| | | super(msg); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception; |
| | | |
| | | /** |
| | | * æ£æ¥å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class CheckedException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public CheckedException(String message) |
| | | { |
| | | super(message); |
| | | } |
| | | |
| | | public CheckedException(Throwable cause) |
| | | { |
| | | super(cause); |
| | | } |
| | | |
| | | public CheckedException(String message, Throwable cause) |
| | | { |
| | | super(message, cause); |
| | | } |
| | | |
| | | public CheckedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) |
| | | { |
| | | super(message, cause, enableSuppression, writableStackTrace); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception; |
| | | |
| | | /** |
| | | * æ¼ç¤ºæ¨¡å¼å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class DemoModeException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public DemoModeException() |
| | | { |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception; |
| | | |
| | | /** |
| | | * å
¨å±å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class GlobalException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * é误æç¤º |
| | | */ |
| | | private String message; |
| | | |
| | | /** |
| | | * é误æç»ï¼å
é¨è°è¯é误 |
| | | * |
| | | * å {@link CommonResult#getDetailMessage()} ä¸è´ç设计 |
| | | */ |
| | | private String detailMessage; |
| | | |
| | | /** |
| | | * 空æé æ¹æ³ï¼é¿å
ååºååé®é¢ |
| | | */ |
| | | public GlobalException() |
| | | { |
| | | } |
| | | |
| | | public GlobalException(String message) |
| | | { |
| | | this.message = message; |
| | | } |
| | | |
| | | public String getDetailMessage() |
| | | { |
| | | return detailMessage; |
| | | } |
| | | |
| | | public GlobalException setDetailMessage(String detailMessage) |
| | | { |
| | | this.detailMessage = detailMessage; |
| | | return this; |
| | | } |
| | | |
| | | @Override |
| | | public String getMessage() |
| | | { |
| | | return message; |
| | | } |
| | | |
| | | public GlobalException setMessage(String message) |
| | | { |
| | | this.message = message; |
| | | return this; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception; |
| | | |
| | | /** |
| | | * å
é¨è®¤è¯å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class InnerAuthException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public InnerAuthException(String message) |
| | | { |
| | | super(message); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception; |
| | | |
| | | /** |
| | | * æéå¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class PreAuthorizeException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public PreAuthorizeException() |
| | | { |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception; |
| | | |
| | | /** |
| | | * ä¸å¡å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public final class ServiceException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * é误ç |
| | | */ |
| | | private Integer code; |
| | | |
| | | /** |
| | | * é误æç¤º |
| | | */ |
| | | private String message; |
| | | |
| | | /** |
| | | * é误æç»ï¼å
é¨è°è¯é误 |
| | | * |
| | | * å {@link CommonResult#getDetailMessage()} ä¸è´ç设计 |
| | | */ |
| | | private String detailMessage; |
| | | |
| | | /** |
| | | * 空æé æ¹æ³ï¼é¿å
ååºååé®é¢ |
| | | */ |
| | | public ServiceException() |
| | | { |
| | | } |
| | | |
| | | public ServiceException(String message) |
| | | { |
| | | this.message = message; |
| | | } |
| | | |
| | | public ServiceException(String message, Integer code) |
| | | { |
| | | this.message = message; |
| | | this.code = code; |
| | | } |
| | | |
| | | public String getDetailMessage() |
| | | { |
| | | return detailMessage; |
| | | } |
| | | |
| | | @Override |
| | | public String getMessage() |
| | | { |
| | | return message; |
| | | } |
| | | |
| | | public Integer getCode() |
| | | { |
| | | return code; |
| | | } |
| | | |
| | | public ServiceException setMessage(String message) |
| | | { |
| | | this.message = message; |
| | | return this; |
| | | } |
| | | |
| | | public ServiceException setDetailMessage(String detailMessage) |
| | | { |
| | | this.detailMessage = detailMessage; |
| | | return this; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception; |
| | | |
| | | /** |
| | | * å·¥å
·ç±»å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class UtilException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 8247610319171014183L; |
| | | |
| | | public UtilException(Throwable e) |
| | | { |
| | | super(e.getMessage(), e); |
| | | } |
| | | |
| | | public UtilException(String message) |
| | | { |
| | | super(message); |
| | | } |
| | | |
| | | public UtilException(String message, Throwable throwable) |
| | | { |
| | | super(message, throwable); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.auth; |
| | | |
| | | /** |
| | | * æªè½éè¿çç»å½è®¤è¯å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class NotLoginException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public NotLoginException(String message) |
| | | { |
| | | super(message); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.auth; |
| | | |
| | | import org.apache.commons.lang3.StringUtils; |
| | | |
| | | /** |
| | | * æªè½éè¿çæé认è¯å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class NotPermissionException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public NotPermissionException(String permission) |
| | | { |
| | | super(permission); |
| | | } |
| | | |
| | | public NotPermissionException(String[] permissions) |
| | | { |
| | | super(StringUtils.join(permissions, ",")); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.auth; |
| | | |
| | | import org.apache.commons.lang3.StringUtils; |
| | | |
| | | /** |
| | | * æªè½éè¿çè§è²è®¤è¯å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class NotRoleException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public NotRoleException(String role) |
| | | { |
| | | super(role); |
| | | } |
| | | |
| | | public NotRoleException(String[] roles) |
| | | { |
| | | super(StringUtils.join(roles, ",")); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.base; |
| | | |
| | | /** |
| | | * åºç¡å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class BaseException extends RuntimeException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * æå±æ¨¡å |
| | | */ |
| | | private String module; |
| | | |
| | | /** |
| | | * é误ç |
| | | */ |
| | | private String code; |
| | | |
| | | /** |
| | | * é误ç 对åºçåæ° |
| | | */ |
| | | private Object[] args; |
| | | |
| | | /** |
| | | * éè¯¯æ¶æ¯ |
| | | */ |
| | | private String defaultMessage; |
| | | |
| | | public BaseException(String module, String code, Object[] args, String defaultMessage) |
| | | { |
| | | this.module = module; |
| | | this.code = code; |
| | | this.args = args; |
| | | this.defaultMessage = defaultMessage; |
| | | } |
| | | |
| | | public BaseException(String module, String code, Object[] args) |
| | | { |
| | | this(module, code, args, null); |
| | | } |
| | | |
| | | public BaseException(String module, String defaultMessage) |
| | | { |
| | | this(module, null, null, defaultMessage); |
| | | } |
| | | |
| | | public BaseException(String code, Object[] args) |
| | | { |
| | | this(null, code, args, null); |
| | | } |
| | | |
| | | public BaseException(String defaultMessage) |
| | | { |
| | | this(null, null, null, defaultMessage); |
| | | } |
| | | |
| | | public String getModule() |
| | | { |
| | | return module; |
| | | } |
| | | |
| | | public String getCode() |
| | | { |
| | | return code; |
| | | } |
| | | |
| | | public Object[] getArgs() |
| | | { |
| | | return args; |
| | | } |
| | | |
| | | public String getDefaultMessage() |
| | | { |
| | | return defaultMessage; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.file; |
| | | |
| | | import com.terra.common.exception.base.BaseException; |
| | | |
| | | /** |
| | | * æä»¶ä¿¡æ¯å¼å¸¸ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class FileException extends BaseException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public FileException(String code, Object[] args, String msg) |
| | | { |
| | | super("file", code, args, msg); |
| | | } |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.file; |
| | | |
| | | /** |
| | | * æä»¶åç§°è¶
é¿éå¶å¼å¸¸ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class FileNameLengthLimitExceededException extends FileException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public FileNameLengthLimitExceededException(int defaultFileNameLength) |
| | | { |
| | | super("upload.filename.exceed.length", new Object[] { defaultFileNameLength }, "the filename is too long"); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.file; |
| | | |
| | | /** |
| | | * æä»¶å大å°éå¶å¼å¸¸ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class FileSizeLimitExceededException extends FileException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public FileSizeLimitExceededException(long defaultMaxSize) |
| | | { |
| | | super("upload.exceed.maxSize", new Object[] { defaultMaxSize }, "the filesize is too large"); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.file; |
| | | |
| | | import java.io.PrintStream; |
| | | import java.io.PrintWriter; |
| | | |
| | | /** |
| | | * æä»¶ä¸ä¼ å¼å¸¸ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class FileUploadException extends Exception |
| | | { |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | private final Throwable cause; |
| | | |
| | | public FileUploadException() |
| | | { |
| | | this(null, null); |
| | | } |
| | | |
| | | public FileUploadException(final String msg) |
| | | { |
| | | this(msg, null); |
| | | } |
| | | |
| | | public FileUploadException(String msg, Throwable cause) |
| | | { |
| | | super(msg); |
| | | this.cause = cause; |
| | | } |
| | | |
| | | @Override |
| | | public void printStackTrace(PrintStream stream) |
| | | { |
| | | super.printStackTrace(stream); |
| | | if (cause != null) |
| | | { |
| | | stream.println("Caused by:"); |
| | | cause.printStackTrace(stream); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void printStackTrace(PrintWriter writer) |
| | | { |
| | | super.printStackTrace(writer); |
| | | if (cause != null) |
| | | { |
| | | writer.println("Caused by:"); |
| | | cause.printStackTrace(writer); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public Throwable getCause() |
| | | { |
| | | return cause; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.file; |
| | | |
| | | import java.util.Arrays; |
| | | |
| | | /** |
| | | * æä»¶ä¸ä¼ 误å¼å¸¸ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class InvalidExtensionException extends FileUploadException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | private String[] allowedExtension; |
| | | private String extension; |
| | | private String filename; |
| | | |
| | | public InvalidExtensionException(String[] allowedExtension, String extension, String filename) |
| | | { |
| | | super("filename : [" + filename + "], extension : [" + extension + "], allowed extension : [" + Arrays.toString(allowedExtension) + "]"); |
| | | this.allowedExtension = allowedExtension; |
| | | this.extension = extension; |
| | | this.filename = filename; |
| | | } |
| | | |
| | | public String[] getAllowedExtension() |
| | | { |
| | | return allowedExtension; |
| | | } |
| | | |
| | | public String getExtension() |
| | | { |
| | | return extension; |
| | | } |
| | | |
| | | public String getFilename() |
| | | { |
| | | return filename; |
| | | } |
| | | |
| | | public static class InvalidImageExtensionException extends InvalidExtensionException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public InvalidImageExtensionException(String[] allowedExtension, String extension, String filename) |
| | | { |
| | | super(allowedExtension, extension, filename); |
| | | } |
| | | } |
| | | |
| | | public static class InvalidFlashExtensionException extends InvalidExtensionException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public InvalidFlashExtensionException(String[] allowedExtension, String extension, String filename) |
| | | { |
| | | super(allowedExtension, extension, filename); |
| | | } |
| | | } |
| | | |
| | | public static class InvalidMediaExtensionException extends InvalidExtensionException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public InvalidMediaExtensionException(String[] allowedExtension, String extension, String filename) |
| | | { |
| | | super(allowedExtension, extension, filename); |
| | | } |
| | | } |
| | | |
| | | public static class InvalidVideoExtensionException extends InvalidExtensionException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public InvalidVideoExtensionException(String[] allowedExtension, String extension, String filename) |
| | | { |
| | | super(allowedExtension, extension, filename); |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.job; |
| | | |
| | | /** |
| | | * 计åçç¥å¼å¸¸ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class TaskException extends Exception |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | private Code code; |
| | | |
| | | public TaskException(String msg, Code code) |
| | | { |
| | | this(msg, code, null); |
| | | } |
| | | |
| | | public TaskException(String msg, Code code, Exception nestedEx) |
| | | { |
| | | super(msg, nestedEx); |
| | | this.code = code; |
| | | } |
| | | |
| | | public Code getCode() |
| | | { |
| | | return code; |
| | | } |
| | | |
| | | public enum Code |
| | | { |
| | | TASK_EXISTS, NO_TASK_EXISTS, TASK_ALREADY_STARTED, UNKNOWN, CONFIG_ERROR, TASK_NODE_NOT_AVAILABLE |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.user; |
| | | |
| | | /** |
| | | * éªè¯ç 失æå¼å¸¸ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class CaptchaExpireException extends UserException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public CaptchaExpireException() |
| | | { |
| | | super("user.jcaptcha.expire", null); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.user; |
| | | |
| | | import com.terra.common.exception.base.BaseException; |
| | | |
| | | /** |
| | | * ç¨æ·ä¿¡æ¯å¼å¸¸ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class UserException extends BaseException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public UserException(String code, Object[] args) |
| | | { |
| | | super("user", code, args, null); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.exception.user; |
| | | |
| | | /** |
| | | * ç¨æ·å¯ç 䏿£ç¡®æä¸ç¬¦åè§èå¼å¸¸ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class UserPasswordNotMatchException extends UserException |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | public UserPasswordNotMatchException() |
| | | { |
| | | super("user.password.not.match", null); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.text; |
| | | |
| | | import java.nio.charset.Charset; |
| | | import java.nio.charset.StandardCharsets; |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * å符éå·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class CharsetKit |
| | | { |
| | | /** ISO-8859-1 */ |
| | | public static final String ISO_8859_1 = "ISO-8859-1"; |
| | | /** UTF-8 */ |
| | | public static final String UTF_8 = "UTF-8"; |
| | | /** GBK */ |
| | | public static final String GBK = "GBK"; |
| | | |
| | | /** ISO-8859-1 */ |
| | | public static final Charset CHARSET_ISO_8859_1 = Charset.forName(ISO_8859_1); |
| | | /** UTF-8 */ |
| | | public static final Charset CHARSET_UTF_8 = Charset.forName(UTF_8); |
| | | /** GBK */ |
| | | public static final Charset CHARSET_GBK = Charset.forName(GBK); |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºCharset对象 |
| | | * |
| | | * @param charset å符éï¼ä¸ºç©ºåè¿åé»è®¤å符é |
| | | * @return Charset |
| | | */ |
| | | public static Charset charset(String charset) |
| | | { |
| | | return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢å符串çå符éç¼ç |
| | | * |
| | | * @param source å符串 |
| | | * @param srcCharset æºå符éï¼é»è®¤ISO-8859-1 |
| | | * @param destCharset ç®æ å符éï¼é»è®¤UTF-8 |
| | | * @return 转æ¢åçå符é |
| | | */ |
| | | public static String convert(String source, String srcCharset, String destCharset) |
| | | { |
| | | return convert(source, Charset.forName(srcCharset), Charset.forName(destCharset)); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢å符串çå符éç¼ç |
| | | * |
| | | * @param source å符串 |
| | | * @param srcCharset æºå符éï¼é»è®¤ISO-8859-1 |
| | | * @param destCharset ç®æ å符éï¼é»è®¤UTF-8 |
| | | * @return 转æ¢åçå符é |
| | | */ |
| | | public static String convert(String source, Charset srcCharset, Charset destCharset) |
| | | { |
| | | if (null == srcCharset) |
| | | { |
| | | srcCharset = StandardCharsets.ISO_8859_1; |
| | | } |
| | | |
| | | if (null == destCharset) |
| | | { |
| | | destCharset = StandardCharsets.UTF_8; |
| | | } |
| | | |
| | | if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset)) |
| | | { |
| | | return source; |
| | | } |
| | | return new String(source.getBytes(srcCharset), destCharset); |
| | | } |
| | | |
| | | /** |
| | | * @return ç³»ç»å符éç¼ç |
| | | */ |
| | | public static String systemCharset() |
| | | { |
| | | return Charset.defaultCharset().name(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.text; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.BigInteger; |
| | | import java.math.RoundingMode; |
| | | import java.nio.ByteBuffer; |
| | | import java.nio.charset.Charset; |
| | | import java.text.NumberFormat; |
| | | import java.util.Set; |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * ç±»å转æ¢å¨ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class Convert |
| | | { |
| | | /** |
| | | * 转æ¢ä¸ºå符串<br> |
| | | * 妿ç»å®çå¼ä¸ºnullï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static String toStr(Object value, String defaultValue) |
| | | { |
| | | if (null == value) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof String) |
| | | { |
| | | return (String) value; |
| | | } |
| | | return value.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºå符串<br> |
| | | * 妿ç»å®çå¼ä¸º<code>null</code>ï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static String toStr(Object value) |
| | | { |
| | | return toStr(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºå符<br> |
| | | * 妿ç»å®çå¼ä¸ºnullï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Character toChar(Object value, Character defaultValue) |
| | | { |
| | | if (null == value) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof Character) |
| | | { |
| | | return (Character) value; |
| | | } |
| | | |
| | | final String valueStr = toStr(value, null); |
| | | return StringUtils.isEmpty(valueStr) ? defaultValue : valueStr.charAt(0); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºå符<br> |
| | | * 妿ç»å®çå¼ä¸º<code>null</code>ï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Character toChar(Object value) |
| | | { |
| | | return toChar(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºbyte<br> |
| | | * 妿ç»å®çå¼ä¸º<code>null</code>ï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Byte toByte(Object value, Byte defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof Byte) |
| | | { |
| | | return (Byte) value; |
| | | } |
| | | if (value instanceof Number) |
| | | { |
| | | return ((Number) value).byteValue(); |
| | | } |
| | | final String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | try |
| | | { |
| | | return Byte.parseByte(valueStr); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºbyte<br> |
| | | * 妿ç»å®çå¼ä¸º<code>null</code>ï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Byte toByte(Object value) |
| | | { |
| | | return toByte(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºShort<br> |
| | | * 妿ç»å®çå¼ä¸º<code>null</code>ï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Short toShort(Object value, Short defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof Short) |
| | | { |
| | | return (Short) value; |
| | | } |
| | | if (value instanceof Number) |
| | | { |
| | | return ((Number) value).shortValue(); |
| | | } |
| | | final String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | try |
| | | { |
| | | return Short.parseShort(valueStr.trim()); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºShort<br> |
| | | * 妿ç»å®çå¼ä¸º<code>null</code>ï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Short toShort(Object value) |
| | | { |
| | | return toShort(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºNumber<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Number toNumber(Object value, Number defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof Number) |
| | | { |
| | | return (Number) value; |
| | | } |
| | | final String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | try |
| | | { |
| | | return NumberFormat.getInstance().parse(valueStr); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºNumber<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Number toNumber(Object value) |
| | | { |
| | | return toNumber(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºint<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Integer toInt(Object value, Integer defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof Integer) |
| | | { |
| | | return (Integer) value; |
| | | } |
| | | if (value instanceof Number) |
| | | { |
| | | return ((Number) value).intValue(); |
| | | } |
| | | final String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | try |
| | | { |
| | | return Integer.parseInt(valueStr.trim()); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºint<br> |
| | | * 妿ç»å®çå¼ä¸º<code>null</code>ï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Integer toInt(Object value) |
| | | { |
| | | return toInt(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºIntegeræ°ç»<br> |
| | | * |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Integer[] toIntArray(String str) |
| | | { |
| | | return toIntArray(",", str); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºLongæ°ç»<br> |
| | | * |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Long[] toLongArray(String str) |
| | | { |
| | | return toLongArray(",", str); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºIntegeræ°ç»<br> |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Integer[] toIntArray(String split, String str) |
| | | { |
| | | if (StringUtils.isEmpty(str)) |
| | | { |
| | | return new Integer[] {}; |
| | | } |
| | | String[] arr = str.split(split); |
| | | final Integer[] ints = new Integer[arr.length]; |
| | | for (int i = 0; i < arr.length; i++) |
| | | { |
| | | final Integer v = toInt(arr[i], 0); |
| | | ints[i] = v; |
| | | } |
| | | return ints; |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºLongæ°ç»<br> |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Long[] toLongArray(String split, String str) |
| | | { |
| | | if (StringUtils.isEmpty(str)) |
| | | { |
| | | return new Long[] {}; |
| | | } |
| | | String[] arr = str.split(split); |
| | | final Long[] longs = new Long[arr.length]; |
| | | for (int i = 0; i < arr.length; i++) |
| | | { |
| | | final Long v = toLong(arr[i], null); |
| | | longs[i] = v; |
| | | } |
| | | return longs; |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºStringæ°ç»<br> |
| | | * |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static String[] toStrArray(String str) |
| | | { |
| | | if (StringUtils.isEmpty(str)) |
| | | { |
| | | return new String[] {}; |
| | | } |
| | | return toStrArray(",", str); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºStringæ°ç»<br> |
| | | * |
| | | * @param split åé符 |
| | | * @param str 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static String[] toStrArray(String split, String str) |
| | | { |
| | | return str.split(split); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºlong<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Long toLong(Object value, Long defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof Long) |
| | | { |
| | | return (Long) value; |
| | | } |
| | | if (value instanceof Number) |
| | | { |
| | | return ((Number) value).longValue(); |
| | | } |
| | | final String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | try |
| | | { |
| | | // æ¯æç§å¦è®¡æ°æ³ |
| | | return new BigDecimal(valueStr.trim()).longValue(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºlong<br> |
| | | * 妿ç»å®çå¼ä¸º<code>null</code>ï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Long toLong(Object value) |
| | | { |
| | | return toLong(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºdouble<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Double toDouble(Object value, Double defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof Double) |
| | | { |
| | | return (Double) value; |
| | | } |
| | | if (value instanceof Number) |
| | | { |
| | | return ((Number) value).doubleValue(); |
| | | } |
| | | final String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | try |
| | | { |
| | | // æ¯æç§å¦è®¡æ°æ³ |
| | | return new BigDecimal(valueStr.trim()).doubleValue(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºdouble<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Double toDouble(Object value) |
| | | { |
| | | return toDouble(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºFloat<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Float toFloat(Object value, Float defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof Float) |
| | | { |
| | | return (Float) value; |
| | | } |
| | | if (value instanceof Number) |
| | | { |
| | | return ((Number) value).floatValue(); |
| | | } |
| | | final String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | try |
| | | { |
| | | return Float.parseFloat(valueStr.trim()); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºFloat<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Float toFloat(Object value) |
| | | { |
| | | return toFloat(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºboolean<br> |
| | | * Stringæ¯æçå¼ä¸ºï¼trueãfalseãyesãokãnoã1ã0ãæ¯ãå¦, 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Boolean toBool(Object value, Boolean defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof Boolean) |
| | | { |
| | | return (Boolean) value; |
| | | } |
| | | String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | valueStr = valueStr.trim().toLowerCase(); |
| | | switch (valueStr) |
| | | { |
| | | case "true": |
| | | case "yes": |
| | | case "ok": |
| | | case "1": |
| | | case "æ¯": |
| | | return true; |
| | | case "false": |
| | | case "no": |
| | | case "0": |
| | | case "å¦": |
| | | return false; |
| | | default: |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºboolean<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static Boolean toBool(Object value) |
| | | { |
| | | return toBool(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºEnum对象<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * |
| | | * @param clazz EnumçClass |
| | | * @param value å¼ |
| | | * @param defaultValue é»è®¤å¼ |
| | | * @return Enum |
| | | */ |
| | | public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (clazz.isAssignableFrom(value.getClass())) |
| | | { |
| | | @SuppressWarnings("unchecked") |
| | | E myE = (E) value; |
| | | return myE; |
| | | } |
| | | final String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | try |
| | | { |
| | | return Enum.valueOf(clazz, valueStr); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºEnum对象<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * |
| | | * @param clazz EnumçClass |
| | | * @param value å¼ |
| | | * @return Enum |
| | | */ |
| | | public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value) |
| | | { |
| | | return toEnum(clazz, value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºBigInteger<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static BigInteger toBigInteger(Object value, BigInteger defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof BigInteger) |
| | | { |
| | | return (BigInteger) value; |
| | | } |
| | | if (value instanceof Long) |
| | | { |
| | | return BigInteger.valueOf((Long) value); |
| | | } |
| | | final String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | try |
| | | { |
| | | return new BigInteger(valueStr); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºBigInteger<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<code>null</code><br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static BigInteger toBigInteger(Object value) |
| | | { |
| | | return toBigInteger(value, null); |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºBigDecimal<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @param defaultValue 转æ¢é误æ¶çé»è®¤å¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) |
| | | { |
| | | if (value == null) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | if (value instanceof BigDecimal) |
| | | { |
| | | return (BigDecimal) value; |
| | | } |
| | | if (value instanceof Long) |
| | | { |
| | | return new BigDecimal((Long) value); |
| | | } |
| | | if (value instanceof Double) |
| | | { |
| | | return BigDecimal.valueOf((Double) value); |
| | | } |
| | | if (value instanceof Integer) |
| | | { |
| | | return new BigDecimal((Integer) value); |
| | | } |
| | | final String valueStr = toStr(value, null); |
| | | if (StringUtils.isEmpty(valueStr)) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | try |
| | | { |
| | | return new BigDecimal(valueStr); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return defaultValue; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转æ¢ä¸ºBigDecimal<br> |
| | | * 妿ç»å®çå¼ä¸ºç©ºï¼æè
转æ¢å¤±è´¥ï¼è¿åé»è®¤å¼<br> |
| | | * 转æ¢å¤±è´¥ä¸ä¼æ¥é |
| | | * |
| | | * @param value 被转æ¢çå¼ |
| | | * @return ç»æ |
| | | */ |
| | | public static BigDecimal toBigDecimal(Object value) |
| | | { |
| | | return toBigDecimal(value, null); |
| | | } |
| | | |
| | | /** |
| | | * å°å¯¹è±¡è½¬ä¸ºå符串<br> |
| | | * 1ãByteæ°ç»åByteBufferä¼è¢«è½¬æ¢ä¸ºå¯¹åºåç¬¦ä¸²çæ°ç» 2ã对象æ°ç»ä¼è°ç¨Arrays.toStringæ¹æ³ |
| | | * |
| | | * @param obj 对象 |
| | | * @return å符串 |
| | | */ |
| | | public static String utf8Str(Object obj) |
| | | { |
| | | return str(obj, CharsetKit.CHARSET_UTF_8); |
| | | } |
| | | |
| | | /** |
| | | * å°å¯¹è±¡è½¬ä¸ºå符串<br> |
| | | * 1ãByteæ°ç»åByteBufferä¼è¢«è½¬æ¢ä¸ºå¯¹åºåç¬¦ä¸²çæ°ç» 2ã对象æ°ç»ä¼è°ç¨Arrays.toStringæ¹æ³ |
| | | * |
| | | * @param obj 对象 |
| | | * @param charsetName å符é |
| | | * @return å符串 |
| | | */ |
| | | public static String str(Object obj, String charsetName) |
| | | { |
| | | return str(obj, Charset.forName(charsetName)); |
| | | } |
| | | |
| | | /** |
| | | * å°å¯¹è±¡è½¬ä¸ºå符串<br> |
| | | * 1ãByteæ°ç»åByteBufferä¼è¢«è½¬æ¢ä¸ºå¯¹åºåç¬¦ä¸²çæ°ç» 2ã对象æ°ç»ä¼è°ç¨Arrays.toStringæ¹æ³ |
| | | * |
| | | * @param obj 对象 |
| | | * @param charset å符é |
| | | * @return å符串 |
| | | */ |
| | | public static String str(Object obj, Charset charset) |
| | | { |
| | | if (null == obj) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | if (obj instanceof String) |
| | | { |
| | | return (String) obj; |
| | | } |
| | | else if (obj instanceof byte[] || obj instanceof Byte[]) |
| | | { |
| | | if (obj instanceof byte[]) |
| | | { |
| | | return str((byte[]) obj, charset); |
| | | } |
| | | else |
| | | { |
| | | Byte[] bytes = (Byte[]) obj; |
| | | int length = bytes.length; |
| | | byte[] dest = new byte[length]; |
| | | for (int i = 0; i < length; i++) |
| | | { |
| | | dest[i] = bytes[i]; |
| | | } |
| | | return str(dest, charset); |
| | | } |
| | | } |
| | | else if (obj instanceof ByteBuffer) |
| | | { |
| | | return str((ByteBuffer) obj, charset); |
| | | } |
| | | return obj.toString(); |
| | | } |
| | | |
| | | /** |
| | | * å°byteæ°ç»è½¬ä¸ºå符串 |
| | | * |
| | | * @param bytes byteæ°ç» |
| | | * @param charset å符é |
| | | * @return å符串 |
| | | */ |
| | | public static String str(byte[] bytes, String charset) |
| | | { |
| | | return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset)); |
| | | } |
| | | |
| | | /** |
| | | * è§£ç åèç |
| | | * |
| | | * @param data å符串 |
| | | * @param charset å符éï¼å¦ææ¤å段为空ï¼åè§£ç çç»æåå³äºå¹³å° |
| | | * @return è§£ç åçå符串 |
| | | */ |
| | | public static String str(byte[] data, Charset charset) |
| | | { |
| | | if (data == null) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | if (null == charset) |
| | | { |
| | | return new String(data); |
| | | } |
| | | return new String(data, charset); |
| | | } |
| | | |
| | | /** |
| | | * å°ç¼ç çbyteBufferæ°æ®è½¬æ¢ä¸ºå符串 |
| | | * |
| | | * @param data æ°æ® |
| | | * @param charset å符éï¼å¦æä¸ºç©ºä½¿ç¨å½åç³»ç»å符é |
| | | * @return å符串 |
| | | */ |
| | | public static String str(ByteBuffer data, String charset) |
| | | { |
| | | if (data == null) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | return str(data, Charset.forName(charset)); |
| | | } |
| | | |
| | | /** |
| | | * å°ç¼ç çbyteBufferæ°æ®è½¬æ¢ä¸ºå符串 |
| | | * |
| | | * @param data æ°æ® |
| | | * @param charset å符éï¼å¦æä¸ºç©ºä½¿ç¨å½åç³»ç»å符é |
| | | * @return å符串 |
| | | */ |
| | | public static String str(ByteBuffer data, Charset charset) |
| | | { |
| | | if (null == charset) |
| | | { |
| | | charset = Charset.defaultCharset(); |
| | | } |
| | | return charset.decode(data).toString(); |
| | | } |
| | | |
| | | // ----------------------------------------------------------------------- å
¨è§åè§è½¬æ¢ |
| | | /** |
| | | * åè§è½¬å
¨è§ |
| | | * |
| | | * @param input String. |
| | | * @return å
¨è§å符串. |
| | | */ |
| | | public static String toSBC(String input) |
| | | { |
| | | return toSBC(input, null); |
| | | } |
| | | |
| | | /** |
| | | * åè§è½¬å
¨è§ |
| | | * |
| | | * @param input String |
| | | * @param notConvertSet 䏿¿æ¢çå符éå |
| | | * @return å
¨è§å符串. |
| | | */ |
| | | public static String toSBC(String input, Set<Character> notConvertSet) |
| | | { |
| | | char[] c = input.toCharArray(); |
| | | for (int i = 0; i < c.length; i++) |
| | | { |
| | | if (null != notConvertSet && notConvertSet.contains(c[i])) |
| | | { |
| | | // è·³è¿ä¸æ¿æ¢çå符 |
| | | continue; |
| | | } |
| | | |
| | | if (c[i] == ' ') |
| | | { |
| | | c[i] = '\u3000'; |
| | | } |
| | | else if (c[i] < '\177') |
| | | { |
| | | c[i] = (char) (c[i] + 65248); |
| | | |
| | | } |
| | | } |
| | | return new String(c); |
| | | } |
| | | |
| | | /** |
| | | * å
¨è§è½¬åè§ |
| | | * |
| | | * @param input String. |
| | | * @return åè§å符串 |
| | | */ |
| | | public static String toDBC(String input) |
| | | { |
| | | return toDBC(input, null); |
| | | } |
| | | |
| | | /** |
| | | * æ¿æ¢å
¨è§ä¸ºåè§ |
| | | * |
| | | * @param text ææ¬ |
| | | * @param notConvertSet 䏿¿æ¢çå符éå |
| | | * @return æ¿æ¢åçå符 |
| | | */ |
| | | public static String toDBC(String text, Set<Character> notConvertSet) |
| | | { |
| | | char[] c = text.toCharArray(); |
| | | for (int i = 0; i < c.length; i++) |
| | | { |
| | | if (null != notConvertSet && notConvertSet.contains(c[i])) |
| | | { |
| | | // è·³è¿ä¸æ¿æ¢çå符 |
| | | continue; |
| | | } |
| | | |
| | | if (c[i] == '\u3000') |
| | | { |
| | | c[i] = ' '; |
| | | } |
| | | else if (c[i] > '\uFF00' && c[i] < '\uFF5F') |
| | | { |
| | | c[i] = (char) (c[i] - 65248); |
| | | } |
| | | } |
| | | return new String(c); |
| | | } |
| | | |
| | | /** |
| | | * æ°åéé¢å¤§åè½¬æ¢ å
åä¸ªå®æ´çç¶åå°å¦é¶æ¾æ¿æ¢æé¶ |
| | | * |
| | | * @param n æ°å |
| | | * @return 䏿大忰å |
| | | */ |
| | | public static String digitUppercase(double n) |
| | | { |
| | | String[] fraction = { "è§", "å" }; |
| | | String[] digit = { "é¶", "壹", "è´°", "å", "è", "ä¼", "é", "æ", "æ", "ç" }; |
| | | String[][] unit = { { "å
", "ä¸", "亿" }, { "", "æ¾", "ä½°", "ä»" } }; |
| | | |
| | | String head = n < 0 ? "è´" : ""; |
| | | n = Math.abs(n); |
| | | |
| | | String s = ""; |
| | | for (int i = 0; i < fraction.length; i++) |
| | | { |
| | | // ä¼ådouble计ç®ç²¾åº¦ä¸¢å¤±é®é¢ |
| | | BigDecimal nNum = new BigDecimal(n); |
| | | BigDecimal decimal = new BigDecimal(10); |
| | | BigDecimal scale = nNum.multiply(decimal).setScale(2, RoundingMode.HALF_EVEN); |
| | | double d = scale.doubleValue(); |
| | | s += (digit[(int) (Math.floor(d * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(é¶.)+", ""); |
| | | } |
| | | if (s.length() < 1) |
| | | { |
| | | s = "æ´"; |
| | | } |
| | | int integerPart = (int) Math.floor(n); |
| | | |
| | | for (int i = 0; i < unit[0].length && integerPart > 0; i++) |
| | | { |
| | | String p = ""; |
| | | for (int j = 0; j < unit[1].length && n > 0; j++) |
| | | { |
| | | p = digit[integerPart % 10] + unit[1][j] + p; |
| | | integerPart = integerPart / 10; |
| | | } |
| | | s = p.replaceAll("(é¶.)*é¶$", "").replaceAll("^$", "é¶") + unit[0][i] + s; |
| | | } |
| | | return head + s.replaceAll("(é¶.)*é¶å
", "å
").replaceFirst("(é¶.)+", "").replaceAll("(é¶.)+", "é¶").replaceAll("^æ´$", "é¶å
æ´"); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.text; |
| | | |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * åç¬¦ä¸²æ ¼å¼å |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class StrFormatter |
| | | { |
| | | public static final String EMPTY_JSON = "{}"; |
| | | public static final char C_BACKSLASH = '\\'; |
| | | public static final char C_DELIM_START = '{'; |
| | | public static final char C_DELIM_END = '}'; |
| | | |
| | | /** |
| | | * æ ¼å¼åå符串<br> |
| | | * æ¤æ¹æ³åªæ¯ç®åå°å ä½ç¬¦ {} æç
§é¡ºåºæ¿æ¢ä¸ºåæ°<br> |
| | | * 妿æ³è¾åº {} ä½¿ç¨ \\è½¬ä¹ { å³å¯ï¼å¦ææ³è¾åº {} ä¹åç \ 使ç¨å转ä¹ç¬¦ \\\\ å³å¯<br> |
| | | * ä¾ï¼<br> |
| | | * é常使ç¨ï¼format("this is {} for {}", "a", "b") -> this is a for b<br> |
| | | * 转ä¹{}ï¼ format("this is \\{} for {}", "a", "b") -> this is \{} for a<br> |
| | | * 转ä¹\ï¼ format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> |
| | | * |
| | | * @param strPattern åç¬¦ä¸²æ¨¡æ¿ |
| | | * @param argArray åæ°å表 |
| | | * @return ç»æ |
| | | */ |
| | | public static String format(final String strPattern, final Object... argArray) |
| | | { |
| | | if (StringUtils.isEmpty(strPattern) || StringUtils.isEmpty(argArray)) |
| | | { |
| | | return strPattern; |
| | | } |
| | | final int strPatternLength = strPattern.length(); |
| | | |
| | | // åå§åå®ä¹å¥½çé¿åº¦ä»¥è·å¾æ´å¥½çæ§è½ |
| | | StringBuilder sbuf = new StringBuilder(strPatternLength + 50); |
| | | |
| | | int handledPosition = 0; |
| | | int delimIndex;// å ä½ç¬¦æå¨ä½ç½® |
| | | for (int argIndex = 0; argIndex < argArray.length; argIndex++) |
| | | { |
| | | delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition); |
| | | if (delimIndex == -1) |
| | | { |
| | | if (handledPosition == 0) |
| | | { |
| | | return strPattern; |
| | | } |
| | | else |
| | | { // å符串模æ¿å©ä½é¨åä¸åå
å«å ä½ç¬¦ï¼å å
¥å©ä½é¨ååè¿åç»æ |
| | | sbuf.append(strPattern, handledPosition, strPatternLength); |
| | | return sbuf.toString(); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH) |
| | | { |
| | | if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH) |
| | | { |
| | | // 转ä¹ç¬¦ä¹åè¿æä¸ä¸ªè½¬ä¹ç¬¦ï¼å ä½ç¬¦ä¾æ§ææ |
| | | sbuf.append(strPattern, handledPosition, delimIndex - 1); |
| | | sbuf.append(Convert.utf8Str(argArray[argIndex])); |
| | | handledPosition = delimIndex + 2; |
| | | } |
| | | else |
| | | { |
| | | // å ä½ç¬¦è¢«è½¬ä¹ |
| | | argIndex--; |
| | | sbuf.append(strPattern, handledPosition, delimIndex - 1); |
| | | sbuf.append(C_DELIM_START); |
| | | handledPosition = delimIndex + 1; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | // æ£å¸¸å ä½ç¬¦ |
| | | sbuf.append(strPattern, handledPosition, delimIndex); |
| | | sbuf.append(Convert.utf8Str(argArray[argIndex])); |
| | | handledPosition = delimIndex + 2; |
| | | } |
| | | } |
| | | } |
| | | // å å
¥æåä¸ä¸ªå ä½ç¬¦åææçå符 |
| | | sbuf.append(strPattern, handledPosition, strPattern.length()); |
| | | |
| | | return sbuf.toString(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils; |
| | | |
| | | import java.lang.management.ManagementFactory; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.time.ZoneId; |
| | | import java.time.ZonedDateTime; |
| | | import java.util.Date; |
| | | import org.apache.commons.lang3.time.DateFormatUtils; |
| | | |
| | | /** |
| | | * æ¶é´å·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class DateUtils extends org.apache.commons.lang3.time.DateUtils |
| | | { |
| | | public static String YYYY = "yyyy"; |
| | | |
| | | public static String YYYY_MM = "yyyy-MM"; |
| | | |
| | | public static String YYYY_MM_DD = "yyyy-MM-dd"; |
| | | |
| | | public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; |
| | | |
| | | public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; |
| | | |
| | | private static String[] parsePatterns = { |
| | | "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", |
| | | "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", |
| | | "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; |
| | | |
| | | /** |
| | | * è·åå½åDate忥æ |
| | | * |
| | | * @return Date() å½åæ¥æ |
| | | */ |
| | | public static Date getNowDate() |
| | | { |
| | | return new Date(); |
| | | } |
| | | |
| | | /** |
| | | * è·åå½åæ¥æ, é»è®¤æ ¼å¼ä¸ºyyyy-MM-dd |
| | | * |
| | | * @return String |
| | | */ |
| | | public static String getDate() |
| | | { |
| | | return dateTimeNow(YYYY_MM_DD); |
| | | } |
| | | |
| | | public static final String getTime() |
| | | { |
| | | return dateTimeNow(YYYY_MM_DD_HH_MM_SS); |
| | | } |
| | | |
| | | public static final String dateTimeNow() |
| | | { |
| | | return dateTimeNow(YYYYMMDDHHMMSS); |
| | | } |
| | | |
| | | public static final String dateTimeNow(final String format) |
| | | { |
| | | return parseDateToStr(format, new Date()); |
| | | } |
| | | |
| | | public static final String dateTime(final Date date) |
| | | { |
| | | return parseDateToStr(YYYY_MM_DD, date); |
| | | } |
| | | |
| | | public static final String parseDateToStr(final String format, final Date date) |
| | | { |
| | | return new SimpleDateFormat(format).format(date); |
| | | } |
| | | |
| | | public static final Date dateTime(final String format, final String ts) |
| | | { |
| | | try |
| | | { |
| | | return new SimpleDateFormat(format).parse(ts); |
| | | } |
| | | catch (ParseException e) |
| | | { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¥æè·¯å¾ å³å¹´/æ/æ¥ å¦2018/08/08 |
| | | */ |
| | | public static final String datePath() |
| | | { |
| | | Date now = new Date(); |
| | | return DateFormatUtils.format(now, "yyyy/MM/dd"); |
| | | } |
| | | |
| | | /** |
| | | * æ¥æè·¯å¾ å³å¹´/æ/æ¥ å¦20180808 |
| | | */ |
| | | public static final String dateTime() |
| | | { |
| | | Date now = new Date(); |
| | | return DateFormatUtils.format(now, "yyyyMMdd"); |
| | | } |
| | | |
| | | /** |
| | | * æ¥æåå符串转åä¸ºæ¥æ æ ¼å¼ |
| | | */ |
| | | public static Date parseDate(Object str) |
| | | { |
| | | if (str == null) |
| | | { |
| | | return null; |
| | | } |
| | | try |
| | | { |
| | | return parseDate(str.toString(), parsePatterns); |
| | | } |
| | | catch (ParseException e) |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åæå¡å¨å¯å¨æ¶é´ |
| | | */ |
| | | public static Date getServerStartDate() |
| | | { |
| | | long time = ManagementFactory.getRuntimeMXBean().getStartTime(); |
| | | return new Date(time); |
| | | } |
| | | |
| | | /** |
| | | * 计ç®ç¸å·®å¤©æ° |
| | | */ |
| | | public static int differentDaysByMillisecond(Date date1, Date date2) |
| | | { |
| | | return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24))); |
| | | } |
| | | |
| | | /** |
| | | * è®¡ç®æ¶é´å·® |
| | | * |
| | | * @param endDate æåæ¶é´ |
| | | * @param startTime å¼å§æ¶é´ |
| | | * @return æ¶é´å·®ï¼å¤©/å°æ¶/åéï¼ |
| | | */ |
| | | public static String timeDistance(Date endDate, Date startTime) |
| | | { |
| | | long nd = 1000 * 24 * 60 * 60; |
| | | long nh = 1000 * 60 * 60; |
| | | long nm = 1000 * 60; |
| | | // long ns = 1000; |
| | | // è·å¾ä¸¤ä¸ªæ¶é´çæ¯«ç§æ¶é´å·®å¼ |
| | | long diff = endDate.getTime() - startTime.getTime(); |
| | | // 计ç®å·®å¤å°å¤© |
| | | long day = diff / nd; |
| | | // 计ç®å·®å¤å°å°æ¶ |
| | | long hour = diff % nd / nh; |
| | | // 计ç®å·®å¤å°åé |
| | | long min = diff % nd % nh / nm; |
| | | // 计ç®å·®å¤å°ç§//è¾åºç»æ |
| | | // long sec = diff % nd % nh % nm / ns; |
| | | return day + "天" + hour + "å°æ¶" + min + "åé"; |
| | | } |
| | | |
| | | /** |
| | | * å¢å LocalDateTime ==> Date |
| | | */ |
| | | public static Date toDate(LocalDateTime temporalAccessor) |
| | | { |
| | | ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault()); |
| | | return Date.from(zdt.toInstant()); |
| | | } |
| | | |
| | | /** |
| | | * å¢å LocalDate ==> Date |
| | | */ |
| | | public static Date toDate(LocalDate temporalAccessor) |
| | | { |
| | | LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0)); |
| | | ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault()); |
| | | return Date.from(zdt.toInstant()); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils; |
| | | |
| | | import java.io.PrintWriter; |
| | | import java.io.StringWriter; |
| | | import org.apache.commons.lang3.exception.ExceptionUtils; |
| | | |
| | | /** |
| | | * é误信æ¯å¤çç±»ã |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class ExceptionUtil |
| | | { |
| | | /** |
| | | * è·åexceptionç详ç»é误信æ¯ã |
| | | */ |
| | | public static String getExceptionMessage(Throwable e) |
| | | { |
| | | StringWriter sw = new StringWriter(); |
| | | e.printStackTrace(new PrintWriter(sw, true)); |
| | | return sw.toString(); |
| | | } |
| | | |
| | | public static String getRootErrorMessage(Exception e) |
| | | { |
| | | Throwable root = ExceptionUtils.getRootCause(e); |
| | | root = (root == null ? e : root); |
| | | if (root == null) |
| | | { |
| | | return ""; |
| | | } |
| | | String msg = root.getMessage(); |
| | | if (msg == null) |
| | | { |
| | | return "null"; |
| | | } |
| | | return StringUtils.defaultString(msg); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils; |
| | | |
| | | import java.util.Map; |
| | | import com.terra.common.constant.SecurityConstants; |
| | | import com.terra.common.constant.TokenConstants; |
| | | import com.terra.common.text.Convert; |
| | | import io.jsonwebtoken.Claims; |
| | | import io.jsonwebtoken.Jwts; |
| | | import io.jsonwebtoken.SignatureAlgorithm; |
| | | |
| | | /** |
| | | * Jwtå·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class JwtUtils |
| | | { |
| | | public static String secret = TokenConstants.SECRET; |
| | | |
| | | /** |
| | | * 仿°æ®å£°æçæä»¤ç |
| | | * |
| | | * @param claims æ°æ®å£°æ |
| | | * @return 令ç |
| | | */ |
| | | public static String createToken(Map<String, Object> claims) |
| | | { |
| | | String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact(); |
| | | return token; |
| | | } |
| | | |
| | | /** |
| | | * ä»ä»¤çä¸è·åæ°æ®å£°æ |
| | | * |
| | | * @param token 令ç |
| | | * @return æ°æ®å£°æ |
| | | */ |
| | | public static Claims parseToken(String token) |
| | | { |
| | | return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody(); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®ä»¤çè·åç¨æ·æ è¯ |
| | | * |
| | | * @param token 令ç |
| | | * @return ç¨æ·ID |
| | | */ |
| | | public static String getUserKey(String token) |
| | | { |
| | | Claims claims = parseToken(token); |
| | | return getValue(claims, SecurityConstants.USER_KEY); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®ä»¤çè·åç¨æ·æ è¯ |
| | | * |
| | | * @param claims èº«ä»½ä¿¡æ¯ |
| | | * @return ç¨æ·ID |
| | | */ |
| | | public static String getUserKey(Claims claims) |
| | | { |
| | | return getValue(claims, SecurityConstants.USER_KEY); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®ä»¤çè·åç¨æ·ID |
| | | * |
| | | * @param token 令ç |
| | | * @return ç¨æ·ID |
| | | */ |
| | | public static String getUserId(String token) |
| | | { |
| | | Claims claims = parseToken(token); |
| | | return getValue(claims, SecurityConstants.DETAILS_USER_ID); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®èº«ä»½ä¿¡æ¯è·åç¨æ·ID |
| | | * |
| | | * @param claims èº«ä»½ä¿¡æ¯ |
| | | * @return ç¨æ·ID |
| | | */ |
| | | public static String getUserId(Claims claims) |
| | | { |
| | | return getValue(claims, SecurityConstants.DETAILS_USER_ID); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®ä»¤çè·åç¨æ·å |
| | | * |
| | | * @param token 令ç |
| | | * @return ç¨æ·å |
| | | */ |
| | | public static String getUserName(String token) |
| | | { |
| | | Claims claims = parseToken(token); |
| | | return getValue(claims, SecurityConstants.DETAILS_USERNAME); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®èº«ä»½ä¿¡æ¯è·åç¨æ·å |
| | | * |
| | | * @param claims èº«ä»½ä¿¡æ¯ |
| | | * @return ç¨æ·å |
| | | */ |
| | | public static String getUserName(Claims claims) |
| | | { |
| | | return getValue(claims, SecurityConstants.DETAILS_USERNAME); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®èº«ä»½ä¿¡æ¯è·åé®å¼ |
| | | * |
| | | * @param claims èº«ä»½ä¿¡æ¯ |
| | | * @param key é® |
| | | * @return å¼ |
| | | */ |
| | | public static String getValue(Claims claims, String key) |
| | | { |
| | | return Convert.toStr(claims.get(key), ""); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils; |
| | | |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.terra.common.utils.sql.SqlUtil; |
| | | import com.terra.common.web.page.PageDomain; |
| | | import com.terra.common.web.page.TableSupport; |
| | | |
| | | /** |
| | | * å页工å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class PageUtils extends PageHelper |
| | | { |
| | | /** |
| | | * 设置请æ±åé¡µæ°æ® |
| | | */ |
| | | public static void startPage() |
| | | { |
| | | PageDomain pageDomain = TableSupport.buildPageRequest(); |
| | | Integer pageNum = pageDomain.getPageNum(); |
| | | Integer pageSize = pageDomain.getPageSize(); |
| | | String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy()); |
| | | Boolean reasonable = pageDomain.getReasonable(); |
| | | PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable); |
| | | } |
| | | |
| | | /** |
| | | * æ¸
çå页ç线ç¨åé |
| | | */ |
| | | public static void clearPage() |
| | | { |
| | | PageHelper.clearPage(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLDecoder; |
| | | import java.net.URLEncoder; |
| | | import java.util.Collections; |
| | | import java.util.Enumeration; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import javax.servlet.ServletRequest; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.servlet.http.HttpSession; |
| | | import org.springframework.core.io.buffer.DataBuffer; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.server.reactive.ServerHttpResponse; |
| | | import org.springframework.util.LinkedCaseInsensitiveMap; |
| | | import org.springframework.web.context.request.RequestAttributes; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.terra.common.constant.Constants; |
| | | import com.terra.common.domain.R; |
| | | import com.terra.common.text.Convert; |
| | | import reactor.core.publisher.Mono; |
| | | |
| | | /** |
| | | * 客æ·ç«¯å·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class ServletUtils |
| | | { |
| | | /** |
| | | * è·åStringåæ° |
| | | */ |
| | | public static String getParameter(String name) |
| | | { |
| | | return getRequest().getParameter(name); |
| | | } |
| | | |
| | | /** |
| | | * è·åStringåæ° |
| | | */ |
| | | public static String getParameter(String name, String defaultValue) |
| | | { |
| | | return Convert.toStr(getRequest().getParameter(name), defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * è·åIntegeråæ° |
| | | */ |
| | | public static Integer getParameterToInt(String name) |
| | | { |
| | | return Convert.toInt(getRequest().getParameter(name)); |
| | | } |
| | | |
| | | /** |
| | | * è·åIntegeråæ° |
| | | */ |
| | | public static Integer getParameterToInt(String name, Integer defaultValue) |
| | | { |
| | | return Convert.toInt(getRequest().getParameter(name), defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * è·åBooleanåæ° |
| | | */ |
| | | public static Boolean getParameterToBool(String name) |
| | | { |
| | | return Convert.toBool(getRequest().getParameter(name)); |
| | | } |
| | | |
| | | /** |
| | | * è·åBooleanåæ° |
| | | */ |
| | | public static Boolean getParameterToBool(String name, Boolean defaultValue) |
| | | { |
| | | return Convert.toBool(getRequest().getParameter(name), defaultValue); |
| | | } |
| | | |
| | | /** |
| | | * è·å¾ææè¯·æ±åæ° |
| | | * |
| | | * @param request 请æ±å¯¹è±¡{@link ServletRequest} |
| | | * @return Map |
| | | */ |
| | | public static Map<String, String[]> getParams(ServletRequest request) |
| | | { |
| | | final Map<String, String[]> map = request.getParameterMap(); |
| | | return Collections.unmodifiableMap(map); |
| | | } |
| | | |
| | | /** |
| | | * è·å¾ææè¯·æ±åæ° |
| | | * |
| | | * @param request 请æ±å¯¹è±¡{@link ServletRequest} |
| | | * @return Map |
| | | */ |
| | | public static Map<String, String> getParamMap(ServletRequest request) |
| | | { |
| | | Map<String, String> params = new HashMap<>(); |
| | | for (Map.Entry<String, String[]> entry : getParams(request).entrySet()) |
| | | { |
| | | params.put(entry.getKey(), StringUtils.join(entry.getValue(), ",")); |
| | | } |
| | | return params; |
| | | } |
| | | |
| | | /** |
| | | * è·årequest |
| | | */ |
| | | public static HttpServletRequest getRequest() |
| | | { |
| | | try |
| | | { |
| | | return getRequestAttributes().getRequest(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åresponse |
| | | */ |
| | | public static HttpServletResponse getResponse() |
| | | { |
| | | try |
| | | { |
| | | return getRequestAttributes().getResponse(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åsession |
| | | */ |
| | | public static HttpSession getSession() |
| | | { |
| | | return getRequest().getSession(); |
| | | } |
| | | |
| | | public static ServletRequestAttributes getRequestAttributes() |
| | | { |
| | | try |
| | | { |
| | | RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); |
| | | return (ServletRequestAttributes) attributes; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public static String getHeader(HttpServletRequest request, String name) |
| | | { |
| | | String value = request.getHeader(name); |
| | | if (StringUtils.isEmpty(value)) |
| | | { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | return urlDecode(value); |
| | | } |
| | | |
| | | public static Map<String, String> getHeaders(HttpServletRequest request) |
| | | { |
| | | Map<String, String> map = new LinkedCaseInsensitiveMap<>(); |
| | | Enumeration<String> enumeration = request.getHeaderNames(); |
| | | if (enumeration != null) |
| | | { |
| | | while (enumeration.hasMoreElements()) |
| | | { |
| | | String key = enumeration.nextElement(); |
| | | String value = request.getHeader(key); |
| | | map.put(key, value); |
| | | } |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * å°å符串渲æå°å®¢æ·ç«¯ |
| | | * |
| | | * @param response 渲æå¯¹è±¡ |
| | | * @param string å¾
渲æçå符串 |
| | | */ |
| | | public static void renderString(HttpServletResponse response, String string) |
| | | { |
| | | try |
| | | { |
| | | response.setStatus(200); |
| | | response.setContentType("application/json"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | response.getWriter().print(string); |
| | | } |
| | | catch (IOException e) |
| | | { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦æ¯Ajax弿¥è¯·æ± |
| | | * |
| | | * @param request |
| | | */ |
| | | public static boolean isAjaxRequest(HttpServletRequest request) |
| | | { |
| | | String accept = request.getHeader("accept"); |
| | | if (accept != null && accept.contains("application/json")) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | String xRequestedWith = request.getHeader("X-Requested-With"); |
| | | if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | String uri = request.getRequestURI(); |
| | | if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml")) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | String ajax = request.getParameter("__ajax"); |
| | | return StringUtils.inStringIgnoreCase(ajax, "json", "xml"); |
| | | } |
| | | |
| | | /** |
| | | * å
容ç¼ç |
| | | * |
| | | * @param str å
容 |
| | | * @return ç¼ç åçå
容 |
| | | */ |
| | | public static String urlEncode(String str) |
| | | { |
| | | try |
| | | { |
| | | return URLEncoder.encode(str, Constants.UTF8); |
| | | } |
| | | catch (UnsupportedEncodingException e) |
| | | { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å
容解ç |
| | | * |
| | | * @param str å
容 |
| | | * @return è§£ç åçå
容 |
| | | */ |
| | | public static String urlDecode(String str) |
| | | { |
| | | try |
| | | { |
| | | return URLDecoder.decode(str, Constants.UTF8); |
| | | } |
| | | catch (UnsupportedEncodingException e) |
| | | { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置webflux模åååº |
| | | * |
| | | * @param response ServerHttpResponse |
| | | * @param value ååºå
容 |
| | | * @return Mono<Void> |
| | | */ |
| | | public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value) |
| | | { |
| | | return webFluxResponseWriter(response, HttpStatus.OK, value, R.FAIL); |
| | | } |
| | | |
| | | /** |
| | | * 设置webflux模åååº |
| | | * |
| | | * @param response ServerHttpResponse |
| | | * @param code ååºç¶æç |
| | | * @param value ååºå
容 |
| | | * @return Mono<Void> |
| | | */ |
| | | public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code) |
| | | { |
| | | return webFluxResponseWriter(response, HttpStatus.OK, value, code); |
| | | } |
| | | |
| | | /** |
| | | * 设置webflux模åååº |
| | | * |
| | | * @param response ServerHttpResponse |
| | | * @param status httpç¶æç |
| | | * @param code ååºç¶æç |
| | | * @param value ååºå
容 |
| | | * @return Mono<Void> |
| | | */ |
| | | public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code) |
| | | { |
| | | return webFluxResponseWriter(response, MediaType.APPLICATION_JSON_VALUE, status, value, code); |
| | | } |
| | | |
| | | /** |
| | | * 设置webflux模åååº |
| | | * |
| | | * @param response ServerHttpResponse |
| | | * @param contentType content-type |
| | | * @param status httpç¶æç |
| | | * @param code ååºç¶æç |
| | | * @param value ååºå
容 |
| | | * @return Mono<Void> |
| | | */ |
| | | public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code) |
| | | { |
| | | response.setStatusCode(status); |
| | | response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType); |
| | | R<?> result = R.fail(code, value.toString()); |
| | | DataBuffer dataBuffer = response.bufferFactory().wrap(JSON.toJSONString(result).getBytes()); |
| | | return response.writeWith(Mono.just(dataBuffer)); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils; |
| | | |
| | | import org.springframework.aop.framework.AopContext; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| | | import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
| | | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * springå·¥å
·ç±» æ¹ä¾¿å¨éspring管çç¯å¢ä¸è·åbean |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Component |
| | | public final class SpringUtils implements BeanFactoryPostProcessor |
| | | { |
| | | /** Springåºç¨ä¸ä¸æç¯å¢ */ |
| | | private static ConfigurableListableBeanFactory beanFactory; |
| | | |
| | | @Override |
| | | public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException |
| | | { |
| | | SpringUtils.beanFactory = beanFactory; |
| | | } |
| | | |
| | | /** |
| | | * è·å对象 |
| | | * |
| | | * @param name |
| | | * @return Object ä¸ä¸ªä»¥æç»ååæ³¨åçbeançå®ä¾ |
| | | * @throws org.springframework.beans.BeansException |
| | | * |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T getBean(String name) throws BeansException |
| | | { |
| | | return (T) beanFactory.getBean(name); |
| | | } |
| | | |
| | | /** |
| | | * è·åç±»å为requiredTypeç对象 |
| | | * |
| | | * @param clz |
| | | * @return |
| | | * @throws org.springframework.beans.BeansException |
| | | * |
| | | */ |
| | | public static <T> T getBean(Class<T> clz) throws BeansException |
| | | { |
| | | T result = (T) beanFactory.getBean(clz); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 妿BeanFactoryå
å«ä¸ä¸ªä¸æç»åç§°å¹é
çbeanå®ä¹ï¼åè¿åtrue |
| | | * |
| | | * @param name |
| | | * @return boolean |
| | | */ |
| | | public static boolean containsBean(String name) |
| | | { |
| | | return beanFactory.containsBean(name); |
| | | } |
| | | |
| | | /** |
| | | * 夿以ç»å®ååæ³¨åçbeanå®ä¹æ¯ä¸ä¸ªsingletonè¿æ¯ä¸ä¸ªprototypeã 妿ä¸ç»å®ååç¸åºçbeanå®ä¹æ²¡æè¢«æ¾å°ï¼å°ä¼æåºä¸ä¸ªå¼å¸¸ï¼NoSuchBeanDefinitionExceptionï¼ |
| | | * |
| | | * @param name |
| | | * @return boolean |
| | | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException |
| | | { |
| | | return beanFactory.isSingleton(name); |
| | | } |
| | | |
| | | /** |
| | | * @param name |
| | | * @return Class 注å对象çç±»å |
| | | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static Class<?> getType(String name) throws NoSuchBeanDefinitionException |
| | | { |
| | | return beanFactory.getType(name); |
| | | } |
| | | |
| | | /** |
| | | * 妿ç»å®çbeanååå¨beanå®ä¹ä¸æå«åï¼åè¿åè¿äºå«å |
| | | * |
| | | * @param name |
| | | * @return |
| | | * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException |
| | | * |
| | | */ |
| | | public static String[] getAliases(String name) throws NoSuchBeanDefinitionException |
| | | { |
| | | return beanFactory.getAliases(name); |
| | | } |
| | | |
| | | /** |
| | | * è·åaop代ç对象 |
| | | * |
| | | * @param invoker |
| | | * @return |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T getAopProxy(T invoker) |
| | | { |
| | | return (T) AopContext.currentProxy(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils; |
| | | |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import org.springframework.util.AntPathMatcher; |
| | | import com.terra.common.constant.Constants; |
| | | import com.terra.common.text.StrFormatter; |
| | | |
| | | /** |
| | | * å符串工å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class StringUtils extends org.apache.commons.lang3.StringUtils |
| | | { |
| | | /** 空å符串 */ |
| | | private static final String NULLSTR = ""; |
| | | |
| | | /** ä¸å线 */ |
| | | private static final char SEPARATOR = '_'; |
| | | |
| | | /** æå· */ |
| | | private static final char ASTERISK = '*'; |
| | | |
| | | /** |
| | | * è·ååæ°ä¸ä¸ºç©ºå¼ |
| | | * |
| | | * @param value defaultValue è¦å¤æçvalue |
| | | * @return value è¿åå¼ |
| | | */ |
| | | public static <T> T nvl(T value, T defaultValue) |
| | | { |
| | | return value != null ? value : defaultValue; |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªCollectionæ¯å¦ä¸ºç©ºï¼ å
å«Listï¼Setï¼Queue |
| | | * |
| | | * @param coll è¦å¤æçCollection |
| | | * @return trueï¼ä¸ºç©º falseï¼é空 |
| | | */ |
| | | public static boolean isEmpty(Collection<?> coll) |
| | | { |
| | | return isNull(coll) || coll.isEmpty(); |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªCollectionæ¯å¦é空ï¼å
å«Listï¼Setï¼Queue |
| | | * |
| | | * @param coll è¦å¤æçCollection |
| | | * @return trueï¼é空 falseï¼ç©º |
| | | */ |
| | | public static boolean isNotEmpty(Collection<?> coll) |
| | | { |
| | | return !isEmpty(coll); |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªå¯¹è±¡æ°ç»æ¯å¦ä¸ºç©º |
| | | * |
| | | * @param objects è¦å¤æç对象æ°ç» |
| | | ** @return trueï¼ä¸ºç©º falseï¼é空 |
| | | */ |
| | | public static boolean isEmpty(Object[] objects) |
| | | { |
| | | return isNull(objects) || (objects.length == 0); |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªå¯¹è±¡æ°ç»æ¯å¦é空 |
| | | * |
| | | * @param objects è¦å¤æç对象æ°ç» |
| | | * @return trueï¼é空 falseï¼ç©º |
| | | */ |
| | | public static boolean isNotEmpty(Object[] objects) |
| | | { |
| | | return !isEmpty(objects); |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªMapæ¯å¦ä¸ºç©º |
| | | * |
| | | * @param map è¦å¤æçMap |
| | | * @return trueï¼ä¸ºç©º falseï¼é空 |
| | | */ |
| | | public static boolean isEmpty(Map<?, ?> map) |
| | | { |
| | | return isNull(map) || map.isEmpty(); |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªMapæ¯å¦ä¸ºç©º |
| | | * |
| | | * @param map è¦å¤æçMap |
| | | * @return trueï¼é空 falseï¼ç©º |
| | | */ |
| | | public static boolean isNotEmpty(Map<?, ?> map) |
| | | { |
| | | return !isEmpty(map); |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªå符串æ¯å¦ä¸ºç©ºä¸² |
| | | * |
| | | * @param str String |
| | | * @return trueï¼ä¸ºç©º falseï¼é空 |
| | | */ |
| | | public static boolean isEmpty(String str) |
| | | { |
| | | return isNull(str) || NULLSTR.equals(str.trim()); |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªå符串æ¯å¦ä¸ºé空串 |
| | | * |
| | | * @param str String |
| | | * @return trueï¼é空串 falseï¼ç©ºä¸² |
| | | */ |
| | | public static boolean isNotEmpty(String str) |
| | | { |
| | | return !isEmpty(str); |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªå¯¹è±¡æ¯å¦ä¸ºç©º |
| | | * |
| | | * @param object Object |
| | | * @return trueï¼ä¸ºç©º falseï¼é空 |
| | | */ |
| | | public static boolean isNull(Object object) |
| | | { |
| | | return object == null; |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªå¯¹è±¡æ¯å¦é空 |
| | | * |
| | | * @param object Object |
| | | * @return trueï¼é空 falseï¼ç©º |
| | | */ |
| | | public static boolean isNotNull(Object object) |
| | | { |
| | | return !isNull(object); |
| | | } |
| | | |
| | | /** |
| | | * * 夿ä¸ä¸ªå¯¹è±¡æ¯å¦æ¯æ°ç»ç±»åï¼Javaåºæ¬åå«çæ°ç»ï¼ |
| | | * |
| | | * @param object 对象 |
| | | * @return trueï¼æ¯æ°ç» falseï¼ä¸æ¯æ°ç» |
| | | */ |
| | | public static boolean isArray(Object object) |
| | | { |
| | | return isNotNull(object) && object.getClass().isArray(); |
| | | } |
| | | |
| | | /** |
| | | * å»ç©ºæ ¼ |
| | | */ |
| | | public static String trim(String str) |
| | | { |
| | | return (str == null ? "" : str.trim()); |
| | | } |
| | | |
| | | /** |
| | | * æ¿æ¢æå®å符串çæå®åºé´å
å符为"*" |
| | | * |
| | | * @param str å符串 |
| | | * @param startInclude å¼å§ä½ç½®ï¼å
å«ï¼ |
| | | * @param endExclude ç»æä½ç½®ï¼ä¸å
å«ï¼ |
| | | * @return æ¿æ¢åçå符串 |
| | | */ |
| | | public static String hide(CharSequence str, int startInclude, int endExclude) |
| | | { |
| | | if (isEmpty(str)) |
| | | { |
| | | return NULLSTR; |
| | | } |
| | | final int strLength = str.length(); |
| | | if (startInclude > strLength) |
| | | { |
| | | return NULLSTR; |
| | | } |
| | | if (endExclude > strLength) |
| | | { |
| | | endExclude = strLength; |
| | | } |
| | | if (startInclude > endExclude) |
| | | { |
| | | // å¦æèµ·å§ä½ç½®å¤§äºç»æä½ç½®ï¼ä¸æ¿æ¢ |
| | | return NULLSTR; |
| | | } |
| | | final char[] chars = new char[strLength]; |
| | | for (int i = 0; i < strLength; i++) |
| | | { |
| | | if (i >= startInclude && i < endExclude) |
| | | { |
| | | chars[i] = ASTERISK; |
| | | } |
| | | else |
| | | { |
| | | chars[i] = str.charAt(i); |
| | | } |
| | | } |
| | | return new String(chars); |
| | | } |
| | | |
| | | /** |
| | | * æªåå符串 |
| | | * |
| | | * @param str å符串 |
| | | * @param start å¼å§ |
| | | * @return ç»æ |
| | | */ |
| | | public static String substring(final String str, int start) |
| | | { |
| | | if (str == null) |
| | | { |
| | | return NULLSTR; |
| | | } |
| | | |
| | | if (start < 0) |
| | | { |
| | | start = str.length() + start; |
| | | } |
| | | |
| | | if (start < 0) |
| | | { |
| | | start = 0; |
| | | } |
| | | if (start > str.length()) |
| | | { |
| | | return NULLSTR; |
| | | } |
| | | |
| | | return str.substring(start); |
| | | } |
| | | |
| | | /** |
| | | * æªåå符串 |
| | | * |
| | | * @param str å符串 |
| | | * @param start å¼å§ |
| | | * @param end ç»æ |
| | | * @return ç»æ |
| | | */ |
| | | public static String substring(final String str, int start, int end) |
| | | { |
| | | if (str == null) |
| | | { |
| | | return NULLSTR; |
| | | } |
| | | |
| | | if (end < 0) |
| | | { |
| | | end = str.length() + end; |
| | | } |
| | | if (start < 0) |
| | | { |
| | | start = str.length() + start; |
| | | } |
| | | |
| | | if (end > str.length()) |
| | | { |
| | | end = str.length(); |
| | | } |
| | | |
| | | if (start > end) |
| | | { |
| | | return NULLSTR; |
| | | } |
| | | |
| | | if (start < 0) |
| | | { |
| | | start = 0; |
| | | } |
| | | if (end < 0) |
| | | { |
| | | end = 0; |
| | | } |
| | | |
| | | return str.substring(start, end); |
| | | } |
| | | |
| | | /** |
| | | * å¨åç¬¦ä¸²ä¸æ¥æ¾ç¬¬ä¸ä¸ªåºç°ç `open` åæåä¸ä¸ªåºç°ç `close` ä¹é´çåå符串 |
| | | * |
| | | * @param str è¦æªåçå符串 |
| | | * @param open èµ·å§å符串 |
| | | * @param close ç»æå符串 |
| | | * @return æªåç»æ |
| | | */ |
| | | public static String substringBetweenLast(final String str, final String open, final String close) |
| | | { |
| | | if (isEmpty(str) || isEmpty(open) || isEmpty(close)) |
| | | { |
| | | return NULLSTR; |
| | | } |
| | | final int start = str.indexOf(open); |
| | | if (start != INDEX_NOT_FOUND) |
| | | { |
| | | final int end = str.lastIndexOf(close); |
| | | if (end != INDEX_NOT_FOUND) |
| | | { |
| | | return str.substring(start + open.length(), end); |
| | | } |
| | | } |
| | | return NULLSTR; |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯å¦ä¸ºç©ºï¼å¹¶ä¸ä¸æ¯ç©ºç½å符 |
| | | * |
| | | * @param str è¦å¤æçvalue |
| | | * @return ç»æ |
| | | */ |
| | | public static boolean hasText(String str) |
| | | { |
| | | return (str != null && !str.isEmpty() && containsText(str)); |
| | | } |
| | | |
| | | private static boolean containsText(CharSequence str) |
| | | { |
| | | int strLen = str.length(); |
| | | for (int i = 0; i < strLen; i++) |
| | | { |
| | | if (!Character.isWhitespace(str.charAt(i))) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * æ ¼å¼åææ¬, {} 表示å ä½ç¬¦<br> |
| | | * æ¤æ¹æ³åªæ¯ç®åå°å ä½ç¬¦ {} æç
§é¡ºåºæ¿æ¢ä¸ºåæ°<br> |
| | | * 妿æ³è¾åº {} ä½¿ç¨ \\è½¬ä¹ { å³å¯ï¼å¦ææ³è¾åº {} ä¹åç \ 使ç¨å转ä¹ç¬¦ \\\\ å³å¯<br> |
| | | * ä¾ï¼<br> |
| | | * é常使ç¨ï¼format("this is {} for {}", "a", "b") -> this is a for b<br> |
| | | * 转ä¹{}ï¼ format("this is \\{} for {}", "a", "b") -> this is \{} for a<br> |
| | | * 转ä¹\ï¼ format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br> |
| | | * |
| | | * @param template ææ¬æ¨¡æ¿ï¼è¢«æ¿æ¢çé¨åç¨ {} 表示 |
| | | * @param params åæ°å¼ |
| | | * @return æ ¼å¼ååçææ¬ |
| | | */ |
| | | public static String format(String template, Object... params) |
| | | { |
| | | if (isEmpty(params) || isEmpty(template)) |
| | | { |
| | | return template; |
| | | } |
| | | return StrFormatter.format(template, params); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦ä¸ºhttp(s)://å¼å¤´ |
| | | * |
| | | * @param link 龿¥ |
| | | * @return ç»æ |
| | | */ |
| | | public static boolean ishttp(String link) |
| | | { |
| | | return StringUtils.startsWithAny(link, Constants.HTTP, Constants.HTTPS); |
| | | } |
| | | |
| | | /** |
| | | * 夿ç»å®çcollectionåè¡¨ä¸æ¯å¦å
嫿°ç»array 夿ç»å®çæ°ç»array䏿¯å¦å
å«ç»å®çå
ç´ value |
| | | * |
| | | * @param collection ç»å®çéå |
| | | * @param array ç»å®çæ°ç» |
| | | * @return boolean ç»æ |
| | | */ |
| | | public static boolean containsAny(Collection<String> collection, String... array) |
| | | { |
| | | if (isEmpty(collection) || isEmpty(array)) |
| | | { |
| | | return false; |
| | | } |
| | | else |
| | | { |
| | | for (String str : array) |
| | | { |
| | | if (collection.contains(str)) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 驼峰转ä¸å线å½å |
| | | */ |
| | | public static String toUnderScoreCase(String str) |
| | | { |
| | | if (str == null) |
| | | { |
| | | return null; |
| | | } |
| | | StringBuilder sb = new StringBuilder(); |
| | | // åç½®å符æ¯å¦å¤§å |
| | | boolean preCharIsUpperCase = true; |
| | | // å½åå符æ¯å¦å¤§å |
| | | boolean curreCharIsUpperCase = true; |
| | | // ä¸ä¸å符æ¯å¦å¤§å |
| | | boolean nexteCharIsUpperCase = true; |
| | | for (int i = 0; i < str.length(); i++) |
| | | { |
| | | char c = str.charAt(i); |
| | | if (i > 0) |
| | | { |
| | | preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1)); |
| | | } |
| | | else |
| | | { |
| | | preCharIsUpperCase = false; |
| | | } |
| | | |
| | | curreCharIsUpperCase = Character.isUpperCase(c); |
| | | |
| | | if (i < (str.length() - 1)) |
| | | { |
| | | nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1)); |
| | | } |
| | | |
| | | if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) |
| | | { |
| | | sb.append(SEPARATOR); |
| | | } |
| | | else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) |
| | | { |
| | | sb.append(SEPARATOR); |
| | | } |
| | | sb.append(Character.toLowerCase(c)); |
| | | } |
| | | |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦å
å«å符串 |
| | | * |
| | | * @param str éªè¯å符串 |
| | | * @param strs åç¬¦ä¸²ç» |
| | | * @return å
å«è¿åtrue |
| | | */ |
| | | public static boolean inStringIgnoreCase(String str, String... strs) |
| | | { |
| | | if (str != null && strs != null) |
| | | { |
| | | for (String s : strs) |
| | | { |
| | | if (str.equalsIgnoreCase(trim(s))) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * å°ä¸åçº¿å¤§åæ¹å¼å½åçå符串转æ¢ä¸ºé©¼å³°å¼ãå¦æè½¬æ¢åçä¸åçº¿å¤§åæ¹å¼å½åçå符串为空ï¼åè¿å空å符串ã ä¾å¦ï¼HELLO_WORLD->HelloWorld |
| | | * |
| | | * @param name 转æ¢åçä¸åçº¿å¤§åæ¹å¼å½åçå符串 |
| | | * @return 转æ¢åç驼峰å¼å½åçå符串 |
| | | */ |
| | | public static String convertToCamelCase(String name) |
| | | { |
| | | StringBuilder result = new StringBuilder(); |
| | | // å¿«éæ£æ¥ |
| | | if (name == null || name.isEmpty()) |
| | | { |
| | | // 没å¿
è¦è½¬æ¢ |
| | | return ""; |
| | | } |
| | | else if (!name.contains("_")) |
| | | { |
| | | // ä¸å«ä¸å线ï¼ä»
å°é¦åæ¯å¤§å |
| | | return name.substring(0, 1).toUpperCase() + name.substring(1); |
| | | } |
| | | // ç¨ä¸å线å°åå§å符串åå² |
| | | String[] camels = name.split("_"); |
| | | for (String camel : camels) |
| | | { |
| | | // è·³è¿åå§å符串ä¸å¼å¤´ãç»å°¾ç䏿¢çº¿æåéä¸å线 |
| | | if (camel.isEmpty()) |
| | | { |
| | | continue; |
| | | } |
| | | // é¦åæ¯å¤§å |
| | | result.append(camel.substring(0, 1).toUpperCase()); |
| | | result.append(camel.substring(1).toLowerCase()); |
| | | } |
| | | return result.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 驼峰å¼å½åæ³ |
| | | * ä¾å¦ï¼user_name->userName |
| | | */ |
| | | public static String toCamelCase(String s) |
| | | { |
| | | if (s == null) |
| | | { |
| | | return null; |
| | | } |
| | | if (s.indexOf(SEPARATOR) == -1) |
| | | { |
| | | return s; |
| | | } |
| | | s = s.toLowerCase(); |
| | | StringBuilder sb = new StringBuilder(s.length()); |
| | | boolean upperCase = false; |
| | | for (int i = 0; i < s.length(); i++) |
| | | { |
| | | char c = s.charAt(i); |
| | | |
| | | if (c == SEPARATOR) |
| | | { |
| | | upperCase = true; |
| | | } |
| | | else if (upperCase) |
| | | { |
| | | sb.append(Character.toUpperCase(c)); |
| | | upperCase = false; |
| | | } |
| | | else |
| | | { |
| | | sb.append(c); |
| | | } |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * æ¥æ¾æå®å符串æ¯å¦å¹é
æå®å符串å表ä¸çä»»æä¸ä¸ªå符串 |
| | | * |
| | | * @param str æå®å符串 |
| | | * @param strs éè¦æ£æ¥çå符串æ°ç» |
| | | * @return æ¯å¦å¹é
|
| | | */ |
| | | public static boolean matches(String str, List<String> strs) |
| | | { |
| | | if (isEmpty(str) || isEmpty(strs)) |
| | | { |
| | | return false; |
| | | } |
| | | for (String pattern : strs) |
| | | { |
| | | if (isMatch(pattern, str)) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 夿urlæ¯å¦ä¸è§åé
ç½®: |
| | | * ? 表示å个å符; |
| | | * * 表示ä¸å±è·¯å¾å
çä»»æå符串ï¼ä¸å¯è·¨å±çº§; |
| | | * ** 表示任æå±è·¯å¾; |
| | | * |
| | | * @param pattern å¹é
è§å |
| | | * @param url éè¦å¹é
çurl |
| | | * @return |
| | | */ |
| | | public static boolean isMatch(String pattern, String url) |
| | | { |
| | | AntPathMatcher matcher = new AntPathMatcher(); |
| | | return matcher.match(pattern, url); |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> T cast(Object obj) |
| | | { |
| | | return (T) obj; |
| | | } |
| | | |
| | | /** |
| | | * æ°å左边补é½0ï¼ä½¿ä¹è¾¾å°æå®é¿åº¦ã注æï¼å¦ææ°å转æ¢ä¸ºå符串åï¼é¿åº¦å¤§äºsizeï¼ååªä¿ç æåsize个å符ã |
| | | * |
| | | * @param num æ°å对象 |
| | | * @param size å符串æå®é¿åº¦ |
| | | * @return è¿åæ°åçåç¬¦ä¸²æ ¼å¼ï¼è¯¥å符串为æå®é¿åº¦ã |
| | | */ |
| | | public static final String padl(final Number num, final int size) |
| | | { |
| | | return padl(num.toString(), size, '0'); |
| | | } |
| | | |
| | | /** |
| | | * å符串左补é½ã妿åå§å符串sé¿åº¦å¤§äºsizeï¼ååªä¿çæåsize个å符ã |
| | | * |
| | | * @param s åå§å符串 |
| | | * @param size å符串æå®é¿åº¦ |
| | | * @param c ç¨äºè¡¥é½çå符 |
| | | * @return è¿åæå®é¿åº¦çå符串ï¼ç±ååç¬¦ä¸²å·¦è¡¥é½ææªåå¾å°ã |
| | | */ |
| | | public static final String padl(final String s, final int size, final char c) |
| | | { |
| | | final StringBuilder sb = new StringBuilder(size); |
| | | if (s != null) |
| | | { |
| | | final int len = s.length(); |
| | | if (s.length() <= size) |
| | | { |
| | | for (int i = size - len; i > 0; i--) |
| | | { |
| | | sb.append(c); |
| | | } |
| | | sb.append(s); |
| | | } |
| | | else |
| | | { |
| | | return s.substring(len - size, len); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | for (int i = size; i > 0; i--) |
| | | { |
| | | sb.append(c); |
| | | } |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.bean; |
| | | |
| | | import java.lang.reflect.Method; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * Bean å·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class BeanUtils extends org.springframework.beans.BeanUtils |
| | | { |
| | | /** Beanæ¹æ³åä¸å±æ§åå¼å§ç䏿 */ |
| | | private static final int BEAN_METHOD_PROP_INDEX = 3; |
| | | |
| | | /** * å¹é
getteræ¹æ³çæ£åè¡¨è¾¾å¼ */ |
| | | private static final Pattern GET_PATTERN = Pattern.compile("get(\\p{javaUpperCase}\\w*)"); |
| | | |
| | | /** * å¹é
setteræ¹æ³çæ£åè¡¨è¾¾å¼ */ |
| | | private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)"); |
| | | |
| | | /** |
| | | * Bean屿§å¤å¶å·¥å
·æ¹æ³ã |
| | | * |
| | | * @param dest ç®æ 对象 |
| | | * @param src æºå¯¹è±¡ |
| | | */ |
| | | public static void copyBeanProp(Object dest, Object src) |
| | | { |
| | | try |
| | | { |
| | | copyProperties(src, dest); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·å对象çsetteræ¹æ³ã |
| | | * |
| | | * @param obj 对象 |
| | | * @return 对象çsetteræ¹æ³å表 |
| | | */ |
| | | public static List<Method> getSetterMethods(Object obj) |
| | | { |
| | | // setteræ¹æ³å表 |
| | | List<Method> setterMethods = new ArrayList<Method>(); |
| | | |
| | | // è·åæææ¹æ³ |
| | | Method[] methods = obj.getClass().getMethods(); |
| | | |
| | | // æ¥æ¾setteræ¹æ³ |
| | | |
| | | for (Method method : methods) |
| | | { |
| | | Matcher m = SET_PATTERN.matcher(method.getName()); |
| | | if (m.matches() && (method.getParameterTypes().length == 1)) |
| | | { |
| | | setterMethods.add(method); |
| | | } |
| | | } |
| | | // è¿åsetteræ¹æ³å表 |
| | | return setterMethods; |
| | | } |
| | | |
| | | /** |
| | | * è·å对象çgetteræ¹æ³ã |
| | | * |
| | | * @param obj 对象 |
| | | * @return 对象çgetteræ¹æ³å表 |
| | | */ |
| | | |
| | | public static List<Method> getGetterMethods(Object obj) |
| | | { |
| | | // getteræ¹æ³å表 |
| | | List<Method> getterMethods = new ArrayList<Method>(); |
| | | // è·åæææ¹æ³ |
| | | Method[] methods = obj.getClass().getMethods(); |
| | | // æ¥æ¾getteræ¹æ³ |
| | | for (Method method : methods) |
| | | { |
| | | Matcher m = GET_PATTERN.matcher(method.getName()); |
| | | if (m.matches() && (method.getParameterTypes().length == 0)) |
| | | { |
| | | getterMethods.add(method); |
| | | } |
| | | } |
| | | // è¿ågetteræ¹æ³å表 |
| | | return getterMethods; |
| | | } |
| | | |
| | | /** |
| | | * æ£æ¥Beanæ¹æ³åä¸ç屿§åæ¯å¦ç¸çã<br> |
| | | * å¦getName()åsetName()屿§å䏿 ·ï¼getName()åsetAge()屿§åä¸ä¸æ ·ã |
| | | * |
| | | * @param m1 æ¹æ³å1 |
| | | * @param m2 æ¹æ³å2 |
| | | * @return 屿§å䏿 ·è¿åtrueï¼å¦åè¿åfalse |
| | | */ |
| | | |
| | | public static boolean isMethodPropEquals(String m1, String m2) |
| | | { |
| | | return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX)); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.bean; |
| | | |
| | | import java.util.Set; |
| | | import javax.validation.ConstraintViolation; |
| | | import javax.validation.ConstraintViolationException; |
| | | import javax.validation.Validator; |
| | | |
| | | /** |
| | | * beanå¯¹è±¡å±æ§éªè¯ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class BeanValidators |
| | | { |
| | | public static void validateWithException(Validator validator, Object object, Class<?>... groups) |
| | | throws ConstraintViolationException |
| | | { |
| | | Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups); |
| | | if (!constraintViolations.isEmpty()) |
| | | { |
| | | throw new ConstraintViolationException(constraintViolations); |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.file; |
| | | |
| | | import java.io.File; |
| | | import java.util.Objects; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | /** |
| | | * æä»¶ç±»åå·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class FileTypeUtils |
| | | { |
| | | /** |
| | | * è·åæä»¶ç±»å |
| | | * <p> |
| | | * ä¾å¦: ruoyi.txt, è¿å: txt |
| | | * |
| | | * @param file æä»¶å |
| | | * @return åç¼ï¼ä¸å«".") |
| | | */ |
| | | public static String getFileType(File file) |
| | | { |
| | | if (null == file) |
| | | { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | return getFileType(file.getName()); |
| | | } |
| | | |
| | | /** |
| | | * è·åæä»¶ç±»å |
| | | * <p> |
| | | * ä¾å¦: ruoyi.txt, è¿å: txt |
| | | * |
| | | * @param fileName æä»¶å |
| | | * @return åç¼ï¼ä¸å«".") |
| | | */ |
| | | public static String getFileType(String fileName) |
| | | { |
| | | int separatorIndex = fileName.lastIndexOf("."); |
| | | if (separatorIndex < 0) |
| | | { |
| | | return ""; |
| | | } |
| | | return fileName.substring(separatorIndex + 1).toLowerCase(); |
| | | } |
| | | |
| | | /** |
| | | * è·åæä»¶åçåç¼ |
| | | * |
| | | * @param file 表åæä»¶ |
| | | * @return åç¼å |
| | | */ |
| | | public static final String getExtension(MultipartFile file) |
| | | { |
| | | String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | if (StringUtils.isEmpty(extension)) |
| | | { |
| | | extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType())); |
| | | } |
| | | return extension; |
| | | } |
| | | |
| | | /** |
| | | * è·åæä»¶ç±»å |
| | | * |
| | | * @param photoByte æä»¶åèç |
| | | * @return åç¼ï¼ä¸å«".") |
| | | */ |
| | | public static String getFileExtendName(byte[] photoByte) |
| | | { |
| | | String strFileExtendName = "JPG"; |
| | | if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56) |
| | | && ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97)) |
| | | { |
| | | strFileExtendName = "GIF"; |
| | | } |
| | | else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70)) |
| | | { |
| | | strFileExtendName = "JPG"; |
| | | } |
| | | else if ((photoByte[0] == 66) && (photoByte[1] == 77)) |
| | | { |
| | | strFileExtendName = "BMP"; |
| | | } |
| | | else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71)) |
| | | { |
| | | strFileExtendName = "PNG"; |
| | | } |
| | | return strFileExtendName; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.file; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import org.apache.commons.lang3.ArrayUtils; |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * æä»¶å¤çå·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class FileUtils |
| | | { |
| | | /** å符常éï¼ææ {@code '/'} */ |
| | | public static final char SLASH = '/'; |
| | | |
| | | /** å符常éï¼åææ {@code '\\'} */ |
| | | public static final char BACKSLASH = '\\'; |
| | | |
| | | public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+"; |
| | | |
| | | /** |
| | | * è¾åºæå®æä»¶çbyteæ°ç» |
| | | * |
| | | * @param filePath æä»¶è·¯å¾ |
| | | * @param os è¾åºæµ |
| | | * @return |
| | | */ |
| | | public static void writeBytes(String filePath, OutputStream os) throws IOException |
| | | { |
| | | FileInputStream fis = null; |
| | | try |
| | | { |
| | | File file = new File(filePath); |
| | | if (!file.exists()) |
| | | { |
| | | throw new FileNotFoundException(filePath); |
| | | } |
| | | fis = new FileInputStream(file); |
| | | byte[] b = new byte[1024]; |
| | | int length; |
| | | while ((length = fis.read(b)) > 0) |
| | | { |
| | | os.write(b, 0, length); |
| | | } |
| | | } |
| | | catch (IOException e) |
| | | { |
| | | throw e; |
| | | } |
| | | finally |
| | | { |
| | | if (os != null) |
| | | { |
| | | try |
| | | { |
| | | os.close(); |
| | | } |
| | | catch (IOException e1) |
| | | { |
| | | e1.printStackTrace(); |
| | | } |
| | | } |
| | | if (fis != null) |
| | | { |
| | | try |
| | | { |
| | | fis.close(); |
| | | } |
| | | catch (IOException e1) |
| | | { |
| | | e1.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å 餿件 |
| | | * |
| | | * @param filePath æä»¶ |
| | | * @return |
| | | */ |
| | | public static boolean deleteFile(String filePath) |
| | | { |
| | | boolean flag = false; |
| | | File file = new File(filePath); |
| | | // è·¯å¾ä¸ºæä»¶ä¸ä¸ä¸ºç©ºåè¿è¡å é¤ |
| | | if (file.isFile() && file.exists()) |
| | | { |
| | | flag = file.delete(); |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | /** |
| | | * æä»¶åç§°éªè¯ |
| | | * |
| | | * @param filename æä»¶åç§° |
| | | * @return true æ£å¸¸ false éæ³ |
| | | */ |
| | | public static boolean isValidFilename(String filename) |
| | | { |
| | | return filename.matches(FILENAME_PATTERN); |
| | | } |
| | | |
| | | /** |
| | | * æ ¡éªæä»¶è·¯å¾åæ³æ§ï¼å®å
¨æ§ä¸æ©å±åï¼ |
| | | * |
| | | * @param fileUrl å¾
æ ¡éªçæä»¶å°å |
| | | * @return true æ£å¸¸ false éæ³ |
| | | */ |
| | | public static boolean validateFilePath(String fileUrl) |
| | | { |
| | | // ç¦æ¢ç®å½ä¸è·³çº§å« |
| | | if (StringUtils.contains(fileUrl, "..")) |
| | | { |
| | | return false; |
| | | } |
| | | // 夿æ¯å¦å¨å
许ä¸è½½çæä»¶è§åå
|
| | | return ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(fileUrl)); |
| | | } |
| | | |
| | | /** |
| | | * ä¸è½½æä»¶åéæ°ç¼ç |
| | | * |
| | | * @param request 请æ±å¯¹è±¡ |
| | | * @param fileName æä»¶å |
| | | * @return ç¼ç åçæä»¶å |
| | | */ |
| | | public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException |
| | | { |
| | | final String agent = request.getHeader("USER-AGENT"); |
| | | String filename = fileName; |
| | | if (agent.contains("MSIE")) |
| | | { |
| | | // IEæµè§å¨ |
| | | filename = URLEncoder.encode(filename, "utf-8"); |
| | | filename = filename.replace("+", " "); |
| | | } |
| | | else if (agent.contains("Firefox")) |
| | | { |
| | | // ç«çæµè§å¨ |
| | | filename = new String(fileName.getBytes(), "ISO8859-1"); |
| | | } |
| | | else if (agent.contains("Chrome")) |
| | | { |
| | | // googleæµè§å¨ |
| | | filename = URLEncoder.encode(filename, "utf-8"); |
| | | } |
| | | else |
| | | { |
| | | // å
¶å®æµè§å¨ |
| | | filename = URLEncoder.encode(filename, "utf-8"); |
| | | } |
| | | return filename; |
| | | } |
| | | |
| | | /** |
| | | * è¿åæä»¶å |
| | | * |
| | | * @param filePath æä»¶ |
| | | * @return æä»¶å |
| | | */ |
| | | public static String getName(String filePath) |
| | | { |
| | | if (null == filePath) |
| | | { |
| | | return null; |
| | | } |
| | | int len = filePath.length(); |
| | | if (0 == len) |
| | | { |
| | | return filePath; |
| | | } |
| | | if (isFileSeparator(filePath.charAt(len - 1))) |
| | | { |
| | | // 以åé符ç»å°¾ç廿ç»å°¾åé符 |
| | | len--; |
| | | } |
| | | |
| | | int begin = 0; |
| | | char c; |
| | | for (int i = len - 1; i > -1; i--) |
| | | { |
| | | c = filePath.charAt(i); |
| | | if (isFileSeparator(c)) |
| | | { |
| | | // æ¥æ¾æåä¸ä¸ªè·¯å¾åé符ï¼/æè
\ï¼ |
| | | begin = i + 1; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | return filePath.substring(begin, len); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦ä¸ºWindowsæè
Linuxï¼Unixï¼æä»¶åé符<br> |
| | | * Windowså¹³å°ä¸åé符为\ï¼Linuxï¼Unixï¼ä¸º/ |
| | | * |
| | | * @param c å符 |
| | | * @return æ¯å¦ä¸ºWindowsæè
Linuxï¼Unixï¼æä»¶åé符 |
| | | */ |
| | | public static boolean isFileSeparator(char c) |
| | | { |
| | | return SLASH == c || BACKSLASH == c; |
| | | } |
| | | |
| | | /** |
| | | * ä¸è½½æä»¶åéæ°ç¼ç |
| | | * |
| | | * @param response ååºå¯¹è±¡ |
| | | * @param realFileName ç宿件å |
| | | * @return |
| | | */ |
| | | public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException |
| | | { |
| | | String percentEncodedFileName = percentEncode(realFileName); |
| | | |
| | | StringBuilder contentDispositionValue = new StringBuilder(); |
| | | contentDispositionValue.append("attachment; filename=") |
| | | .append(percentEncodedFileName) |
| | | .append(";") |
| | | .append("filename*=") |
| | | .append("utf-8''") |
| | | .append(percentEncodedFileName); |
| | | |
| | | response.setHeader("Content-disposition", contentDispositionValue.toString()); |
| | | response.setHeader("download-filename", percentEncodedFileName); |
| | | } |
| | | |
| | | /** |
| | | * ç¾åå·ç¼ç å·¥å
·æ¹æ³ |
| | | * |
| | | * @param s éè¦ç¾åå·ç¼ç çå符串 |
| | | * @return ç¾åå·ç¼ç åçå符串 |
| | | */ |
| | | public static String percentEncode(String s) throws UnsupportedEncodingException |
| | | { |
| | | String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString()); |
| | | return encode.replaceAll("\\+", "%20"); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.file; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | import java.net.URLConnection; |
| | | import java.util.Arrays; |
| | | import org.apache.poi.util.IOUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | /** |
| | | * å¾çå¤çå·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class ImageUtils |
| | | { |
| | | private static final Logger log = LoggerFactory.getLogger(ImageUtils.class); |
| | | |
| | | public static byte[] getImage(String imagePath) |
| | | { |
| | | InputStream is = getFile(imagePath); |
| | | try |
| | | { |
| | | return IOUtils.toByteArray(is); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("å¾çå è½½å¼å¸¸ {}", e); |
| | | return null; |
| | | } |
| | | finally |
| | | { |
| | | IOUtils.closeQuietly(is); |
| | | } |
| | | } |
| | | |
| | | public static InputStream getFile(String imagePath) |
| | | { |
| | | try |
| | | { |
| | | byte[] result = readFile(imagePath); |
| | | result = Arrays.copyOf(result, result.length); |
| | | return new ByteArrayInputStream(result); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("è·åå¾çå¼å¸¸ {}", e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 读åæä»¶ä¸ºåèæ°æ® |
| | | * |
| | | * @param url å°å |
| | | * @return åèæ°æ® |
| | | */ |
| | | public static byte[] readFile(String url) |
| | | { |
| | | InputStream in = null; |
| | | try |
| | | { |
| | | // ç½ç»å°å |
| | | URL urlObj = new URL(url); |
| | | URLConnection urlConnection = urlObj.openConnection(); |
| | | urlConnection.setConnectTimeout(30 * 1000); |
| | | urlConnection.setReadTimeout(60 * 1000); |
| | | urlConnection.setDoInput(true); |
| | | in = urlConnection.getInputStream(); |
| | | return IOUtils.toByteArray(in); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("è®¿é®æä»¶å¼å¸¸ {}", e); |
| | | return null; |
| | | } |
| | | finally |
| | | { |
| | | IOUtils.closeQuietly(in); |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.file; |
| | | |
| | | /** |
| | | * åªä½ç±»åå·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class MimeTypeUtils |
| | | { |
| | | public static final String IMAGE_PNG = "image/png"; |
| | | |
| | | public static final String IMAGE_JPG = "image/jpg"; |
| | | |
| | | public static final String IMAGE_JPEG = "image/jpeg"; |
| | | |
| | | public static final String IMAGE_BMP = "image/bmp"; |
| | | |
| | | public static final String IMAGE_GIF = "image/gif"; |
| | | |
| | | public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png" }; |
| | | |
| | | public static final String[] FLASH_EXTENSION = { "swf", "flv" }; |
| | | |
| | | public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg", |
| | | "asf", "rm", "rmvb" }; |
| | | |
| | | public static final String[] VIDEO_EXTENSION = { "mp4", "avi", "rmvb" }; |
| | | |
| | | public static final String[] DEFAULT_ALLOWED_EXTENSION = { |
| | | // å¾ç |
| | | "bmp", "gif", "jpg", "jpeg", "png", |
| | | // word excel powerpoint |
| | | "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt", |
| | | // å缩æä»¶ |
| | | "rar", "zip", "gz", "bz2", |
| | | // è§é¢æ ¼å¼ |
| | | "mp4", "avi", "rmvb", |
| | | // pdf |
| | | "pdf" }; |
| | | |
| | | public static String getExtension(String prefix) |
| | | { |
| | | switch (prefix) |
| | | { |
| | | case IMAGE_PNG: |
| | | return "png"; |
| | | case IMAGE_JPG: |
| | | return "jpg"; |
| | | case IMAGE_JPEG: |
| | | return "jpeg"; |
| | | case IMAGE_BMP: |
| | | return "bmp"; |
| | | case IMAGE_GIF: |
| | | return "gif"; |
| | | default: |
| | | return ""; |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.html; |
| | | |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * 转ä¹åå转ä¹å·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class EscapeUtil |
| | | { |
| | | public static final String RE_HTML_MARK = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)"; |
| | | |
| | | private static final char[][] TEXT = new char[64][]; |
| | | |
| | | static |
| | | { |
| | | for (int i = 0; i < 64; i++) |
| | | { |
| | | TEXT[i] = new char[] { (char) i }; |
| | | } |
| | | |
| | | // special HTML characters |
| | | TEXT['\''] = "'".toCharArray(); // åå¼å· |
| | | TEXT['"'] = """.toCharArray(); // åå¼å· |
| | | TEXT['&'] = "&".toCharArray(); // &符 |
| | | TEXT['<'] = "<".toCharArray(); // å°äºå· |
| | | TEXT['>'] = ">".toCharArray(); // 大äºå· |
| | | } |
| | | |
| | | /** |
| | | * è½¬ä¹ææ¬ä¸çHTMLå符为å®å
¨çå符 |
| | | * |
| | | * @param text 被转ä¹çææ¬ |
| | | * @return 转ä¹åçææ¬ |
| | | */ |
| | | public static String escape(String text) |
| | | { |
| | | return encode(text); |
| | | } |
| | | |
| | | /** |
| | | * è¿å被转ä¹çHTMLç¹æ®å符 |
| | | * |
| | | * @param content å
å«è½¬ä¹ç¬¦çHTMLå
容 |
| | | * @return 转æ¢åçå符串 |
| | | */ |
| | | public static String unescape(String content) |
| | | { |
| | | return decode(content); |
| | | } |
| | | |
| | | /** |
| | | * æ¸
餿æHTMLæ ç¾ï¼ä½æ¯ä¸å 餿 ç¾å
çå
容 |
| | | * |
| | | * @param content ææ¬ |
| | | * @return æ¸
餿 ç¾åçææ¬ |
| | | */ |
| | | public static String clean(String content) |
| | | { |
| | | return new HTMLFilter().filter(content); |
| | | } |
| | | |
| | | /** |
| | | * Escapeç¼ç |
| | | * |
| | | * @param text 被ç¼ç çææ¬ |
| | | * @return ç¼ç åçå符 |
| | | */ |
| | | private static String encode(String text) |
| | | { |
| | | if (StringUtils.isEmpty(text)) |
| | | { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | |
| | | final StringBuilder tmp = new StringBuilder(text.length() * 6); |
| | | char c; |
| | | for (int i = 0; i < text.length(); i++) |
| | | { |
| | | c = text.charAt(i); |
| | | if (c < 256) |
| | | { |
| | | tmp.append("%"); |
| | | if (c < 16) |
| | | { |
| | | tmp.append("0"); |
| | | } |
| | | tmp.append(Integer.toString(c, 16)); |
| | | } |
| | | else |
| | | { |
| | | tmp.append("%u"); |
| | | if (c <= 0xfff) |
| | | { |
| | | // issue#I49JU8@Gitee |
| | | tmp.append("0"); |
| | | } |
| | | tmp.append(Integer.toString(c, 16)); |
| | | } |
| | | } |
| | | return tmp.toString(); |
| | | } |
| | | |
| | | /** |
| | | * Escapeè§£ç |
| | | * |
| | | * @param content 被转ä¹çå
容 |
| | | * @return è§£ç åçå符串 |
| | | */ |
| | | public static String decode(String content) |
| | | { |
| | | if (StringUtils.isEmpty(content)) |
| | | { |
| | | return content; |
| | | } |
| | | |
| | | StringBuilder tmp = new StringBuilder(content.length()); |
| | | int lastPos = 0, pos = 0; |
| | | char ch; |
| | | while (lastPos < content.length()) |
| | | { |
| | | pos = content.indexOf("%", lastPos); |
| | | if (pos == lastPos) |
| | | { |
| | | if (content.charAt(pos + 1) == 'u') |
| | | { |
| | | ch = (char) Integer.parseInt(content.substring(pos + 2, pos + 6), 16); |
| | | tmp.append(ch); |
| | | lastPos = pos + 6; |
| | | } |
| | | else |
| | | { |
| | | ch = (char) Integer.parseInt(content.substring(pos + 1, pos + 3), 16); |
| | | tmp.append(ch); |
| | | lastPos = pos + 3; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | if (pos == -1) |
| | | { |
| | | tmp.append(content.substring(lastPos)); |
| | | lastPos = content.length(); |
| | | } |
| | | else |
| | | { |
| | | tmp.append(content.substring(lastPos, pos)); |
| | | lastPos = pos; |
| | | } |
| | | } |
| | | } |
| | | return tmp.toString(); |
| | | } |
| | | |
| | | public static void main(String[] args) |
| | | { |
| | | String html = "<script>alert(1);</script>"; |
| | | String escape = EscapeUtil.escape(html); |
| | | // String html = "<scr<script>ipt>alert(\"XSS\")</scr<script>ipt>"; |
| | | // String html = "<123"; |
| | | // String html = "123>"; |
| | | System.out.println("clean: " + EscapeUtil.clean(html)); |
| | | System.out.println("escape: " + escape); |
| | | System.out.println("unescape: " + EscapeUtil.unescape(escape)); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.html; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.concurrent.ConcurrentMap; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * HTMLè¿æ»¤å¨ï¼ç¨äºå»é¤XSSæ¼æ´éæ£ã |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public final class HTMLFilter |
| | | { |
| | | /** |
| | | * regex flag union representing /si modifiers in php |
| | | **/ |
| | | private static final int REGEX_FLAGS_SI = Pattern.CASE_INSENSITIVE | Pattern.DOTALL; |
| | | private static final Pattern P_COMMENTS = Pattern.compile("<!--(.*?)-->", Pattern.DOTALL); |
| | | private static final Pattern P_COMMENT = Pattern.compile("^!--(.*)--$", REGEX_FLAGS_SI); |
| | | private static final Pattern P_TAGS = Pattern.compile("<(.*?)>", Pattern.DOTALL); |
| | | private static final Pattern P_END_TAG = Pattern.compile("^/([a-z0-9]+)", REGEX_FLAGS_SI); |
| | | private static final Pattern P_START_TAG = Pattern.compile("^([a-z0-9]+)(.*?)(/?)$", REGEX_FLAGS_SI); |
| | | private static final Pattern P_QUOTED_ATTRIBUTES = Pattern.compile("([a-z0-9]+)=([\"'])(.*?)\\2", REGEX_FLAGS_SI); |
| | | private static final Pattern P_UNQUOTED_ATTRIBUTES = Pattern.compile("([a-z0-9]+)(=)([^\"\\s']+)", REGEX_FLAGS_SI); |
| | | private static final Pattern P_PROTOCOL = Pattern.compile("^([^:]+):", REGEX_FLAGS_SI); |
| | | private static final Pattern P_ENTITY = Pattern.compile("&#(\\d+);?"); |
| | | private static final Pattern P_ENTITY_UNICODE = Pattern.compile("&#x([0-9a-f]+);?"); |
| | | private static final Pattern P_ENCODE = Pattern.compile("%([0-9a-f]{2});?"); |
| | | private static final Pattern P_VALID_ENTITIES = Pattern.compile("&([^&;]*)(?=(;|&|$))"); |
| | | private static final Pattern P_VALID_QUOTES = Pattern.compile("(>|^)([^<]+?)(<|$)", Pattern.DOTALL); |
| | | private static final Pattern P_END_ARROW = Pattern.compile("^>"); |
| | | private static final Pattern P_BODY_TO_END = Pattern.compile("<([^>]*?)(?=<|$)"); |
| | | private static final Pattern P_XML_CONTENT = Pattern.compile("(^|>)([^<]*?)(?=>)"); |
| | | private static final Pattern P_STRAY_LEFT_ARROW = Pattern.compile("<([^>]*?)(?=<|$)"); |
| | | private static final Pattern P_STRAY_RIGHT_ARROW = Pattern.compile("(^|>)([^<]*?)(?=>)"); |
| | | private static final Pattern P_AMP = Pattern.compile("&"); |
| | | private static final Pattern P_QUOTE = Pattern.compile("\""); |
| | | private static final Pattern P_LEFT_ARROW = Pattern.compile("<"); |
| | | private static final Pattern P_RIGHT_ARROW = Pattern.compile(">"); |
| | | private static final Pattern P_BOTH_ARROWS = Pattern.compile("<>"); |
| | | |
| | | // @xxx could grow large... maybe use sesat's ReferenceMap |
| | | private static final ConcurrentMap<String, Pattern> P_REMOVE_PAIR_BLANKS = new ConcurrentHashMap<>(); |
| | | private static final ConcurrentMap<String, Pattern> P_REMOVE_SELF_BLANKS = new ConcurrentHashMap<>(); |
| | | |
| | | /** |
| | | * set of allowed html elements, along with allowed attributes for each element |
| | | **/ |
| | | private final Map<String, List<String>> vAllowed; |
| | | /** |
| | | * counts of open tags for each (allowable) html element |
| | | **/ |
| | | private final Map<String, Integer> vTagCounts = new HashMap<>(); |
| | | |
| | | /** |
| | | * html elements which must always be self-closing (e.g. "<img />") |
| | | **/ |
| | | private final String[] vSelfClosingTags; |
| | | /** |
| | | * html elements which must always have separate opening and closing tags (e.g. "<b></b>") |
| | | **/ |
| | | private final String[] vNeedClosingTags; |
| | | /** |
| | | * set of disallowed html elements |
| | | **/ |
| | | private final String[] vDisallowed; |
| | | /** |
| | | * attributes which should be checked for valid protocols |
| | | **/ |
| | | private final String[] vProtocolAtts; |
| | | /** |
| | | * allowed protocols |
| | | **/ |
| | | private final String[] vAllowedProtocols; |
| | | /** |
| | | * tags which should be removed if they contain no content (e.g. "<b></b>" or "<b />") |
| | | **/ |
| | | private final String[] vRemoveBlanks; |
| | | /** |
| | | * entities allowed within html markup |
| | | **/ |
| | | private final String[] vAllowedEntities; |
| | | /** |
| | | * flag determining whether comments are allowed in input String. |
| | | */ |
| | | private final boolean stripComment; |
| | | private final boolean encodeQuotes; |
| | | /** |
| | | * flag determining whether to try to make tags when presented with "unbalanced" angle brackets (e.g. "<b text </b>" |
| | | * becomes "<b> text </b>"). If set to false, unbalanced angle brackets will be html escaped. |
| | | */ |
| | | private final boolean alwaysMakeTags; |
| | | |
| | | /** |
| | | * Default constructor. |
| | | */ |
| | | public HTMLFilter() |
| | | { |
| | | vAllowed = new HashMap<>(); |
| | | |
| | | final ArrayList<String> a_atts = new ArrayList<>(); |
| | | a_atts.add("href"); |
| | | a_atts.add("target"); |
| | | vAllowed.put("a", a_atts); |
| | | |
| | | final ArrayList<String> img_atts = new ArrayList<>(); |
| | | img_atts.add("src"); |
| | | img_atts.add("width"); |
| | | img_atts.add("height"); |
| | | img_atts.add("alt"); |
| | | vAllowed.put("img", img_atts); |
| | | |
| | | final ArrayList<String> no_atts = new ArrayList<>(); |
| | | vAllowed.put("b", no_atts); |
| | | vAllowed.put("strong", no_atts); |
| | | vAllowed.put("i", no_atts); |
| | | vAllowed.put("em", no_atts); |
| | | |
| | | vSelfClosingTags = new String[] { "img" }; |
| | | vNeedClosingTags = new String[] { "a", "b", "strong", "i", "em" }; |
| | | vDisallowed = new String[] {}; |
| | | vAllowedProtocols = new String[] { "http", "mailto", "https" }; // no ftp. |
| | | vProtocolAtts = new String[] { "src", "href" }; |
| | | vRemoveBlanks = new String[] { "a", "b", "strong", "i", "em" }; |
| | | vAllowedEntities = new String[] { "amp", "gt", "lt", "quot" }; |
| | | stripComment = true; |
| | | encodeQuotes = true; |
| | | alwaysMakeTags = false; |
| | | } |
| | | |
| | | /** |
| | | * Map-parameter configurable constructor. |
| | | * |
| | | * @param conf map containing configuration. keys match field names. |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public HTMLFilter(final Map<String, Object> conf) |
| | | { |
| | | |
| | | assert conf.containsKey("vAllowed") : "configuration requires vAllowed"; |
| | | assert conf.containsKey("vSelfClosingTags") : "configuration requires vSelfClosingTags"; |
| | | assert conf.containsKey("vNeedClosingTags") : "configuration requires vNeedClosingTags"; |
| | | assert conf.containsKey("vDisallowed") : "configuration requires vDisallowed"; |
| | | assert conf.containsKey("vAllowedProtocols") : "configuration requires vAllowedProtocols"; |
| | | assert conf.containsKey("vProtocolAtts") : "configuration requires vProtocolAtts"; |
| | | assert conf.containsKey("vRemoveBlanks") : "configuration requires vRemoveBlanks"; |
| | | assert conf.containsKey("vAllowedEntities") : "configuration requires vAllowedEntities"; |
| | | |
| | | vAllowed = Collections.unmodifiableMap((HashMap<String, List<String>>) conf.get("vAllowed")); |
| | | vSelfClosingTags = (String[]) conf.get("vSelfClosingTags"); |
| | | vNeedClosingTags = (String[]) conf.get("vNeedClosingTags"); |
| | | vDisallowed = (String[]) conf.get("vDisallowed"); |
| | | vAllowedProtocols = (String[]) conf.get("vAllowedProtocols"); |
| | | vProtocolAtts = (String[]) conf.get("vProtocolAtts"); |
| | | vRemoveBlanks = (String[]) conf.get("vRemoveBlanks"); |
| | | vAllowedEntities = (String[]) conf.get("vAllowedEntities"); |
| | | stripComment = conf.containsKey("stripComment") ? (Boolean) conf.get("stripComment") : true; |
| | | encodeQuotes = conf.containsKey("encodeQuotes") ? (Boolean) conf.get("encodeQuotes") : true; |
| | | alwaysMakeTags = conf.containsKey("alwaysMakeTags") ? (Boolean) conf.get("alwaysMakeTags") : true; |
| | | } |
| | | |
| | | private void reset() |
| | | { |
| | | vTagCounts.clear(); |
| | | } |
| | | |
| | | // --------------------------------------------------------------- |
| | | // my versions of some PHP library functions |
| | | public static String chr(final int decimal) |
| | | { |
| | | return String.valueOf((char) decimal); |
| | | } |
| | | |
| | | public static String htmlSpecialChars(final String s) |
| | | { |
| | | String result = s; |
| | | result = regexReplace(P_AMP, "&", result); |
| | | result = regexReplace(P_QUOTE, """, result); |
| | | result = regexReplace(P_LEFT_ARROW, "<", result); |
| | | result = regexReplace(P_RIGHT_ARROW, ">", result); |
| | | return result; |
| | | } |
| | | |
| | | // --------------------------------------------------------------- |
| | | |
| | | /** |
| | | * given a user submitted input String, filter out any invalid or restricted html. |
| | | * |
| | | * @param input text (i.e. submitted by a user) than may contain html |
| | | * @return "clean" version of input, with only valid, whitelisted html elements allowed |
| | | */ |
| | | public String filter(final String input) |
| | | { |
| | | reset(); |
| | | String s = input; |
| | | |
| | | s = escapeComments(s); |
| | | |
| | | s = balanceHTML(s); |
| | | |
| | | s = checkTags(s); |
| | | |
| | | s = processRemoveBlanks(s); |
| | | |
| | | // s = validateEntities(s); |
| | | |
| | | return s; |
| | | } |
| | | |
| | | public boolean isAlwaysMakeTags() |
| | | { |
| | | return alwaysMakeTags; |
| | | } |
| | | |
| | | public boolean isStripComments() |
| | | { |
| | | return stripComment; |
| | | } |
| | | |
| | | private String escapeComments(final String s) |
| | | { |
| | | final Matcher m = P_COMMENTS.matcher(s); |
| | | final StringBuffer buf = new StringBuffer(); |
| | | if (m.find()) |
| | | { |
| | | final String match = m.group(1); // (.*?) |
| | | m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->")); |
| | | } |
| | | m.appendTail(buf); |
| | | |
| | | return buf.toString(); |
| | | } |
| | | |
| | | private String balanceHTML(String s) |
| | | { |
| | | if (alwaysMakeTags) |
| | | { |
| | | // |
| | | // try and form html |
| | | // |
| | | s = regexReplace(P_END_ARROW, "", s); |
| | | // ä¸è¿½å ç»ææ ç¾ |
| | | s = regexReplace(P_BODY_TO_END, "<$1>", s); |
| | | s = regexReplace(P_XML_CONTENT, "$1<$2", s); |
| | | |
| | | } |
| | | else |
| | | { |
| | | // |
| | | // escape stray brackets |
| | | // |
| | | s = regexReplace(P_STRAY_LEFT_ARROW, "<$1", s); |
| | | s = regexReplace(P_STRAY_RIGHT_ARROW, "$1$2><", s); |
| | | |
| | | // |
| | | // the last regexp causes '<>' entities to appear |
| | | // (we need to do a lookahead assertion so that the last bracket can |
| | | // be used in the next pass of the regexp) |
| | | // |
| | | s = regexReplace(P_BOTH_ARROWS, "", s); |
| | | } |
| | | |
| | | return s; |
| | | } |
| | | |
| | | private String checkTags(String s) |
| | | { |
| | | Matcher m = P_TAGS.matcher(s); |
| | | |
| | | final StringBuffer buf = new StringBuffer(); |
| | | while (m.find()) |
| | | { |
| | | String replaceStr = m.group(1); |
| | | replaceStr = processTag(replaceStr); |
| | | m.appendReplacement(buf, Matcher.quoteReplacement(replaceStr)); |
| | | } |
| | | m.appendTail(buf); |
| | | |
| | | // these get tallied in processTag |
| | | // (remember to reset before subsequent calls to filter method) |
| | | final StringBuilder sBuilder = new StringBuilder(buf.toString()); |
| | | for (String key : vTagCounts.keySet()) |
| | | { |
| | | for (int ii = 0; ii < vTagCounts.get(key); ii++) |
| | | { |
| | | sBuilder.append("</").append(key).append(">"); |
| | | } |
| | | } |
| | | s = sBuilder.toString(); |
| | | |
| | | return s; |
| | | } |
| | | |
| | | private String processRemoveBlanks(final String s) |
| | | { |
| | | String result = s; |
| | | for (String tag : vRemoveBlanks) |
| | | { |
| | | if (!P_REMOVE_PAIR_BLANKS.containsKey(tag)) |
| | | { |
| | | P_REMOVE_PAIR_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?></" + tag + ">")); |
| | | } |
| | | result = regexReplace(P_REMOVE_PAIR_BLANKS.get(tag), "", result); |
| | | if (!P_REMOVE_SELF_BLANKS.containsKey(tag)) |
| | | { |
| | | P_REMOVE_SELF_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?/>")); |
| | | } |
| | | result = regexReplace(P_REMOVE_SELF_BLANKS.get(tag), "", result); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | private static String regexReplace(final Pattern regex_pattern, final String replacement, final String s) |
| | | { |
| | | Matcher m = regex_pattern.matcher(s); |
| | | return m.replaceAll(replacement); |
| | | } |
| | | |
| | | private String processTag(final String s) |
| | | { |
| | | // ending tags |
| | | Matcher m = P_END_TAG.matcher(s); |
| | | if (m.find()) |
| | | { |
| | | final String name = m.group(1).toLowerCase(); |
| | | if (allowed(name)) |
| | | { |
| | | if (!inArray(name, vSelfClosingTags)) |
| | | { |
| | | if (vTagCounts.containsKey(name)) |
| | | { |
| | | vTagCounts.put(name, vTagCounts.get(name) - 1); |
| | | return "</" + name + ">"; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // starting tags |
| | | m = P_START_TAG.matcher(s); |
| | | if (m.find()) |
| | | { |
| | | final String name = m.group(1).toLowerCase(); |
| | | final String body = m.group(2); |
| | | String ending = m.group(3); |
| | | |
| | | // debug( "in a starting tag, name='" + name + "'; body='" + body + "'; ending='" + ending + "'" ); |
| | | if (allowed(name)) |
| | | { |
| | | final StringBuilder params = new StringBuilder(); |
| | | |
| | | final Matcher m2 = P_QUOTED_ATTRIBUTES.matcher(body); |
| | | final Matcher m3 = P_UNQUOTED_ATTRIBUTES.matcher(body); |
| | | final List<String> paramNames = new ArrayList<>(); |
| | | final List<String> paramValues = new ArrayList<>(); |
| | | while (m2.find()) |
| | | { |
| | | paramNames.add(m2.group(1)); // ([a-z0-9]+) |
| | | paramValues.add(m2.group(3)); // (.*?) |
| | | } |
| | | while (m3.find()) |
| | | { |
| | | paramNames.add(m3.group(1)); // ([a-z0-9]+) |
| | | paramValues.add(m3.group(3)); // ([^\"\\s']+) |
| | | } |
| | | |
| | | String paramName, paramValue; |
| | | for (int ii = 0; ii < paramNames.size(); ii++) |
| | | { |
| | | paramName = paramNames.get(ii).toLowerCase(); |
| | | paramValue = paramValues.get(ii); |
| | | |
| | | // debug( "paramName='" + paramName + "'" ); |
| | | // debug( "paramValue='" + paramValue + "'" ); |
| | | // debug( "allowed? " + vAllowed.get( name ).contains( paramName ) ); |
| | | |
| | | if (allowedAttribute(name, paramName)) |
| | | { |
| | | if (inArray(paramName, vProtocolAtts)) |
| | | { |
| | | paramValue = processParamProtocol(paramValue); |
| | | } |
| | | params.append(' ').append(paramName).append("=\\\"").append(paramValue).append("\\\""); |
| | | } |
| | | } |
| | | |
| | | if (inArray(name, vSelfClosingTags)) |
| | | { |
| | | ending = " /"; |
| | | } |
| | | |
| | | if (inArray(name, vNeedClosingTags)) |
| | | { |
| | | ending = ""; |
| | | } |
| | | |
| | | if (ending == null || ending.length() < 1) |
| | | { |
| | | if (vTagCounts.containsKey(name)) |
| | | { |
| | | vTagCounts.put(name, vTagCounts.get(name) + 1); |
| | | } |
| | | else |
| | | { |
| | | vTagCounts.put(name, 1); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | ending = " /"; |
| | | } |
| | | return "<" + name + params + ending + ">"; |
| | | } |
| | | else |
| | | { |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | // comments |
| | | m = P_COMMENT.matcher(s); |
| | | if (!stripComment && m.find()) |
| | | { |
| | | return "<" + m.group() + ">"; |
| | | } |
| | | |
| | | return ""; |
| | | } |
| | | |
| | | private String processParamProtocol(String s) |
| | | { |
| | | s = decodeEntities(s); |
| | | final Matcher m = P_PROTOCOL.matcher(s); |
| | | if (m.find()) |
| | | { |
| | | final String protocol = m.group(1); |
| | | if (!inArray(protocol, vAllowedProtocols)) |
| | | { |
| | | // bad protocol, turn into local anchor link instead |
| | | s = "#" + s.substring(protocol.length() + 1); |
| | | if (s.startsWith("#//")) |
| | | { |
| | | s = "#" + s.substring(3); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return s; |
| | | } |
| | | |
| | | private String decodeEntities(String s) |
| | | { |
| | | StringBuffer buf = new StringBuffer(); |
| | | |
| | | Matcher m = P_ENTITY.matcher(s); |
| | | while (m.find()) |
| | | { |
| | | final String match = m.group(1); |
| | | final int decimal = Integer.decode(match).intValue(); |
| | | m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal))); |
| | | } |
| | | m.appendTail(buf); |
| | | s = buf.toString(); |
| | | |
| | | buf = new StringBuffer(); |
| | | m = P_ENTITY_UNICODE.matcher(s); |
| | | while (m.find()) |
| | | { |
| | | final String match = m.group(1); |
| | | final int decimal = Integer.valueOf(match, 16).intValue(); |
| | | m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal))); |
| | | } |
| | | m.appendTail(buf); |
| | | s = buf.toString(); |
| | | |
| | | buf = new StringBuffer(); |
| | | m = P_ENCODE.matcher(s); |
| | | while (m.find()) |
| | | { |
| | | final String match = m.group(1); |
| | | final int decimal = Integer.valueOf(match, 16).intValue(); |
| | | m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal))); |
| | | } |
| | | m.appendTail(buf); |
| | | s = buf.toString(); |
| | | |
| | | s = validateEntities(s); |
| | | return s; |
| | | } |
| | | |
| | | private String validateEntities(final String s) |
| | | { |
| | | StringBuffer buf = new StringBuffer(); |
| | | |
| | | // validate entities throughout the string |
| | | Matcher m = P_VALID_ENTITIES.matcher(s); |
| | | while (m.find()) |
| | | { |
| | | final String one = m.group(1); // ([^&;]*) |
| | | final String two = m.group(2); // (?=(;|&|$)) |
| | | m.appendReplacement(buf, Matcher.quoteReplacement(checkEntity(one, two))); |
| | | } |
| | | m.appendTail(buf); |
| | | |
| | | return encodeQuotes(buf.toString()); |
| | | } |
| | | |
| | | private String encodeQuotes(final String s) |
| | | { |
| | | if (encodeQuotes) |
| | | { |
| | | StringBuffer buf = new StringBuffer(); |
| | | Matcher m = P_VALID_QUOTES.matcher(s); |
| | | while (m.find()) |
| | | { |
| | | final String one = m.group(1); // (>|^) |
| | | final String two = m.group(2); // ([^<]+?) |
| | | final String three = m.group(3); // (<|$) |
| | | // 䏿¿æ¢åå¼å·ä¸º"ï¼é²æ¢jsonæ ¼å¼æ æ regexReplace(P_QUOTE, """, two) |
| | | m.appendReplacement(buf, Matcher.quoteReplacement(one + two + three)); |
| | | } |
| | | m.appendTail(buf); |
| | | return buf.toString(); |
| | | } |
| | | else |
| | | { |
| | | return s; |
| | | } |
| | | } |
| | | |
| | | private String checkEntity(final String preamble, final String term) |
| | | { |
| | | |
| | | return ";".equals(term) && isValidEntity(preamble) ? '&' + preamble : "&" + preamble; |
| | | } |
| | | |
| | | private boolean isValidEntity(final String entity) |
| | | { |
| | | return inArray(entity, vAllowedEntities); |
| | | } |
| | | |
| | | private static boolean inArray(final String s, final String[] array) |
| | | { |
| | | for (String item : array) |
| | | { |
| | | if (item != null && item.equals(s)) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | private boolean allowed(final String name) |
| | | { |
| | | return (vAllowed.isEmpty() || vAllowed.containsKey(name)) && !inArray(name, vDisallowed); |
| | | } |
| | | |
| | | private boolean allowedAttribute(final String name, final String paramName) |
| | | { |
| | | return allowed(name) && (vAllowed.isEmpty() || vAllowed.get(name).contains(paramName)); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.ip; |
| | | |
| | | import java.net.InetAddress; |
| | | import java.net.UnknownHostException; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import com.terra.common.utils.ServletUtils; |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * è·åIPæ¹æ³ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class IpUtils |
| | | { |
| | | public final static String REGX_0_255 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)"; |
| | | // å¹é
ip |
| | | public final static String REGX_IP = "((" + REGX_0_255 + "\\.){3}" + REGX_0_255 + ")"; |
| | | public final static String REGX_IP_WILDCARD = "(((\\*\\.){3}\\*)|(" + REGX_0_255 + "(\\.\\*){3})|(" + REGX_0_255 + "\\." + REGX_0_255 + ")(\\.\\*){2}" + "|((" + REGX_0_255 + "\\.){3}\\*))"; |
| | | // å¹é
ç½æ®µ |
| | | public final static String REGX_IP_SEG = "(" + REGX_IP + "\\-" + REGX_IP + ")"; |
| | | |
| | | /** |
| | | * è·å客æ·ç«¯IP |
| | | * |
| | | * @return IPå°å |
| | | */ |
| | | public static String getIpAddr() |
| | | { |
| | | return getIpAddr(ServletUtils.getRequest()); |
| | | } |
| | | |
| | | /** |
| | | * è·å客æ·ç«¯IP |
| | | * |
| | | * @param request 请æ±å¯¹è±¡ |
| | | * @return IPå°å |
| | | */ |
| | | public static String getIpAddr(HttpServletRequest request) |
| | | { |
| | | if (request == null) |
| | | { |
| | | return "unknown"; |
| | | } |
| | | String ip = request.getHeader("x-forwarded-for"); |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) |
| | | { |
| | | ip = request.getHeader("Proxy-Client-IP"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) |
| | | { |
| | | ip = request.getHeader("X-Forwarded-For"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) |
| | | { |
| | | ip = request.getHeader("WL-Proxy-Client-IP"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) |
| | | { |
| | | ip = request.getHeader("X-Real-IP"); |
| | | } |
| | | |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) |
| | | { |
| | | ip = request.getRemoteAddr(); |
| | | } |
| | | |
| | | return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip); |
| | | } |
| | | |
| | | /** |
| | | * æ£æ¥æ¯å¦ä¸ºå
é¨IPå°å |
| | | * |
| | | * @param ip IPå°å |
| | | * @return ç»æ |
| | | */ |
| | | public static boolean internalIp(String ip) |
| | | { |
| | | byte[] addr = textToNumericFormatV4(ip); |
| | | return internalIp(addr) || "127.0.0.1".equals(ip); |
| | | } |
| | | |
| | | /** |
| | | * æ£æ¥æ¯å¦ä¸ºå
é¨IPå°å |
| | | * |
| | | * @param addr byteå°å |
| | | * @return ç»æ |
| | | */ |
| | | private static boolean internalIp(byte[] addr) |
| | | { |
| | | if (StringUtils.isNull(addr) || addr.length < 2) |
| | | { |
| | | return true; |
| | | } |
| | | final byte b0 = addr[0]; |
| | | final byte b1 = addr[1]; |
| | | // 10.x.x.x/8 |
| | | final byte SECTION_1 = 0x0A; |
| | | // 172.16.x.x/12 |
| | | final byte SECTION_2 = (byte) 0xAC; |
| | | final byte SECTION_3 = (byte) 0x10; |
| | | final byte SECTION_4 = (byte) 0x1F; |
| | | // 192.168.x.x/16 |
| | | final byte SECTION_5 = (byte) 0xC0; |
| | | final byte SECTION_6 = (byte) 0xA8; |
| | | switch (b0) |
| | | { |
| | | case SECTION_1: |
| | | return true; |
| | | case SECTION_2: |
| | | if (b1 >= SECTION_3 && b1 <= SECTION_4) |
| | | { |
| | | return true; |
| | | } |
| | | case SECTION_5: |
| | | switch (b1) |
| | | { |
| | | case SECTION_6: |
| | | return true; |
| | | } |
| | | default: |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å°IPv4å°åè½¬æ¢æåè |
| | | * |
| | | * @param text IPv4å°å |
| | | * @return byte åè |
| | | */ |
| | | public static byte[] textToNumericFormatV4(String text) |
| | | { |
| | | if (text.length() == 0) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | byte[] bytes = new byte[4]; |
| | | String[] elements = text.split("\\.", -1); |
| | | try |
| | | { |
| | | long l; |
| | | int i; |
| | | switch (elements.length) |
| | | { |
| | | case 1: |
| | | l = Long.parseLong(elements[0]); |
| | | if ((l < 0L) || (l > 4294967295L)) |
| | | { |
| | | return null; |
| | | } |
| | | bytes[0] = (byte) (int) (l >> 24 & 0xFF); |
| | | bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF); |
| | | bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF); |
| | | bytes[3] = (byte) (int) (l & 0xFF); |
| | | break; |
| | | case 2: |
| | | l = Integer.parseInt(elements[0]); |
| | | if ((l < 0L) || (l > 255L)) |
| | | { |
| | | return null; |
| | | } |
| | | bytes[0] = (byte) (int) (l & 0xFF); |
| | | l = Integer.parseInt(elements[1]); |
| | | if ((l < 0L) || (l > 16777215L)) |
| | | { |
| | | return null; |
| | | } |
| | | bytes[1] = (byte) (int) (l >> 16 & 0xFF); |
| | | bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF); |
| | | bytes[3] = (byte) (int) (l & 0xFF); |
| | | break; |
| | | case 3: |
| | | for (i = 0; i < 2; ++i) |
| | | { |
| | | l = Integer.parseInt(elements[i]); |
| | | if ((l < 0L) || (l > 255L)) |
| | | { |
| | | return null; |
| | | } |
| | | bytes[i] = (byte) (int) (l & 0xFF); |
| | | } |
| | | l = Integer.parseInt(elements[2]); |
| | | if ((l < 0L) || (l > 65535L)) |
| | | { |
| | | return null; |
| | | } |
| | | bytes[2] = (byte) (int) (l >> 8 & 0xFF); |
| | | bytes[3] = (byte) (int) (l & 0xFF); |
| | | break; |
| | | case 4: |
| | | for (i = 0; i < 4; ++i) |
| | | { |
| | | l = Integer.parseInt(elements[i]); |
| | | if ((l < 0L) || (l > 255L)) |
| | | { |
| | | return null; |
| | | } |
| | | bytes[i] = (byte) (int) (l & 0xFF); |
| | | } |
| | | break; |
| | | default: |
| | | return null; |
| | | } |
| | | } |
| | | catch (NumberFormatException e) |
| | | { |
| | | return null; |
| | | } |
| | | return bytes; |
| | | } |
| | | |
| | | /** |
| | | * è·åIPå°å |
| | | * |
| | | * @return æ¬å°IPå°å |
| | | */ |
| | | public static String getHostIp() |
| | | { |
| | | try |
| | | { |
| | | return InetAddress.getLocalHost().getHostAddress(); |
| | | } |
| | | catch (UnknownHostException e) |
| | | { |
| | | } |
| | | return "127.0.0.1"; |
| | | } |
| | | |
| | | /** |
| | | * è·å主æºå |
| | | * |
| | | * @return æ¬å°ä¸»æºå |
| | | */ |
| | | public static String getHostName() |
| | | { |
| | | try |
| | | { |
| | | return InetAddress.getLocalHost().getHostName(); |
| | | } |
| | | catch (UnknownHostException e) |
| | | { |
| | | } |
| | | return "æªç¥"; |
| | | } |
| | | |
| | | /** |
| | | * ä»å¤çº§åå代çä¸è·å¾ç¬¬ä¸ä¸ªéunknown IPå°å |
| | | * |
| | | * @param ip è·å¾çIPå°å |
| | | * @return 第ä¸ä¸ªéunknown IPå°å |
| | | */ |
| | | public static String getMultistageReverseProxyIp(String ip) |
| | | { |
| | | // å¤çº§ååä»£çæ£æµ |
| | | if (ip != null && ip.indexOf(",") > 0) |
| | | { |
| | | final String[] ips = ip.trim().split(","); |
| | | for (String subIp : ips) |
| | | { |
| | | if (false == isUnknown(subIp)) |
| | | { |
| | | ip = subIp; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | return StringUtils.substring(ip, 0, 255); |
| | | } |
| | | |
| | | /** |
| | | * æ£æµç»å®å符串æ¯å¦ä¸ºæªç¥ï¼å¤ç¨äºæ£æµHTTP请æ±ç¸å
³ |
| | | * |
| | | * @param checkString è¢«æ£æµçå符串 |
| | | * @return æ¯å¦æªç¥ |
| | | */ |
| | | public static boolean isUnknown(String checkString) |
| | | { |
| | | return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦ä¸ºIP |
| | | */ |
| | | public static boolean isIP(String ip) |
| | | { |
| | | return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦ä¸ºIPï¼æ *为é´éçéé
符å°å |
| | | */ |
| | | public static boolean isIpWildCard(String ip) |
| | | { |
| | | return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP_WILDCARD); |
| | | } |
| | | |
| | | /** |
| | | * æ£æµåæ°æ¯å¦å¨ipéé
符é |
| | | */ |
| | | public static boolean ipIsInWildCardNoCheck(String ipWildCard, String ip) |
| | | { |
| | | String[] s1 = ipWildCard.split("\\."); |
| | | String[] s2 = ip.split("\\."); |
| | | boolean isMatchedSeg = true; |
| | | for (int i = 0; i < s1.length && !s1[i].equals("*"); i++) |
| | | { |
| | | if (!s1[i].equals(s2[i])) |
| | | { |
| | | isMatchedSeg = false; |
| | | break; |
| | | } |
| | | } |
| | | return isMatchedSeg; |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦ä¸ºç¹å®æ ¼å¼å¦:â10.10.10.1-10.10.10.99âçip段å符串 |
| | | */ |
| | | public static boolean isIPSegment(String ipSeg) |
| | | { |
| | | return StringUtils.isNotBlank(ipSeg) && ipSeg.matches(REGX_IP_SEG); |
| | | } |
| | | |
| | | /** |
| | | * 夿ipæ¯å¦å¨æå®ç½æ®µä¸ |
| | | */ |
| | | public static boolean ipIsInNetNoCheck(String iparea, String ip) |
| | | { |
| | | int idx = iparea.indexOf('-'); |
| | | String[] sips = iparea.substring(0, idx).split("\\."); |
| | | String[] sipe = iparea.substring(idx + 1).split("\\."); |
| | | String[] sipt = ip.split("\\."); |
| | | long ips = 0L, ipe = 0L, ipt = 0L; |
| | | for (int i = 0; i < 4; ++i) |
| | | { |
| | | ips = ips << 8 | Integer.parseInt(sips[i]); |
| | | ipe = ipe << 8 | Integer.parseInt(sipe[i]); |
| | | ipt = ipt << 8 | Integer.parseInt(sipt[i]); |
| | | } |
| | | if (ips > ipe) |
| | | { |
| | | long t = ips; |
| | | ips = ipe; |
| | | ipe = t; |
| | | } |
| | | return ips <= ipt && ipt <= ipe; |
| | | } |
| | | |
| | | /** |
| | | * æ ¡éªipæ¯å¦ç¬¦åè¿æ»¤ä¸²è§å |
| | | * |
| | | * @param filter è¿æ»¤IPå表,æ¯æåç¼'*'éé
,æ¯æç½æ®µå¦:`10.10.10.1-10.10.10.99` |
| | | * @param ip æ ¡éªIPå°å |
| | | * @return boolean ç»æ |
| | | */ |
| | | public static boolean isMatchedIp(String filter, String ip) |
| | | { |
| | | if (StringUtils.isEmpty(filter) || StringUtils.isEmpty(ip)) |
| | | { |
| | | return false; |
| | | } |
| | | String[] ips = filter.split(";"); |
| | | for (String iStr : ips) |
| | | { |
| | | if (isIP(iStr) && iStr.equals(ip)) |
| | | { |
| | | return true; |
| | | } |
| | | else if (isIpWildCard(iStr) && ipIsInWildCardNoCheck(iStr, ip)) |
| | | { |
| | | return true; |
| | | } |
| | | else if (isIPSegment(iStr) && ipIsInNetNoCheck(iStr, ip)) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.poi; |
| | | |
| | | import org.apache.poi.ss.usermodel.Cell; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | |
| | | /** |
| | | * Excelæ°æ®æ ¼å¼å¤çéé
å¨ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public interface ExcelHandlerAdapter |
| | | { |
| | | /** |
| | | * æ ¼å¼å |
| | | * |
| | | * @param value åå
æ ¼æ°æ®å¼ |
| | | * @param args excel注解argsåæ°ç» |
| | | * @param cell åå
æ ¼å¯¹è±¡ |
| | | * @param wb å·¥ä½ç°¿å¯¹è±¡ |
| | | * |
| | | * @return å¤çåçå¼ |
| | | */ |
| | | Object format(Object value, String[] args, Cell cell, Workbook wb); |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.poi; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.Method; |
| | | import java.lang.reflect.ParameterizedType; |
| | | import java.math.BigDecimal; |
| | | import java.text.DecimalFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Collection; |
| | | import java.util.Comparator; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import org.apache.commons.lang3.ArrayUtils; |
| | | import org.apache.commons.lang3.RegExUtils; |
| | | import org.apache.commons.lang3.reflect.FieldUtils; |
| | | import org.apache.poi.ss.usermodel.BorderStyle; |
| | | import org.apache.poi.ss.usermodel.Cell; |
| | | import org.apache.poi.ss.usermodel.CellStyle; |
| | | import org.apache.poi.ss.usermodel.CellType; |
| | | import org.apache.poi.ss.usermodel.ClientAnchor; |
| | | import org.apache.poi.ss.usermodel.DataFormat; |
| | | import org.apache.poi.ss.usermodel.DataValidation; |
| | | import org.apache.poi.ss.usermodel.DataValidationConstraint; |
| | | import org.apache.poi.ss.usermodel.DataValidationHelper; |
| | | import org.apache.poi.ss.usermodel.DateUtil; |
| | | import org.apache.poi.ss.usermodel.Drawing; |
| | | import org.apache.poi.ss.usermodel.FillPatternType; |
| | | import org.apache.poi.ss.usermodel.Font; |
| | | import org.apache.poi.ss.usermodel.HorizontalAlignment; |
| | | import org.apache.poi.ss.usermodel.IndexedColors; |
| | | import org.apache.poi.ss.usermodel.Name; |
| | | import org.apache.poi.ss.usermodel.Row; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.usermodel.VerticalAlignment; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.apache.poi.ss.usermodel.WorkbookFactory; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | import org.apache.poi.ss.util.CellRangeAddressList; |
| | | import org.apache.poi.util.IOUtils; |
| | | import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
| | | import org.apache.poi.xssf.usermodel.XSSFClientAnchor; |
| | | import org.apache.poi.xssf.usermodel.XSSFDataValidation; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import com.terra.common.annotation.Excel; |
| | | import com.terra.common.annotation.Excel.ColumnType; |
| | | import com.terra.common.annotation.Excel.Type; |
| | | import com.terra.common.annotation.Excels; |
| | | import com.terra.common.exception.UtilException; |
| | | import com.terra.common.text.Convert; |
| | | import com.terra.common.utils.DateUtils; |
| | | import com.terra.common.utils.StringUtils; |
| | | import com.terra.common.utils.file.FileTypeUtils; |
| | | import com.terra.common.utils.file.ImageUtils; |
| | | import com.terra.common.utils.reflect.ReflectUtils; |
| | | |
| | | /** |
| | | * Excelç¸å
³å¤ç |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class ExcelUtil<T> |
| | | { |
| | | private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class); |
| | | |
| | | public static final String SEPARATOR = ","; |
| | | |
| | | public static final String FORMULA_REGEX_STR = "=|-|\\+|@"; |
| | | |
| | | public static final String[] FORMULA_STR = { "=", "-", "+", "@" }; |
| | | |
| | | /** |
| | | * Excel sheetæå¤§è¡æ°ï¼é»è®¤65536 |
| | | */ |
| | | public static final int sheetSize = 65536; |
| | | |
| | | /** |
| | | * å·¥ä½è¡¨åç§° |
| | | */ |
| | | private String sheetName; |
| | | |
| | | /** |
| | | * 导åºç±»åï¼EXPORT:å¯¼åºæ°æ®ï¼IMPORTï¼å¯¼å
¥æ¨¡æ¿ï¼ |
| | | */ |
| | | private Type type; |
| | | |
| | | /** |
| | | * å·¥ä½è对象 |
| | | */ |
| | | private Workbook wb; |
| | | |
| | | /** |
| | | * å·¥ä½è¡¨å¯¹è±¡ |
| | | */ |
| | | private Sheet sheet; |
| | | |
| | | /** |
| | | * æ ·å¼å表 |
| | | */ |
| | | private Map<String, CellStyle> styles; |
| | | |
| | | /** |
| | | * 导å
¥å¯¼åºæ°æ®å表 |
| | | */ |
| | | private List<T> list; |
| | | |
| | | /** |
| | | * 注解å表 |
| | | */ |
| | | private List<Object[]> fields; |
| | | |
| | | /** |
| | | * å½åè¡å· |
| | | */ |
| | | private int rownum; |
| | | |
| | | /** |
| | | * æ é¢ |
| | | */ |
| | | private String title; |
| | | |
| | | /** |
| | | * æå¤§é«åº¦ |
| | | */ |
| | | private short maxHeight; |
| | | |
| | | /** |
| | | * åå¹¶åæåè¡æ° |
| | | */ |
| | | private int subMergedLastRowNum = 0; |
| | | |
| | | /** |
| | | * åå¹¶åå¼å§è¡æ° |
| | | */ |
| | | private int subMergedFirstRowNum = 1; |
| | | |
| | | /** |
| | | * 对象çååè¡¨æ¹æ³ |
| | | */ |
| | | private Method subMethod; |
| | | |
| | | /** |
| | | * 对象çååè¡¨å±æ§ |
| | | */ |
| | | private List<Field> subFields; |
| | | |
| | | /** |
| | | * ç»è®¡å表 |
| | | */ |
| | | private Map<Integer, Double> statistics = new HashMap<Integer, Double>(); |
| | | |
| | | /** |
| | | * å®ä½å¯¹è±¡ |
| | | */ |
| | | public Class<T> clazz; |
| | | |
| | | /** |
| | | * éè¦æ¾ç¤ºå屿§ |
| | | */ |
| | | public String[] includeFields; |
| | | |
| | | /** |
| | | * éè¦æé¤å屿§ |
| | | */ |
| | | public String[] excludeFields; |
| | | |
| | | public ExcelUtil(Class<T> clazz) |
| | | { |
| | | this.clazz = clazz; |
| | | } |
| | | |
| | | /** |
| | | * ä»
å¨Excel䏿¾ç¤ºå屿§ |
| | | * |
| | | * @param fields å屿§å 示ä¾[å个"name"/å¤ä¸ª"id","name"] |
| | | */ |
| | | public void showColumn(String... fields) |
| | | { |
| | | this.includeFields = fields; |
| | | } |
| | | |
| | | /** |
| | | * éèExcelä¸å屿§ |
| | | * |
| | | * @param fields å屿§å 示ä¾[å个"name"/å¤ä¸ª"id","name"] |
| | | */ |
| | | public void hideColumn(String... fields) |
| | | { |
| | | this.excludeFields = fields; |
| | | } |
| | | |
| | | public void init(List<T> list, String sheetName, String title, Type type) |
| | | { |
| | | if (list == null) |
| | | { |
| | | list = new ArrayList<T>(); |
| | | } |
| | | this.list = list; |
| | | this.sheetName = sheetName; |
| | | this.type = type; |
| | | this.title = title; |
| | | createExcelField(); |
| | | createWorkbook(); |
| | | createTitle(); |
| | | createSubHead(); |
| | | } |
| | | |
| | | /** |
| | | * å建excel第ä¸è¡æ é¢ |
| | | */ |
| | | public void createTitle() |
| | | { |
| | | if (StringUtils.isNotEmpty(title)) |
| | | { |
| | | int titleLastCol = this.fields.size() - 1; |
| | | if (isSubList()) |
| | | { |
| | | titleLastCol = titleLastCol + subFields.size() - 1; |
| | | } |
| | | Row titleRow = sheet.createRow(rownum == 0 ? rownum++ : 0); |
| | | titleRow.setHeightInPoints(30); |
| | | Cell titleCell = titleRow.createCell(0); |
| | | titleCell.setCellStyle(styles.get("title")); |
| | | titleCell.setCellValue(title); |
| | | sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), 0, titleLastCol)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å建对象çåå表åç§° |
| | | */ |
| | | public void createSubHead() |
| | | { |
| | | if (isSubList()) |
| | | { |
| | | Row subRow = sheet.createRow(rownum); |
| | | int column = 0; |
| | | int subFieldSize = subFields != null ? subFields.size() : 0; |
| | | for (Object[] objects : fields) |
| | | { |
| | | Field field = (Field) objects[0]; |
| | | Excel attr = (Excel) objects[1]; |
| | | if (Collection.class.isAssignableFrom(field.getType())) |
| | | { |
| | | Cell cell = subRow.createCell(column); |
| | | cell.setCellValue(attr.name()); |
| | | cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); |
| | | if (subFieldSize > 1) |
| | | { |
| | | CellRangeAddress cellAddress = new CellRangeAddress(rownum, rownum, column, column + subFieldSize - 1); |
| | | sheet.addMergedRegion(cellAddress); |
| | | } |
| | | column += subFieldSize; |
| | | } |
| | | else |
| | | { |
| | | Cell cell = subRow.createCell(column++); |
| | | cell.setCellValue(attr.name()); |
| | | cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); |
| | | } |
| | | } |
| | | rownum++; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 对excel表åé»è®¤ç¬¬ä¸ä¸ªç´¢å¼åè½¬æ¢ælist |
| | | * |
| | | * @param is è¾å
¥æµ |
| | | * @return 转æ¢åéå |
| | | */ |
| | | public List<T> importExcel(InputStream is) |
| | | { |
| | | return importExcel(is, 0); |
| | | } |
| | | |
| | | /** |
| | | * 对excel表åé»è®¤ç¬¬ä¸ä¸ªç´¢å¼åè½¬æ¢ælist |
| | | * |
| | | * @param is è¾å
¥æµ |
| | | * @param titleNum æ é¢å ç¨è¡æ° |
| | | * @return 转æ¢åéå |
| | | */ |
| | | public List<T> importExcel(InputStream is, int titleNum) |
| | | { |
| | | List<T> list = null; |
| | | try |
| | | { |
| | | list = importExcel(StringUtils.EMPTY, is, titleNum); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("导å
¥Excelå¼å¸¸{}", e.getMessage()); |
| | | throw new UtilException(e.getMessage()); |
| | | } |
| | | finally |
| | | { |
| | | IOUtils.closeQuietly(is); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 对excel表åæå®è¡¨æ ¼ç´¢å¼åè½¬æ¢ælist |
| | | * |
| | | * @param sheetName è¡¨æ ¼ç´¢å¼å |
| | | * @param titleNum æ é¢å ç¨è¡æ° |
| | | * @param is è¾å
¥æµ |
| | | * @return 转æ¢åéå |
| | | */ |
| | | public List<T> importExcel(String sheetName, InputStream is, int titleNum) throws Exception |
| | | { |
| | | this.type = Type.IMPORT; |
| | | this.wb = WorkbookFactory.create(is); |
| | | List<T> list = new ArrayList<T>(); |
| | | // 妿æå®sheetå,ååæå®sheetä¸çå
容 å¦åé»è®¤æå第1个sheet |
| | | Sheet sheet = StringUtils.isNotEmpty(sheetName) ? wb.getSheet(sheetName) : wb.getSheetAt(0); |
| | | if (sheet == null) |
| | | { |
| | | throw new IOException("æä»¶sheetä¸åå¨"); |
| | | } |
| | | |
| | | // è·åæåä¸ä¸ªé空è¡çè¡ä¸æ ï¼æ¯å¦æ»è¡æ°ä¸ºnï¼åè¿åç为n-1 |
| | | int rows = sheet.getLastRowNum(); |
| | | if (rows > 0) |
| | | { |
| | | // å®ä¹ä¸ä¸ªmapç¨äºåæ¾excelåçåºå·åfield. |
| | | Map<String, Integer> cellMap = new HashMap<String, Integer>(); |
| | | // è·å表头 |
| | | Row heard = sheet.getRow(titleNum); |
| | | for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) |
| | | { |
| | | Cell cell = heard.getCell(i); |
| | | if (StringUtils.isNotNull(cell)) |
| | | { |
| | | String value = this.getCellValue(heard, i).toString(); |
| | | cellMap.put(value, i); |
| | | } |
| | | else |
| | | { |
| | | cellMap.put(null, i); |
| | | } |
| | | } |
| | | // ææ°æ®æ¶æå¤ç å¾å°ç±»çææfield. |
| | | List<Object[]> fields = this.getFields(); |
| | | Map<Integer, Object[]> fieldsMap = new HashMap<Integer, Object[]>(); |
| | | for (Object[] objects : fields) |
| | | { |
| | | Excel attr = (Excel) objects[1]; |
| | | Integer column = cellMap.get(attr.name()); |
| | | if (column != null) |
| | | { |
| | | fieldsMap.put(column, objects); |
| | | } |
| | | } |
| | | for (int i = titleNum + 1; i <= rows; i++) |
| | | { |
| | | // ä»ç¬¬2è¡å¼å§åæ°æ®,é»è®¤ç¬¬ä¸è¡æ¯è¡¨å¤´. |
| | | Row row = sheet.getRow(i); |
| | | // 夿å½åè¡æ¯å¦æ¯ç©ºè¡ |
| | | if (isRowEmpty(row)) |
| | | { |
| | | continue; |
| | | } |
| | | T entity = null; |
| | | for (Map.Entry<Integer, Object[]> entry : fieldsMap.entrySet()) |
| | | { |
| | | Object val = this.getCellValue(row, entry.getKey()); |
| | | |
| | | // 妿ä¸åå¨å®ä¾åæ°å»º. |
| | | entity = (entity == null ? clazz.newInstance() : entity); |
| | | // ä»mapä¸å¾å°å¯¹åºåçfield. |
| | | Field field = (Field) entry.getValue()[0]; |
| | | Excel attr = (Excel) entry.getValue()[1]; |
| | | // åå¾ç±»å,å¹¶æ ¹æ®å¯¹è±¡ç±»å设置å¼. |
| | | Class<?> fieldType = field.getType(); |
| | | if (String.class == fieldType) |
| | | { |
| | | String s = Convert.toStr(val); |
| | | if (s.matches("^\\d+\\.0$")) |
| | | { |
| | | val = StringUtils.substringBefore(s, ".0"); |
| | | } |
| | | else |
| | | { |
| | | String dateFormat = field.getAnnotation(Excel.class).dateFormat(); |
| | | if (StringUtils.isNotEmpty(dateFormat)) |
| | | { |
| | | val = parseDateToStr(dateFormat, val); |
| | | } |
| | | else |
| | | { |
| | | val = Convert.toStr(val); |
| | | } |
| | | } |
| | | } |
| | | else if ((Integer.TYPE == fieldType || Integer.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val))) |
| | | { |
| | | val = Convert.toInt(val); |
| | | } |
| | | else if ((Long.TYPE == fieldType || Long.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val))) |
| | | { |
| | | val = Convert.toLong(val); |
| | | } |
| | | else if (Double.TYPE == fieldType || Double.class == fieldType) |
| | | { |
| | | val = Convert.toDouble(val); |
| | | } |
| | | else if (Float.TYPE == fieldType || Float.class == fieldType) |
| | | { |
| | | val = Convert.toFloat(val); |
| | | } |
| | | else if (BigDecimal.class == fieldType) |
| | | { |
| | | val = Convert.toBigDecimal(val); |
| | | } |
| | | else if (Date.class == fieldType) |
| | | { |
| | | if (val instanceof String) |
| | | { |
| | | val = DateUtils.parseDate(val); |
| | | } |
| | | else if (val instanceof Double) |
| | | { |
| | | val = DateUtil.getJavaDate((Double) val); |
| | | } |
| | | } |
| | | else if (Boolean.TYPE == fieldType || Boolean.class == fieldType) |
| | | { |
| | | val = Convert.toBool(val, false); |
| | | } |
| | | if (StringUtils.isNotNull(fieldType)) |
| | | { |
| | | String propertyName = field.getName(); |
| | | if (StringUtils.isNotEmpty(attr.targetAttr())) |
| | | { |
| | | propertyName = field.getName() + "." + attr.targetAttr(); |
| | | } |
| | | if (StringUtils.isNotEmpty(attr.readConverterExp())) |
| | | { |
| | | val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator()); |
| | | } |
| | | else if (!attr.handler().equals(ExcelHandlerAdapter.class)) |
| | | { |
| | | val = dataFormatHandlerAdapter(val, attr, null); |
| | | } |
| | | ReflectUtils.invokeSetter(entity, propertyName, val); |
| | | } |
| | | } |
| | | list.add(entity); |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 对listæ°æ®æºå°å
¶éé¢çæ°æ®å¯¼å
¥å°excel表å |
| | | * |
| | | * @param response è¿åæ°æ® |
| | | * @param list å¯¼åºæ°æ®éå |
| | | * @param sheetName å·¥ä½è¡¨çåç§° |
| | | * @return ç»æ |
| | | */ |
| | | public void exportExcel(HttpServletResponse response, List<T> list, String sheetName) |
| | | { |
| | | exportExcel(response, list, sheetName, StringUtils.EMPTY); |
| | | } |
| | | |
| | | /** |
| | | * 对listæ°æ®æºå°å
¶éé¢çæ°æ®å¯¼å
¥å°excel表å |
| | | * |
| | | * @param response è¿åæ°æ® |
| | | * @param list å¯¼åºæ°æ®éå |
| | | * @param sheetName å·¥ä½è¡¨çåç§° |
| | | * @param title æ é¢ |
| | | * @return ç»æ |
| | | */ |
| | | public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title) |
| | | { |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | this.init(list, sheetName, title, Type.EXPORT); |
| | | exportExcel(response); |
| | | } |
| | | |
| | | /** |
| | | * 对listæ°æ®æºå°å
¶éé¢çæ°æ®å¯¼å
¥å°excel表å |
| | | * |
| | | * @param sheetName å·¥ä½è¡¨çåç§° |
| | | * @return ç»æ |
| | | */ |
| | | public void importTemplateExcel(HttpServletResponse response, String sheetName) |
| | | { |
| | | importTemplateExcel(response, sheetName, StringUtils.EMPTY); |
| | | } |
| | | |
| | | /** |
| | | * 对listæ°æ®æºå°å
¶éé¢çæ°æ®å¯¼å
¥å°excel表å |
| | | * |
| | | * @param sheetName å·¥ä½è¡¨çåç§° |
| | | * @param title æ é¢ |
| | | * @return ç»æ |
| | | */ |
| | | public void importTemplateExcel(HttpServletResponse response, String sheetName, String title) |
| | | { |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | this.init(null, sheetName, title, Type.IMPORT); |
| | | exportExcel(response); |
| | | } |
| | | |
| | | /** |
| | | * 对listæ°æ®æºå°å
¶éé¢çæ°æ®å¯¼å
¥å°excel表å |
| | | * |
| | | * @return ç»æ |
| | | */ |
| | | public void exportExcel(HttpServletResponse response) |
| | | { |
| | | try |
| | | { |
| | | writeSheet(); |
| | | wb.write(response.getOutputStream()); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("导åºExcelå¼å¸¸{}", e.getMessage()); |
| | | } |
| | | finally |
| | | { |
| | | IOUtils.closeQuietly(wb); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å建åå
¥æ°æ®å°Sheet |
| | | */ |
| | | public void writeSheet() |
| | | { |
| | | // ååºä¸å
±æå¤å°ä¸ªsheet. |
| | | int sheetNo = Math.max(1, (int) Math.ceil(list.size() * 1.0 / sheetSize)); |
| | | for (int index = 0; index < sheetNo; index++) |
| | | { |
| | | createSheet(sheetNo, index); |
| | | |
| | | // 产çä¸è¡ |
| | | Row row = sheet.createRow(rownum); |
| | | int column = 0; |
| | | // åå
¥åä¸ªåæ®µçå头åç§° |
| | | for (Object[] os : fields) |
| | | { |
| | | Field field = (Field) os[0]; |
| | | Excel excel = (Excel) os[1]; |
| | | if (Collection.class.isAssignableFrom(field.getType())) |
| | | { |
| | | for (Field subField : subFields) |
| | | { |
| | | Excel subExcel = subField.getAnnotation(Excel.class); |
| | | this.createHeadCell(subExcel, row, column++); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | this.createHeadCell(excel, row, column++); |
| | | } |
| | | } |
| | | if (Type.EXPORT.equals(type)) |
| | | { |
| | | fillExcelData(index, row); |
| | | addStatisticsRow(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å¡«å
excelæ°æ® |
| | | * |
| | | * @param index åºå· |
| | | * @param row åå
æ ¼è¡ |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public void fillExcelData(int index, Row row) |
| | | { |
| | | int startNo = index * sheetSize; |
| | | int endNo = Math.min(startNo + sheetSize, list.size()); |
| | | int currentRowNum = rownum + 1; // 仿 é¢è¡åå¼å§ |
| | | |
| | | for (int i = startNo; i < endNo; i++) |
| | | { |
| | | row = sheet.createRow(currentRowNum); |
| | | T vo = (T) list.get(i); |
| | | int column = 0; |
| | | int maxSubListSize = getCurrentMaxSubListSize(vo); |
| | | for (Object[] os : fields) |
| | | { |
| | | Field field = (Field) os[0]; |
| | | Excel excel = (Excel) os[1]; |
| | | if (Collection.class.isAssignableFrom(field.getType())) |
| | | { |
| | | try |
| | | { |
| | | Collection<?> subList = (Collection<?>) getTargetValue(vo, field, excel); |
| | | if (subList != null && !subList.isEmpty()) |
| | | { |
| | | int subIndex = 0; |
| | | for (Object subVo : subList) |
| | | { |
| | | Row subRow = sheet.getRow(currentRowNum + subIndex); |
| | | if (subRow == null) |
| | | { |
| | | subRow = sheet.createRow(currentRowNum + subIndex); |
| | | } |
| | | |
| | | int subColumn = column; |
| | | for (Field subField : subFields) |
| | | { |
| | | Excel subExcel = subField.getAnnotation(Excel.class); |
| | | addCell(subExcel, subRow, (T) subVo, subField, subColumn++); |
| | | } |
| | | subIndex++; |
| | | } |
| | | column += subFields.size(); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("å¡«å
éåæ°æ®å¤±è´¥", e); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | // å建åå
æ ¼å¹¶è®¾ç½®å¼ |
| | | addCell(excel, row, vo, field, column); |
| | | if (maxSubListSize > 1 && excel.needMerge()) |
| | | { |
| | | sheet.addMergedRegion(new CellRangeAddress(currentRowNum, currentRowNum + maxSubListSize - 1, column, column)); |
| | | } |
| | | column++; |
| | | } |
| | | } |
| | | currentRowNum += maxSubListSize; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·ååå表æå¤§æ° |
| | | */ |
| | | private int getCurrentMaxSubListSize(T vo) |
| | | { |
| | | int maxSubListSize = 1; |
| | | for (Object[] os : fields) |
| | | { |
| | | Field field = (Field) os[0]; |
| | | if (Collection.class.isAssignableFrom(field.getType())) |
| | | { |
| | | try |
| | | { |
| | | Collection<?> subList = (Collection<?>) getTargetValue(vo, field, (Excel) os[1]); |
| | | if (subList != null && !subList.isEmpty()) |
| | | { |
| | | maxSubListSize = Math.max(maxSubListSize, subList.size()); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("è·åéå大å°å¤±è´¥", e); |
| | | } |
| | | } |
| | | } |
| | | return maxSubListSize; |
| | | } |
| | | |
| | | /** |
| | | * åå»ºè¡¨æ ¼æ ·å¼ |
| | | * |
| | | * @param wb å·¥ä½è对象 |
| | | * @return æ ·å¼å表 |
| | | */ |
| | | private Map<String, CellStyle> createStyles(Workbook wb) |
| | | { |
| | | // åå
¥åæ¡è®°å½,æ¯æ¡è®°å½å¯¹åºexcel表ä¸çä¸è¡ |
| | | Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); |
| | | CellStyle style = wb.createCellStyle(); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | Font titleFont = wb.createFont(); |
| | | titleFont.setFontName("Arial"); |
| | | titleFont.setFontHeightInPoints((short) 16); |
| | | titleFont.setBold(true); |
| | | style.setFont(titleFont); |
| | | DataFormat dataFormat = wb.createDataFormat(); |
| | | style.setDataFormat(dataFormat.getFormat("@")); |
| | | styles.put("title", style); |
| | | |
| | | style = wb.createCellStyle(); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | style.setBorderRight(BorderStyle.THIN); |
| | | style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | style.setBorderLeft(BorderStyle.THIN); |
| | | style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | style.setBorderTop(BorderStyle.THIN); |
| | | style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | style.setBorderBottom(BorderStyle.THIN); |
| | | style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | Font dataFont = wb.createFont(); |
| | | dataFont.setFontName("Arial"); |
| | | dataFont.setFontHeightInPoints((short) 10); |
| | | style.setFont(dataFont); |
| | | styles.put("data", style); |
| | | |
| | | style = wb.createCellStyle(); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | style.setDataFormat(dataFormat.getFormat("######0.00")); |
| | | Font totalFont = wb.createFont(); |
| | | totalFont.setFontName("Arial"); |
| | | totalFont.setFontHeightInPoints((short) 10); |
| | | style.setFont(totalFont); |
| | | styles.put("total", style); |
| | | |
| | | styles.putAll(annotationHeaderStyles(wb, styles)); |
| | | |
| | | styles.putAll(annotationDataStyles(wb)); |
| | | |
| | | return styles; |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®Excel注解åå»ºè¡¨æ ¼å¤´æ ·å¼ |
| | | * |
| | | * @param wb å·¥ä½è对象 |
| | | * @return èªå®ä¹æ ·å¼å表 |
| | | */ |
| | | private Map<String, CellStyle> annotationHeaderStyles(Workbook wb, Map<String, CellStyle> styles) |
| | | { |
| | | Map<String, CellStyle> headerStyles = new HashMap<String, CellStyle>(); |
| | | for (Object[] os : fields) |
| | | { |
| | | Excel excel = (Excel) os[1]; |
| | | String key = StringUtils.format("header_{}_{}", excel.headerColor(), excel.headerBackgroundColor()); |
| | | if (!headerStyles.containsKey(key)) |
| | | { |
| | | CellStyle style = wb.createCellStyle(); |
| | | style.cloneStyleFrom(styles.get("data")); |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | style.setFillForegroundColor(excel.headerBackgroundColor().index); |
| | | style.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
| | | Font headerFont = wb.createFont(); |
| | | headerFont.setFontName("Arial"); |
| | | headerFont.setFontHeightInPoints((short) 10); |
| | | headerFont.setBold(true); |
| | | headerFont.setColor(excel.headerColor().index); |
| | | style.setFont(headerFont); |
| | | // è®¾ç½®è¡¨æ ¼å¤´åå
æ ¼ææ¬å½¢å¼ |
| | | DataFormat dataFormat = wb.createDataFormat(); |
| | | style.setDataFormat(dataFormat.getFormat("@")); |
| | | headerStyles.put(key, style); |
| | | } |
| | | } |
| | | return headerStyles; |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®Excel注解åå»ºè¡¨æ ¼åæ ·å¼ |
| | | * |
| | | * @param wb å·¥ä½è对象 |
| | | * @return èªå®ä¹æ ·å¼å表 |
| | | */ |
| | | private Map<String, CellStyle> annotationDataStyles(Workbook wb) |
| | | { |
| | | Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); |
| | | for (Object[] os : fields) |
| | | { |
| | | Field field = (Field) os[0]; |
| | | Excel excel = (Excel) os[1]; |
| | | if (Collection.class.isAssignableFrom(field.getType())) |
| | | { |
| | | ParameterizedType pt = (ParameterizedType) field.getGenericType(); |
| | | Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0]; |
| | | List<Field> subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class); |
| | | for (Field subField : subFields) |
| | | { |
| | | Excel subExcel = subField.getAnnotation(Excel.class); |
| | | annotationDataStyles(styles, subField, subExcel); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | annotationDataStyles(styles, field, excel); |
| | | } |
| | | } |
| | | return styles; |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®Excel注解åå»ºè¡¨æ ¼åæ ·å¼ |
| | | * |
| | | * @param styles èªå®ä¹æ ·å¼å表 |
| | | * @param field 屿§åä¿¡æ¯ |
| | | * @param excel æ³¨è§£ä¿¡æ¯ |
| | | */ |
| | | public void annotationDataStyles(Map<String, CellStyle> styles, Field field, Excel excel) |
| | | { |
| | | String key = StringUtils.format("data_{}_{}_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor(), excel.cellType(), excel.wrapText()); |
| | | if (!styles.containsKey(key)) |
| | | { |
| | | CellStyle style = wb.createCellStyle(); |
| | | style.setAlignment(excel.align()); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | style.setBorderRight(BorderStyle.THIN); |
| | | style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | style.setBorderLeft(BorderStyle.THIN); |
| | | style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | style.setBorderTop(BorderStyle.THIN); |
| | | style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | style.setBorderBottom(BorderStyle.THIN); |
| | | style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | style.setFillPattern(FillPatternType.SOLID_FOREGROUND); |
| | | style.setFillForegroundColor(excel.backgroundColor().getIndex()); |
| | | style.setWrapText(excel.wrapText()); |
| | | Font dataFont = wb.createFont(); |
| | | dataFont.setFontName("Arial"); |
| | | dataFont.setFontHeightInPoints((short) 10); |
| | | dataFont.setColor(excel.color().index); |
| | | style.setFont(dataFont); |
| | | if (ColumnType.TEXT == excel.cellType()) |
| | | { |
| | | DataFormat dataFormat = wb.createDataFormat(); |
| | | style.setDataFormat(dataFormat.getFormat("@")); |
| | | } |
| | | styles.put(key, style); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å建åå
æ ¼ |
| | | */ |
| | | public Cell createHeadCell(Excel attr, Row row, int column) |
| | | { |
| | | // å建å |
| | | Cell cell = row.createCell(column); |
| | | // åå
¥åä¿¡æ¯ |
| | | cell.setCellValue(attr.name()); |
| | | setDataValidation(attr, row, column); |
| | | cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); |
| | | if (isSubList()) |
| | | { |
| | | // å¡«å
é»è®¤æ ·å¼ï¼é²æ¢åå¹¶åå
æ ¼æ ·å¼å¤±æ |
| | | sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType(), attr.wrapText()))); |
| | | if (attr.needMerge()) |
| | | { |
| | | sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column)); |
| | | } |
| | | } |
| | | return cell; |
| | | } |
| | | |
| | | /** |
| | | * 设置åå
æ ¼ä¿¡æ¯ |
| | | * |
| | | * @param value åå
æ ¼å¼ |
| | | * @param attr 注解ç¸å
³ |
| | | * @param cell åå
æ ¼ä¿¡æ¯ |
| | | */ |
| | | public void setCellVo(Object value, Excel attr, Cell cell) |
| | | { |
| | | if (ColumnType.STRING == attr.cellType() || ColumnType.TEXT == attr.cellType()) |
| | | { |
| | | String cellValue = Convert.toStr(value); |
| | | // 对äºä»»ä½ä»¥è¡¨è¾¾å¼è§¦åå符 =-+@å¼å¤´çåå
æ ¼ï¼ç´æ¥ä½¿ç¨tabå符ä½ä¸ºåç¼ï¼é²æ¢CSV注å
¥ã |
| | | if (StringUtils.startsWithAny(cellValue, FORMULA_STR)) |
| | | { |
| | | cellValue = RegExUtils.replaceFirst(cellValue, FORMULA_REGEX_STR, "\t$0"); |
| | | } |
| | | if (value instanceof Collection && StringUtils.equals("[]", cellValue)) |
| | | { |
| | | cellValue = StringUtils.EMPTY; |
| | | } |
| | | cell.setCellValue(StringUtils.isNull(cellValue) ? attr.defaultValue() : cellValue + attr.suffix()); |
| | | } |
| | | else if (ColumnType.NUMERIC == attr.cellType()) |
| | | { |
| | | if (StringUtils.isNotNull(value)) |
| | | { |
| | | cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value)); |
| | | } |
| | | } |
| | | else if (ColumnType.IMAGE == attr.cellType()) |
| | | { |
| | | ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1); |
| | | String imagePath = Convert.toStr(value); |
| | | if (StringUtils.isNotEmpty(imagePath)) |
| | | { |
| | | byte[] data = ImageUtils.getImage(imagePath); |
| | | getDrawingPatriarch(cell.getSheet()).createPicture(anchor, |
| | | cell.getSheet().getWorkbook().addPicture(data, getImageType(data))); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åç»å¸ |
| | | */ |
| | | public static Drawing<?> getDrawingPatriarch(Sheet sheet) |
| | | { |
| | | if (sheet.getDrawingPatriarch() == null) |
| | | { |
| | | sheet.createDrawingPatriarch(); |
| | | } |
| | | return sheet.getDrawingPatriarch(); |
| | | } |
| | | |
| | | /** |
| | | * è·åå¾çç±»å,设置å¾çæå
¥ç±»å |
| | | */ |
| | | public int getImageType(byte[] value) |
| | | { |
| | | String type = FileTypeUtils.getFileExtendName(value); |
| | | if ("JPG".equalsIgnoreCase(type)) |
| | | { |
| | | return Workbook.PICTURE_TYPE_JPEG; |
| | | } |
| | | else if ("PNG".equalsIgnoreCase(type)) |
| | | { |
| | | return Workbook.PICTURE_TYPE_PNG; |
| | | } |
| | | return Workbook.PICTURE_TYPE_JPEG; |
| | | } |
| | | |
| | | /** |
| | | * åå»ºè¡¨æ ¼æ ·å¼ |
| | | */ |
| | | public void setDataValidation(Excel attr, Row row, int column) |
| | | { |
| | | if (attr.name().indexOf("注ï¼") >= 0) |
| | | { |
| | | sheet.setColumnWidth(column, 6000); |
| | | } |
| | | else |
| | | { |
| | | // 设置å宽 |
| | | sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256)); |
| | | } |
| | | if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0) |
| | | { |
| | | if (attr.combo().length > 15 || StringUtils.join(attr.combo()).length() > 255) |
| | | { |
| | | // 妿䏿æ°å¤§äº15æå符串é¿åº¦å¤§äº255ï¼å使ç¨ä¸ä¸ªæ°sheetåå¨ï¼é¿å
çæç模æ¿ä¸æå¼è·åä¸å° |
| | | setXSSFValidationWithHidden(sheet, attr.combo(), attr.prompt(), 1, 100, column, column); |
| | | } |
| | | else |
| | | { |
| | | // æç¤ºä¿¡æ¯æåªè½éæ©ä¸è½è¾å
¥çåå
容. |
| | | setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ·»å åå
æ ¼ |
| | | */ |
| | | public Cell addCell(Excel attr, Row row, T vo, Field field, int column) |
| | | { |
| | | Cell cell = null; |
| | | try |
| | | { |
| | | // 设置è¡é« |
| | | row.setHeight(maxHeight); |
| | | // æ ¹æ®Excelä¸è®¾ç½®æ
åµå³å®æ¯å¦å¯¼åº,æäºæ
åµéè¦ä¿æä¸ºç©º,å¸æç¨æ·å¡«åè¿ä¸å. |
| | | if (attr.isExport()) |
| | | { |
| | | // å建cell |
| | | cell = row.createCell(column); |
| | | if (isSubListValue(vo) && getListCellValue(vo).size() > 1 && attr.needMerge()) |
| | | { |
| | | if (subMergedLastRowNum >= subMergedFirstRowNum) |
| | | { |
| | | sheet.addMergedRegion(new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column)); |
| | | } |
| | | } |
| | | cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType(), attr.wrapText()))); |
| | | |
| | | // ç¨äºè¯»å对象ä¸ç屿§ |
| | | Object value = getTargetValue(vo, field, attr); |
| | | String dateFormat = attr.dateFormat(); |
| | | String readConverterExp = attr.readConverterExp(); |
| | | String separator = attr.separator(); |
| | | if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) |
| | | { |
| | | cell.getCellStyle().setDataFormat(this.wb.getCreationHelper().createDataFormat().getFormat(dateFormat)); |
| | | cell.setCellValue(parseDateToStr(dateFormat, value)); |
| | | } |
| | | else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) |
| | | { |
| | | cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator)); |
| | | } |
| | | else if (value instanceof BigDecimal && -1 != attr.scale()) |
| | | { |
| | | cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).doubleValue()); |
| | | } |
| | | else if (!attr.handler().equals(ExcelHandlerAdapter.class)) |
| | | { |
| | | cell.setCellValue(dataFormatHandlerAdapter(value, attr, cell)); |
| | | } |
| | | else |
| | | { |
| | | // 设置åç±»å |
| | | setCellVo(value, attr, cell); |
| | | } |
| | | addStatisticsData(column, Convert.toStr(value), attr); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("导åºExcel失败{}", e); |
| | | } |
| | | return cell; |
| | | } |
| | | |
| | | /** |
| | | * 设置 POI XSSFSheet åå
æ ¼æç¤ºæéæ©æ¡ |
| | | * |
| | | * @param sheet 表å |
| | | * @param textlist ä¸ææ¡æ¾ç¤ºçå
容 |
| | | * @param promptContent æç¤ºå
容 |
| | | * @param firstRow å¼å§è¡ |
| | | * @param endRow ç»æè¡ |
| | | * @param firstCol å¼å§å |
| | | * @param endCol ç»æå |
| | | */ |
| | | public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow, |
| | | int firstCol, int endCol) |
| | | { |
| | | DataValidationHelper helper = sheet.getDataValidationHelper(); |
| | | DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1"); |
| | | CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); |
| | | DataValidation dataValidation = helper.createValidation(constraint, regions); |
| | | if (StringUtils.isNotEmpty(promptContent)) |
| | | { |
| | | // å¦æè®¾ç½®äºæç¤ºä¿¡æ¯åé¼ æ æ¾ä¸å»æç¤º |
| | | dataValidation.createPromptBox("", promptContent); |
| | | dataValidation.setShowPromptBox(true); |
| | | } |
| | | // å¤çExcelå
¼å®¹æ§é®é¢ |
| | | if (dataValidation instanceof XSSFDataValidation) |
| | | { |
| | | dataValidation.setSuppressDropDownArrow(true); |
| | | dataValidation.setShowErrorBox(true); |
| | | } |
| | | else |
| | | { |
| | | dataValidation.setSuppressDropDownArrow(false); |
| | | } |
| | | sheet.addValidationData(dataValidation); |
| | | } |
| | | |
| | | /** |
| | | * 设置æäºåçå¼åªè½è¾å
¥é¢å¶çæ°æ®,æ¾ç¤ºä¸ææ¡ï¼å
¼å®¹è¶
åºä¸å®æ°éç䏿æ¡ï¼. |
| | | * |
| | | * @param sheet è¦è®¾ç½®çsheet. |
| | | * @param textlist ä¸ææ¡æ¾ç¤ºçå
容 |
| | | * @param promptContent æç¤ºå
容 |
| | | * @param firstRow å¼å§è¡ |
| | | * @param endRow ç»æè¡ |
| | | * @param firstCol å¼å§å |
| | | * @param endCol ç»æå |
| | | */ |
| | | public void setXSSFValidationWithHidden(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow, int firstCol, int endCol) |
| | | { |
| | | String hideSheetName = "combo_" + firstCol + "_" + endCol; |
| | | Sheet hideSheet = wb.createSheet(hideSheetName); // ç¨äºåå¨ ä¸æèåæ°æ® |
| | | for (int i = 0; i < textlist.length; i++) |
| | | { |
| | | hideSheet.createRow(i).createCell(0).setCellValue(textlist[i]); |
| | | } |
| | | // å建åç§°ï¼å¯è¢«å
¶ä»åå
æ ¼å¼ç¨ |
| | | Name name = wb.createName(); |
| | | name.setNameName(hideSheetName + "_data"); |
| | | name.setRefersToFormula(hideSheetName + "!$A$1:$A$" + textlist.length); |
| | | DataValidationHelper helper = sheet.getDataValidationHelper(); |
| | | // å è½½ä¸æå表å
容 |
| | | DataValidationConstraint constraint = helper.createFormulaListConstraint(hideSheetName + "_data"); |
| | | // è®¾ç½®æ°æ®æææ§å è½½å¨åªä¸ªåå
æ ¼ä¸,åä¸ªåæ°å嫿¯ï¼èµ·å§è¡ãç»æ¢è¡ãèµ·å§åãç»æ¢å |
| | | CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); |
| | | // æ°æ®æææ§å¯¹è±¡ |
| | | DataValidation dataValidation = helper.createValidation(constraint, regions); |
| | | if (StringUtils.isNotEmpty(promptContent)) |
| | | { |
| | | // å¦æè®¾ç½®äºæç¤ºä¿¡æ¯åé¼ æ æ¾ä¸å»æç¤º |
| | | dataValidation.createPromptBox("", promptContent); |
| | | dataValidation.setShowPromptBox(true); |
| | | } |
| | | // å¤çExcelå
¼å®¹æ§é®é¢ |
| | | if (dataValidation instanceof XSSFDataValidation) |
| | | { |
| | | dataValidation.setSuppressDropDownArrow(true); |
| | | dataValidation.setShowErrorBox(true); |
| | | } |
| | | else |
| | | { |
| | | dataValidation.setSuppressDropDownArrow(false); |
| | | } |
| | | |
| | | sheet.addValidationData(dataValidation); |
| | | // 设置hiddenSheetéè |
| | | wb.setSheetHidden(wb.getSheetIndex(hideSheet), true); |
| | | } |
| | | |
| | | /** |
| | | * è§£æå¯¼åºå¼ 0=ç·,1=女,2=æªç¥ |
| | | * |
| | | * @param propertyValue åæ°å¼ |
| | | * @param converterExp ç¿»è¯æ³¨è§£ |
| | | * @param separator åé符 |
| | | * @return è§£æåå¼ |
| | | */ |
| | | public static String convertByExp(String propertyValue, String converterExp, String separator) |
| | | { |
| | | StringBuilder propertyString = new StringBuilder(); |
| | | String[] convertSource = converterExp.split(SEPARATOR); |
| | | for (String item : convertSource) |
| | | { |
| | | String[] itemArray = item.split("="); |
| | | if (StringUtils.containsAny(propertyValue, separator)) |
| | | { |
| | | for (String value : propertyValue.split(separator)) |
| | | { |
| | | if (itemArray[0].equals(value)) |
| | | { |
| | | propertyString.append(itemArray[1] + separator); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | else |
| | | { |
| | | if (itemArray[0].equals(propertyValue)) |
| | | { |
| | | return itemArray[1]; |
| | | } |
| | | } |
| | | } |
| | | return StringUtils.stripEnd(propertyString.toString(), separator); |
| | | } |
| | | |
| | | /** |
| | | * ååè§£æå¼ ç·=0,女=1,æªç¥=2 |
| | | * |
| | | * @param propertyValue åæ°å¼ |
| | | * @param converterExp ç¿»è¯æ³¨è§£ |
| | | * @param separator åé符 |
| | | * @return è§£æåå¼ |
| | | */ |
| | | public static String reverseByExp(String propertyValue, String converterExp, String separator) |
| | | { |
| | | StringBuilder propertyString = new StringBuilder(); |
| | | String[] convertSource = converterExp.split(SEPARATOR); |
| | | for (String item : convertSource) |
| | | { |
| | | String[] itemArray = item.split("="); |
| | | if (StringUtils.containsAny(propertyValue, separator)) |
| | | { |
| | | for (String value : propertyValue.split(separator)) |
| | | { |
| | | if (itemArray[1].equals(value)) |
| | | { |
| | | propertyString.append(itemArray[0] + separator); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | else |
| | | { |
| | | if (itemArray[1].equals(propertyValue)) |
| | | { |
| | | return itemArray[0]; |
| | | } |
| | | } |
| | | } |
| | | return StringUtils.stripEnd(propertyString.toString(), separator); |
| | | } |
| | | |
| | | /** |
| | | * æ°æ®å¤çå¨ |
| | | * |
| | | * @param value æ°æ®å¼ |
| | | * @param excel æ°æ®æ³¨è§£ |
| | | * @return |
| | | */ |
| | | public String dataFormatHandlerAdapter(Object value, Excel excel, Cell cell) |
| | | { |
| | | try |
| | | { |
| | | Object instance = excel.handler().newInstance(); |
| | | Method formatMethod = excel.handler().getMethod("format", new Class[] { Object.class, String[].class, Cell.class, Workbook.class }); |
| | | value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("ä¸è½æ ¼å¼åæ°æ® " + excel.handler(), e.getMessage()); |
| | | } |
| | | return Convert.toStr(value); |
| | | } |
| | | |
| | | /** |
| | | * å计ç»è®¡ä¿¡æ¯ |
| | | */ |
| | | private void addStatisticsData(Integer index, String text, Excel entity) |
| | | { |
| | | if (entity != null && entity.isStatistics()) |
| | | { |
| | | Double temp = 0D; |
| | | if (!statistics.containsKey(index)) |
| | | { |
| | | statistics.put(index, temp); |
| | | } |
| | | try |
| | | { |
| | | temp = Double.valueOf(text); |
| | | } |
| | | catch (NumberFormatException e) |
| | | { |
| | | } |
| | | statistics.put(index, statistics.get(index) + temp); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å建ç»è®¡è¡ |
| | | */ |
| | | public void addStatisticsRow() |
| | | { |
| | | if (statistics.size() > 0) |
| | | { |
| | | Row row = sheet.createRow(sheet.getLastRowNum() + 1); |
| | | Set<Integer> keys = statistics.keySet(); |
| | | Cell cell = row.createCell(0); |
| | | cell.setCellStyle(styles.get("total")); |
| | | cell.setCellValue("å计"); |
| | | |
| | | for (Integer key : keys) |
| | | { |
| | | cell = row.createCell(key); |
| | | cell.setCellStyle(styles.get("total")); |
| | | cell.setCellValue(statistics.get(key)); |
| | | } |
| | | statistics.clear(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åbeanä¸ç屿§å¼ |
| | | * |
| | | * @param vo å®ä½å¯¹è±¡ |
| | | * @param field åæ®µ |
| | | * @param excel 注解 |
| | | * @return æç»ç屿§å¼ |
| | | * @throws Exception |
| | | */ |
| | | private Object getTargetValue(T vo, Field field, Excel excel) throws Exception |
| | | { |
| | | field.setAccessible(true); |
| | | Object o = field.get(vo); |
| | | if (StringUtils.isNotEmpty(excel.targetAttr())) |
| | | { |
| | | String target = excel.targetAttr(); |
| | | if (target.contains(".")) |
| | | { |
| | | String[] targets = target.split("[.]"); |
| | | for (String name : targets) |
| | | { |
| | | o = getValue(o, name); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | o = getValue(o, target); |
| | | } |
| | | } |
| | | return o; |
| | | } |
| | | |
| | | /** |
| | | * 以类ç屿§çgetæ¹æ³æ¹æ³å½¢å¼è·åå¼ |
| | | * |
| | | * @param o |
| | | * @param name |
| | | * @return value |
| | | * @throws Exception |
| | | */ |
| | | private Object getValue(Object o, String name) throws Exception |
| | | { |
| | | if (StringUtils.isNotNull(o) && StringUtils.isNotEmpty(name)) |
| | | { |
| | | Class<?> clazz = o.getClass(); |
| | | Field field = clazz.getDeclaredField(name); |
| | | field.setAccessible(true); |
| | | o = field.get(o); |
| | | } |
| | | return o; |
| | | } |
| | | |
| | | /** |
| | | * å¾å°ææå®ä¹å段 |
| | | */ |
| | | private void createExcelField() |
| | | { |
| | | this.fields = getFields(); |
| | | this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList()); |
| | | this.maxHeight = getRowHeight(); |
| | | } |
| | | |
| | | /** |
| | | * è·ååæ®µæ³¨è§£ä¿¡æ¯ |
| | | */ |
| | | public List<Object[]> getFields() |
| | | { |
| | | List<Object[]> fields = new ArrayList<Object[]>(); |
| | | List<Field> tempFields = new ArrayList<>(); |
| | | tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields())); |
| | | tempFields.addAll(Arrays.asList(clazz.getDeclaredFields())); |
| | | if (StringUtils.isNotEmpty(includeFields)) |
| | | { |
| | | for (Field field : tempFields) |
| | | { |
| | | if (ArrayUtils.contains(this.includeFields, field.getName()) || field.isAnnotationPresent(Excels.class)) |
| | | { |
| | | addField(fields, field); |
| | | } |
| | | } |
| | | } |
| | | else if (StringUtils.isNotEmpty(excludeFields)) |
| | | { |
| | | for (Field field : tempFields) |
| | | { |
| | | if (!ArrayUtils.contains(this.excludeFields, field.getName())) |
| | | { |
| | | addField(fields, field); |
| | | } |
| | | } |
| | | } |
| | | else |
| | | { |
| | | for (Field field : tempFields) |
| | | { |
| | | addField(fields, field); |
| | | } |
| | | } |
| | | return fields; |
| | | } |
| | | |
| | | /** |
| | | * æ·»å åæ®µä¿¡æ¯ |
| | | */ |
| | | public void addField(List<Object[]> fields, Field field) |
| | | { |
| | | // åæ³¨è§£ |
| | | if (field.isAnnotationPresent(Excel.class)) |
| | | { |
| | | Excel attr = field.getAnnotation(Excel.class); |
| | | if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) |
| | | { |
| | | fields.add(new Object[] { field, attr }); |
| | | } |
| | | if (Collection.class.isAssignableFrom(field.getType())) |
| | | { |
| | | subMethod = getSubMethod(field.getName(), clazz); |
| | | ParameterizedType pt = (ParameterizedType) field.getGenericType(); |
| | | Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0]; |
| | | this.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class); |
| | | } |
| | | } |
| | | |
| | | // 夿³¨è§£ |
| | | if (field.isAnnotationPresent(Excels.class)) |
| | | { |
| | | Excels attrs = field.getAnnotation(Excels.class); |
| | | Excel[] excels = attrs.value(); |
| | | for (Excel attr : excels) |
| | | { |
| | | if (StringUtils.isNotEmpty(includeFields)) |
| | | { |
| | | if (ArrayUtils.contains(this.includeFields, field.getName() + "." + attr.targetAttr()) |
| | | && (attr != null && (attr.type() == Type.ALL || attr.type() == type))) |
| | | { |
| | | fields.add(new Object[] { field, attr }); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr()) |
| | | && (attr != null && (attr.type() == Type.ALL || attr.type() == type))) |
| | | { |
| | | fields.add(new Object[] { field, attr }); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®æ³¨è§£è·åæå¤§è¡é« |
| | | */ |
| | | public short getRowHeight() |
| | | { |
| | | double maxHeight = 0; |
| | | for (Object[] os : this.fields) |
| | | { |
| | | Excel excel = (Excel) os[1]; |
| | | maxHeight = Math.max(maxHeight, excel.height()); |
| | | } |
| | | return (short) (maxHeight * 20); |
| | | } |
| | | |
| | | /** |
| | | * å建ä¸ä¸ªå·¥ä½ç°¿ |
| | | */ |
| | | public void createWorkbook() |
| | | { |
| | | this.wb = new SXSSFWorkbook(500); |
| | | this.sheet = wb.createSheet(); |
| | | wb.setSheetName(0, sheetName); |
| | | this.styles = createStyles(wb); |
| | | } |
| | | |
| | | /** |
| | | * å建工ä½è¡¨ |
| | | * |
| | | * @param sheetNo sheetæ°é |
| | | * @param index åºå· |
| | | */ |
| | | public void createSheet(int sheetNo, int index) |
| | | { |
| | | // 设置工ä½è¡¨çåç§°. |
| | | if (sheetNo > 1 && index > 0) |
| | | { |
| | | this.sheet = wb.createSheet(); |
| | | this.createTitle(); |
| | | wb.setSheetName(index, sheetName + index); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·ååå
æ ¼å¼ |
| | | * |
| | | * @param row è·åçè¡ |
| | | * @param column è·ååå
æ ¼åå· |
| | | * @return åå
æ ¼å¼ |
| | | */ |
| | | public Object getCellValue(Row row, int column) |
| | | { |
| | | if (row == null) |
| | | { |
| | | return row; |
| | | } |
| | | Object val = ""; |
| | | try |
| | | { |
| | | Cell cell = row.getCell(column); |
| | | if (StringUtils.isNotNull(cell)) |
| | | { |
| | | if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA) |
| | | { |
| | | val = cell.getNumericCellValue(); |
| | | if (DateUtil.isCellDateFormatted(cell)) |
| | | { |
| | | val = DateUtil.getJavaDate((Double) val); // POI Excel æ¥ææ ¼å¼è½¬æ¢ |
| | | } |
| | | else |
| | | { |
| | | if ((Double) val % 1 != 0) |
| | | { |
| | | val = new BigDecimal(val.toString()); |
| | | } |
| | | else |
| | | { |
| | | val = new DecimalFormat("0").format(val); |
| | | } |
| | | } |
| | | } |
| | | else if (cell.getCellType() == CellType.STRING) |
| | | { |
| | | val = cell.getStringCellValue(); |
| | | } |
| | | else if (cell.getCellType() == CellType.BOOLEAN) |
| | | { |
| | | val = cell.getBooleanCellValue(); |
| | | } |
| | | else if (cell.getCellType() == CellType.ERROR) |
| | | { |
| | | val = cell.getErrorCellValue(); |
| | | } |
| | | |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return val; |
| | | } |
| | | return val; |
| | | } |
| | | |
| | | /** |
| | | * 夿æ¯å¦æ¯ç©ºè¡ |
| | | * |
| | | * @param row 夿çè¡ |
| | | * @return |
| | | */ |
| | | private boolean isRowEmpty(Row row) |
| | | { |
| | | if (row == null) |
| | | { |
| | | return true; |
| | | } |
| | | for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) |
| | | { |
| | | Cell cell = row.getCell(i); |
| | | if (cell != null && cell.getCellType() != CellType.BLANK) |
| | | { |
| | | return false; |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * æ ¼å¼åä¸åç±»åçæ¥æå¯¹è±¡ |
| | | * |
| | | * @param dateFormat æ¥ææ ¼å¼ |
| | | * @param val è¢«æ ¼å¼åçæ¥æå¯¹è±¡ |
| | | * @return æ ¼å¼ååçæ¥æå符 |
| | | */ |
| | | public String parseDateToStr(String dateFormat, Object val) |
| | | { |
| | | if (val == null) |
| | | { |
| | | return ""; |
| | | } |
| | | String str; |
| | | if (val instanceof Date) |
| | | { |
| | | str = DateUtils.parseDateToStr(dateFormat, (Date) val); |
| | | } |
| | | else if (val instanceof LocalDateTime) |
| | | { |
| | | str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDateTime) val)); |
| | | } |
| | | else if (val instanceof LocalDate) |
| | | { |
| | | str = DateUtils.parseDateToStr(dateFormat, DateUtils.toDate((LocalDate) val)); |
| | | } |
| | | else |
| | | { |
| | | str = val.toString(); |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦æå¯¹è±¡çåå表 |
| | | */ |
| | | public boolean isSubList() |
| | | { |
| | | return StringUtils.isNotNull(subFields) && subFields.size() > 0; |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦æå¯¹è±¡çåå表ï¼éåä¸ä¸ºç©º |
| | | */ |
| | | public boolean isSubListValue(T vo) |
| | | { |
| | | return StringUtils.isNotNull(subFields) && subFields.size() > 0 && StringUtils.isNotNull(getListCellValue(vo)) && getListCellValue(vo).size() > 0; |
| | | } |
| | | |
| | | /** |
| | | * è·åéåçå¼ |
| | | */ |
| | | public Collection<?> getListCellValue(Object obj) |
| | | { |
| | | Object value; |
| | | try |
| | | { |
| | | value = subMethod.invoke(obj, new Object[] {}); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return new ArrayList<Object>(); |
| | | } |
| | | return (Collection<?>) value; |
| | | } |
| | | |
| | | /** |
| | | * è·å对象çååè¡¨æ¹æ³ |
| | | * |
| | | * @param name åç§° |
| | | * @param pojoClass 类对象 |
| | | * @return ååè¡¨æ¹æ³ |
| | | */ |
| | | public Method getSubMethod(String name, Class<?> pojoClass) |
| | | { |
| | | StringBuffer getMethodName = new StringBuffer("get"); |
| | | getMethodName.append(name.substring(0, 1).toUpperCase()); |
| | | getMethodName.append(name.substring(1)); |
| | | Method method = null; |
| | | try |
| | | { |
| | | method = pojoClass.getMethod(getMethodName.toString(), new Class[] {}); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | log.error("è·å对象å¼å¸¸{}", e.getMessage()); |
| | | } |
| | | return method; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.reflect; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.InvocationTargetException; |
| | | import java.lang.reflect.Method; |
| | | import java.lang.reflect.Modifier; |
| | | import java.lang.reflect.ParameterizedType; |
| | | import java.lang.reflect.Type; |
| | | import java.util.Date; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.commons.lang3.Validate; |
| | | import org.apache.poi.ss.usermodel.DateUtil; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import com.terra.common.text.Convert; |
| | | import com.terra.common.utils.DateUtils; |
| | | |
| | | /** |
| | | * åå°å·¥å
·ç±». æä¾è°ç¨getter/setteræ¹æ³, 访é®ç§æåé, è°ç¨ç§ææ¹æ³, è·åæ³åç±»åClass, 被AOPè¿ççå®ç±»çå·¥å
·å½æ°. |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @SuppressWarnings("rawtypes") |
| | | public class ReflectUtils |
| | | { |
| | | private static final String SETTER_PREFIX = "set"; |
| | | |
| | | private static final String GETTER_PREFIX = "get"; |
| | | |
| | | private static final String CGLIB_CLASS_SEPARATOR = "$$"; |
| | | |
| | | private static Logger logger = LoggerFactory.getLogger(ReflectUtils.class); |
| | | |
| | | /** |
| | | * è°ç¨Getteræ¹æ³. |
| | | * æ¯æå¤çº§ï¼å¦ï¼å¯¹è±¡å.对象å.æ¹æ³ |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <E> E invokeGetter(Object obj, String propertyName) |
| | | { |
| | | Object object = obj; |
| | | for (String name : StringUtils.split(propertyName, ".")) |
| | | { |
| | | String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(name); |
| | | object = invokeMethod(object, getterMethodName, new Class[] {}, new Object[] {}); |
| | | } |
| | | return (E) object; |
| | | } |
| | | |
| | | /** |
| | | * è°ç¨Setteræ¹æ³, ä»
å¹é
æ¹æ³åã |
| | | * æ¯æå¤çº§ï¼å¦ï¼å¯¹è±¡å.对象å.æ¹æ³ |
| | | */ |
| | | public static <E> void invokeSetter(Object obj, String propertyName, E value) |
| | | { |
| | | Object object = obj; |
| | | String[] names = StringUtils.split(propertyName, "."); |
| | | for (int i = 0; i < names.length; i++) |
| | | { |
| | | if (i < names.length - 1) |
| | | { |
| | | String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(names[i]); |
| | | object = invokeMethod(object, getterMethodName, new Class[] {}, new Object[] {}); |
| | | } |
| | | else |
| | | { |
| | | String setterMethodName = SETTER_PREFIX + StringUtils.capitalize(names[i]); |
| | | invokeMethodByName(object, setterMethodName, new Object[] { value }); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ç´æ¥è¯»åå¯¹è±¡å±æ§å¼, æ è§private/protected修饰符, ä¸ç»è¿getter彿°. |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <E> E getFieldValue(final Object obj, final String fieldName) |
| | | { |
| | | Field field = getAccessibleField(obj, fieldName); |
| | | if (field == null) |
| | | { |
| | | logger.debug("å¨ [" + obj.getClass() + "] ä¸ï¼æ²¡ææ¾å° [" + fieldName + "] åæ®µ "); |
| | | return null; |
| | | } |
| | | E result = null; |
| | | try |
| | | { |
| | | result = (E) field.get(obj); |
| | | } |
| | | catch (IllegalAccessException e) |
| | | { |
| | | logger.error("ä¸å¯è½æåºçå¼å¸¸{}", e.getMessage()); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * ç´æ¥è®¾ç½®å¯¹è±¡å±æ§å¼, æ è§private/protected修饰符, ä¸ç»è¿setter彿°. |
| | | */ |
| | | public static <E> void setFieldValue(final Object obj, final String fieldName, final E value) |
| | | { |
| | | Field field = getAccessibleField(obj, fieldName); |
| | | if (field == null) |
| | | { |
| | | // throw new IllegalArgumentException("å¨ [" + obj.getClass() + "] ä¸ï¼æ²¡ææ¾å° [" + fieldName + "] åæ®µ "); |
| | | logger.debug("å¨ [" + obj.getClass() + "] ä¸ï¼æ²¡ææ¾å° [" + fieldName + "] åæ®µ "); |
| | | return; |
| | | } |
| | | try |
| | | { |
| | | field.set(obj, value); |
| | | } |
| | | catch (IllegalAccessException e) |
| | | { |
| | | logger.error("ä¸å¯è½æåºçå¼å¸¸: {}", e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ç´æ¥è°ç¨å¯¹è±¡æ¹æ³, æ è§private/protected修饰符. |
| | | * ç¨äºä¸æ¬¡æ§è°ç¨çæ
åµï¼å¦ååºä½¿ç¨getAccessibleMethod()彿°è·å¾Methodååå¤è°ç¨. |
| | | * åæ¶å¹é
æ¹æ³å+åæ°ç±»åï¼ |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <E> E invokeMethod(final Object obj, final String methodName, final Class<?>[] parameterTypes, |
| | | final Object[] args) |
| | | { |
| | | if (obj == null || methodName == null) |
| | | { |
| | | return null; |
| | | } |
| | | Method method = getAccessibleMethod(obj, methodName, parameterTypes); |
| | | if (method == null) |
| | | { |
| | | logger.debug("å¨ [" + obj.getClass() + "] ä¸ï¼æ²¡ææ¾å° [" + methodName + "] æ¹æ³ "); |
| | | return null; |
| | | } |
| | | try |
| | | { |
| | | return (E) method.invoke(obj, args); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | String msg = "method: " + method + ", obj: " + obj + ", args: " + args + ""; |
| | | throw convertReflectionExceptionToUnchecked(msg, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ç´æ¥è°ç¨å¯¹è±¡æ¹æ³, æ è§private/protectedä¿®é¥°ç¬¦ï¼ |
| | | * ç¨äºä¸æ¬¡æ§è°ç¨çæ
åµï¼å¦ååºä½¿ç¨getAccessibleMethodByName()彿°è·å¾Methodååå¤è°ç¨. |
| | | * åªå¹é
彿°åï¼å¦ææå¤ä¸ªåå彿°è°ç¨ç¬¬ä¸ä¸ªã |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <E> E invokeMethodByName(final Object obj, final String methodName, final Object[] args) |
| | | { |
| | | Method method = getAccessibleMethodByName(obj, methodName, args.length); |
| | | if (method == null) |
| | | { |
| | | // å¦æä¸ºç©ºä¸æ¥éï¼ç´æ¥è¿å空ã |
| | | logger.debug("å¨ [" + obj.getClass() + "] ä¸ï¼æ²¡ææ¾å° [" + methodName + "] æ¹æ³ "); |
| | | return null; |
| | | } |
| | | try |
| | | { |
| | | // ç±»å转æ¢ï¼å°åæ°æ°æ®ç±»å转æ¢ä¸ºç®æ æ¹æ³åæ°ç±»åï¼ |
| | | Class<?>[] cs = method.getParameterTypes(); |
| | | for (int i = 0; i < cs.length; i++) |
| | | { |
| | | if (args[i] != null && !args[i].getClass().equals(cs[i])) |
| | | { |
| | | if (cs[i] == String.class) |
| | | { |
| | | args[i] = Convert.toStr(args[i]); |
| | | if (StringUtils.endsWith((String) args[i], ".0")) |
| | | { |
| | | args[i] = StringUtils.substringBefore((String) args[i], ".0"); |
| | | } |
| | | } |
| | | else if (cs[i] == Integer.class) |
| | | { |
| | | args[i] = Convert.toInt(args[i]); |
| | | } |
| | | else if (cs[i] == Long.class) |
| | | { |
| | | args[i] = Convert.toLong(args[i]); |
| | | } |
| | | else if (cs[i] == Double.class) |
| | | { |
| | | args[i] = Convert.toDouble(args[i]); |
| | | } |
| | | else if (cs[i] == Float.class) |
| | | { |
| | | args[i] = Convert.toFloat(args[i]); |
| | | } |
| | | else if (cs[i] == Date.class) |
| | | { |
| | | if (args[i] instanceof String) |
| | | { |
| | | args[i] = DateUtils.parseDate(args[i]); |
| | | } |
| | | else |
| | | { |
| | | args[i] = DateUtil.getJavaDate((Double) args[i]); |
| | | } |
| | | } |
| | | else if (cs[i] == boolean.class || cs[i] == Boolean.class) |
| | | { |
| | | args[i] = Convert.toBool(args[i]); |
| | | } |
| | | } |
| | | } |
| | | return (E) method.invoke(obj, args); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | String msg = "method: " + method + ", obj: " + obj + ", args: " + args + ""; |
| | | throw convertReflectionExceptionToUnchecked(msg, e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 循ç¯åä¸è½¬å, è·å对象çDeclaredField, 并强å¶è®¾ç½®ä¸ºå¯è®¿é®. |
| | | * å¦åä¸è½¬åå°Object仿 æ³æ¾å°, è¿ånull. |
| | | */ |
| | | public static Field getAccessibleField(final Object obj, final String fieldName) |
| | | { |
| | | // ä¸ºç©ºä¸æ¥éãç´æ¥è¿å null |
| | | if (obj == null) |
| | | { |
| | | return null; |
| | | } |
| | | Validate.notBlank(fieldName, "fieldName can't be blank"); |
| | | for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) |
| | | { |
| | | try |
| | | { |
| | | Field field = superClass.getDeclaredField(fieldName); |
| | | makeAccessible(field); |
| | | return field; |
| | | } |
| | | catch (NoSuchFieldException e) |
| | | { |
| | | continue; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 循ç¯åä¸è½¬å, è·å对象çDeclaredMethod,并强å¶è®¾ç½®ä¸ºå¯è®¿é®. |
| | | * å¦åä¸è½¬åå°Object仿 æ³æ¾å°, è¿ånull. |
| | | * å¹é
彿°å+åæ°ç±»åã |
| | | * ç¨äºæ¹æ³éè¦è¢«å¤æ¬¡è°ç¨çæ
åµ. å
ä½¿ç¨æ¬å½æ°å
åå¾Method,ç¶åè°ç¨Method.invoke(Object obj, Object... args) |
| | | */ |
| | | public static Method getAccessibleMethod(final Object obj, final String methodName, |
| | | final Class<?>... parameterTypes) |
| | | { |
| | | // ä¸ºç©ºä¸æ¥éãç´æ¥è¿å null |
| | | if (obj == null) |
| | | { |
| | | return null; |
| | | } |
| | | Validate.notBlank(methodName, "methodName can't be blank"); |
| | | for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) |
| | | { |
| | | try |
| | | { |
| | | Method method = searchType.getDeclaredMethod(methodName, parameterTypes); |
| | | makeAccessible(method); |
| | | return method; |
| | | } |
| | | catch (NoSuchMethodException e) |
| | | { |
| | | continue; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 循ç¯åä¸è½¬å, è·å对象çDeclaredMethod,并强å¶è®¾ç½®ä¸ºå¯è®¿é®. |
| | | * å¦åä¸è½¬åå°Object仿 æ³æ¾å°, è¿ånull. |
| | | * åªå¹é
彿°åã |
| | | * ç¨äºæ¹æ³éè¦è¢«å¤æ¬¡è°ç¨çæ
åµ. å
ä½¿ç¨æ¬å½æ°å
åå¾Method,ç¶åè°ç¨Method.invoke(Object obj, Object... args) |
| | | */ |
| | | public static Method getAccessibleMethodByName(final Object obj, final String methodName, int argsNum) |
| | | { |
| | | // ä¸ºç©ºä¸æ¥éãç´æ¥è¿å null |
| | | if (obj == null) |
| | | { |
| | | return null; |
| | | } |
| | | Validate.notBlank(methodName, "methodName can't be blank"); |
| | | for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) |
| | | { |
| | | Method[] methods = searchType.getDeclaredMethods(); |
| | | for (Method method : methods) |
| | | { |
| | | if (method.getName().equals(methodName) && method.getParameterTypes().length == argsNum) |
| | | { |
| | | makeAccessible(method); |
| | | return method; |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * æ¹åprivate/protectedçæ¹æ³ä¸ºpublicï¼å°½éä¸è°ç¨å®é
æ¹å¨çè¯å¥ï¼é¿å
JDKçSecurityManageræ±æ¨ã |
| | | */ |
| | | public static void makeAccessible(Method method) |
| | | { |
| | | if ((!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) |
| | | && !method.isAccessible()) |
| | | { |
| | | method.setAccessible(true); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¹åprivate/protectedçæååé为publicï¼å°½éä¸è°ç¨å®é
æ¹å¨çè¯å¥ï¼é¿å
JDKçSecurityManageræ±æ¨ã |
| | | */ |
| | | public static void makeAccessible(Field field) |
| | | { |
| | | if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) |
| | | || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) |
| | | { |
| | | field.setAccessible(true); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * éè¿åå°, è·å¾Classå®ä¹ä¸å£°æçæ³ååæ°çç±»å, æ³¨ææ³åå¿
é¡»å®ä¹å¨ç¶ç±»å¤ |
| | | * 妿 æ³æ¾å°, è¿åObject.class. |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | public static <T> Class<T> getClassGenricType(final Class clazz) |
| | | { |
| | | return getClassGenricType(clazz, 0); |
| | | } |
| | | |
| | | /** |
| | | * éè¿åå°, è·å¾Classå®ä¹ä¸å£°æçç¶ç±»çæ³ååæ°çç±»å. |
| | | * 妿 æ³æ¾å°, è¿åObject.class. |
| | | */ |
| | | public static Class getClassGenricType(final Class clazz, final int index) |
| | | { |
| | | Type genType = clazz.getGenericSuperclass(); |
| | | |
| | | if (!(genType instanceof ParameterizedType)) |
| | | { |
| | | logger.debug(clazz.getSimpleName() + "'s superclass not ParameterizedType"); |
| | | return Object.class; |
| | | } |
| | | |
| | | Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); |
| | | |
| | | if (index >= params.length || index < 0) |
| | | { |
| | | logger.debug("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " |
| | | + params.length); |
| | | return Object.class; |
| | | } |
| | | if (!(params[index] instanceof Class)) |
| | | { |
| | | logger.debug(clazz.getSimpleName() + " not set the actual class on superclass generic parameter"); |
| | | return Object.class; |
| | | } |
| | | |
| | | return (Class) params[index]; |
| | | } |
| | | |
| | | public static Class<?> getUserClass(Object instance) |
| | | { |
| | | if (instance == null) |
| | | { |
| | | throw new RuntimeException("Instance must not be null"); |
| | | } |
| | | Class clazz = instance.getClass(); |
| | | if (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) |
| | | { |
| | | Class<?> superClass = clazz.getSuperclass(); |
| | | if (superClass != null && !Object.class.equals(superClass)) |
| | | { |
| | | return superClass; |
| | | } |
| | | } |
| | | return clazz; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * å°åå°æ¶çchecked exception转æ¢ä¸ºunchecked exception. |
| | | */ |
| | | public static RuntimeException convertReflectionExceptionToUnchecked(String msg, Exception e) |
| | | { |
| | | if (e instanceof IllegalAccessException || e instanceof IllegalArgumentException |
| | | || e instanceof NoSuchMethodException) |
| | | { |
| | | return new IllegalArgumentException(msg, e); |
| | | } |
| | | else if (e instanceof InvocationTargetException) |
| | | { |
| | | return new RuntimeException(msg, ((InvocationTargetException) e).getTargetException()); |
| | | } |
| | | return new RuntimeException(msg, e); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.sign; |
| | | |
| | | /** |
| | | * Base64å·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public final class Base64 |
| | | { |
| | | static private final int BASELENGTH = 128; |
| | | static private final int LOOKUPLENGTH = 64; |
| | | static private final int TWENTYFOURBITGROUP = 24; |
| | | static private final int EIGHTBIT = 8; |
| | | static private final int SIXTEENBIT = 16; |
| | | static private final int FOURBYTE = 4; |
| | | static private final int SIGN = -128; |
| | | static private final char PAD = '='; |
| | | static final private byte[] base64Alphabet = new byte[BASELENGTH]; |
| | | static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH]; |
| | | |
| | | static |
| | | { |
| | | for (int i = 0; i < BASELENGTH; ++i) |
| | | { |
| | | base64Alphabet[i] = -1; |
| | | } |
| | | for (int i = 'Z'; i >= 'A'; i--) |
| | | { |
| | | base64Alphabet[i] = (byte) (i - 'A'); |
| | | } |
| | | for (int i = 'z'; i >= 'a'; i--) |
| | | { |
| | | base64Alphabet[i] = (byte) (i - 'a' + 26); |
| | | } |
| | | |
| | | for (int i = '9'; i >= '0'; i--) |
| | | { |
| | | base64Alphabet[i] = (byte) (i - '0' + 52); |
| | | } |
| | | |
| | | base64Alphabet['+'] = 62; |
| | | base64Alphabet['/'] = 63; |
| | | |
| | | for (int i = 0; i <= 25; i++) |
| | | { |
| | | lookUpBase64Alphabet[i] = (char) ('A' + i); |
| | | } |
| | | |
| | | for (int i = 26, j = 0; i <= 51; i++, j++) |
| | | { |
| | | lookUpBase64Alphabet[i] = (char) ('a' + j); |
| | | } |
| | | |
| | | for (int i = 52, j = 0; i <= 61; i++, j++) |
| | | { |
| | | lookUpBase64Alphabet[i] = (char) ('0' + j); |
| | | } |
| | | lookUpBase64Alphabet[62] = (char) '+'; |
| | | lookUpBase64Alphabet[63] = (char) '/'; |
| | | } |
| | | |
| | | private static boolean isWhiteSpace(char octect) |
| | | { |
| | | return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9); |
| | | } |
| | | |
| | | private static boolean isPad(char octect) |
| | | { |
| | | return (octect == PAD); |
| | | } |
| | | |
| | | private static boolean isData(char octect) |
| | | { |
| | | return (octect < BASELENGTH && base64Alphabet[octect] != -1); |
| | | } |
| | | |
| | | /** |
| | | * Encodes hex octects into Base64 |
| | | * |
| | | * @param binaryData Array containing binaryData |
| | | * @return Encoded Base64 array |
| | | */ |
| | | public static String encode(byte[] binaryData) |
| | | { |
| | | if (binaryData == null) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | int lengthDataBits = binaryData.length * EIGHTBIT; |
| | | if (lengthDataBits == 0) |
| | | { |
| | | return ""; |
| | | } |
| | | |
| | | int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP; |
| | | int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP; |
| | | int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets; |
| | | char encodedData[] = null; |
| | | |
| | | encodedData = new char[numberQuartet * 4]; |
| | | |
| | | byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0; |
| | | |
| | | int encodedIndex = 0; |
| | | int dataIndex = 0; |
| | | |
| | | for (int i = 0; i < numberTriplets; i++) |
| | | { |
| | | b1 = binaryData[dataIndex++]; |
| | | b2 = binaryData[dataIndex++]; |
| | | b3 = binaryData[dataIndex++]; |
| | | |
| | | l = (byte) (b2 & 0x0f); |
| | | k = (byte) (b1 & 0x03); |
| | | |
| | | byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); |
| | | byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0); |
| | | byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc); |
| | | |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f]; |
| | | } |
| | | |
| | | // form integral number of 6-bit groups |
| | | if (fewerThan24bits == EIGHTBIT) |
| | | { |
| | | b1 = binaryData[dataIndex]; |
| | | k = (byte) (b1 & 0x03); |
| | | byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4]; |
| | | encodedData[encodedIndex++] = PAD; |
| | | encodedData[encodedIndex++] = PAD; |
| | | } |
| | | else if (fewerThan24bits == SIXTEENBIT) |
| | | { |
| | | b1 = binaryData[dataIndex]; |
| | | b2 = binaryData[dataIndex + 1]; |
| | | l = (byte) (b2 & 0x0f); |
| | | k = (byte) (b1 & 0x03); |
| | | |
| | | byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); |
| | | byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0); |
| | | |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; |
| | | encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2]; |
| | | encodedData[encodedIndex++] = PAD; |
| | | } |
| | | return new String(encodedData); |
| | | } |
| | | |
| | | /** |
| | | * Decodes Base64 data into octects |
| | | * |
| | | * @param encoded string containing Base64 data |
| | | * @return Array containind decoded data. |
| | | */ |
| | | public static byte[] decode(String encoded) |
| | | { |
| | | if (encoded == null) |
| | | { |
| | | return null; |
| | | } |
| | | |
| | | char[] base64Data = encoded.toCharArray(); |
| | | // remove white spaces |
| | | int len = removeWhiteSpace(base64Data); |
| | | |
| | | if (len % FOURBYTE != 0) |
| | | { |
| | | return null;// should be divisible by four |
| | | } |
| | | |
| | | int numberQuadruple = (len / FOURBYTE); |
| | | |
| | | if (numberQuadruple == 0) |
| | | { |
| | | return new byte[0]; |
| | | } |
| | | |
| | | byte decodedData[] = null; |
| | | byte b1 = 0, b2 = 0, b3 = 0, b4 = 0; |
| | | char d1 = 0, d2 = 0, d3 = 0, d4 = 0; |
| | | |
| | | int i = 0; |
| | | int encodedIndex = 0; |
| | | int dataIndex = 0; |
| | | decodedData = new byte[(numberQuadruple) * 3]; |
| | | |
| | | for (; i < numberQuadruple - 1; i++) |
| | | { |
| | | |
| | | if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++])) |
| | | || !isData((d3 = base64Data[dataIndex++])) || !isData((d4 = base64Data[dataIndex++]))) |
| | | { |
| | | return null; |
| | | } // if found "no data" just return null |
| | | |
| | | b1 = base64Alphabet[d1]; |
| | | b2 = base64Alphabet[d2]; |
| | | b3 = base64Alphabet[d3]; |
| | | b4 = base64Alphabet[d4]; |
| | | |
| | | decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); |
| | | decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); |
| | | decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); |
| | | } |
| | | |
| | | if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))) |
| | | { |
| | | return null;// if found "no data" just return null |
| | | } |
| | | |
| | | b1 = base64Alphabet[d1]; |
| | | b2 = base64Alphabet[d2]; |
| | | |
| | | d3 = base64Data[dataIndex++]; |
| | | d4 = base64Data[dataIndex++]; |
| | | if (!isData((d3)) || !isData((d4))) |
| | | {// Check if they are PAD characters |
| | | if (isPad(d3) && isPad(d4)) |
| | | { |
| | | if ((b2 & 0xf) != 0)// last 4 bits should be zero |
| | | { |
| | | return null; |
| | | } |
| | | byte[] tmp = new byte[i * 3 + 1]; |
| | | System.arraycopy(decodedData, 0, tmp, 0, i * 3); |
| | | tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); |
| | | return tmp; |
| | | } |
| | | else if (!isPad(d3) && isPad(d4)) |
| | | { |
| | | b3 = base64Alphabet[d3]; |
| | | if ((b3 & 0x3) != 0)// last 2 bits should be zero |
| | | { |
| | | return null; |
| | | } |
| | | byte[] tmp = new byte[i * 3 + 2]; |
| | | System.arraycopy(decodedData, 0, tmp, 0, i * 3); |
| | | tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); |
| | | tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); |
| | | return tmp; |
| | | } |
| | | else |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | else |
| | | { // No PAD e.g 3cQl |
| | | b3 = base64Alphabet[d3]; |
| | | b4 = base64Alphabet[d4]; |
| | | decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); |
| | | decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); |
| | | decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); |
| | | |
| | | } |
| | | return decodedData; |
| | | } |
| | | |
| | | /** |
| | | * remove WhiteSpace from MIME containing encoded Base64 data. |
| | | * |
| | | * @param data the byte array of base64 data (with WS) |
| | | * @return the new length |
| | | */ |
| | | private static int removeWhiteSpace(char[] data) |
| | | { |
| | | if (data == null) |
| | | { |
| | | return 0; |
| | | } |
| | | |
| | | // count characters that's not whitespace |
| | | int newSize = 0; |
| | | int len = data.length; |
| | | for (int i = 0; i < len; i++) |
| | | { |
| | | if (!isWhiteSpace(data[i])) |
| | | { |
| | | data[newSize++] = data[i]; |
| | | } |
| | | } |
| | | return newSize; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.sql; |
| | | |
| | | import com.terra.common.exception.UtilException; |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * sqlæä½å·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class SqlUtil |
| | | { |
| | | /** |
| | | * å®ä¹å¸¸ç¨ç sqlå
³é®å |
| | | */ |
| | | public static String SQL_REGEX = "\u000B|and |extractvalue|updatexml|sleep|exec |insert |select |delete |update |drop |count |chr |mid |master |truncate |char |declare |or |union |like |+|/*|user()"; |
| | | |
| | | /** |
| | | * ä»
æ¯æåæ¯ãæ°åãä¸å线ãç©ºæ ¼ãéå·ãå°æ°ç¹ï¼æ¯æå¤ä¸ªå段æåºï¼ |
| | | */ |
| | | public static String SQL_PATTERN = "[a-zA-Z0-9_\\ \\,\\.]+"; |
| | | |
| | | /** |
| | | * éå¶orderByæå¤§é¿åº¦ |
| | | */ |
| | | private static final int ORDER_BY_MAX_LENGTH = 500; |
| | | |
| | | /** |
| | | * æ£æ¥å符ï¼é²æ¢æ³¨å
¥ç»è¿ |
| | | */ |
| | | public static String escapeOrderBySql(String value) |
| | | { |
| | | if (StringUtils.isNotEmpty(value) && !isValidOrderBySql(value)) |
| | | { |
| | | throw new UtilException("åæ°ä¸ç¬¦åè§èï¼ä¸è½è¿è¡æ¥è¯¢"); |
| | | } |
| | | if (StringUtils.length(value) > ORDER_BY_MAX_LENGTH) |
| | | { |
| | | throw new UtilException("åæ°å·²è¶
è¿æå¤§éå¶ï¼ä¸è½è¿è¡æ¥è¯¢"); |
| | | } |
| | | return value; |
| | | } |
| | | |
| | | /** |
| | | * éªè¯ order by è¯æ³æ¯å¦ç¬¦åè§è |
| | | */ |
| | | public static boolean isValidOrderBySql(String value) |
| | | { |
| | | return value.matches(SQL_PATTERN); |
| | | } |
| | | |
| | | /** |
| | | * SQLå
³é®åæ£æ¥ |
| | | */ |
| | | public static void filterKeyword(String value) |
| | | { |
| | | if (StringUtils.isEmpty(value)) |
| | | { |
| | | return; |
| | | } |
| | | String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|"); |
| | | for (String sqlKeyword : sqlKeywords) |
| | | { |
| | | if (StringUtils.indexOfIgnoreCase(value, sqlKeyword) > -1) |
| | | { |
| | | throw new UtilException("åæ°åå¨SQL注å
¥é£é©"); |
| | | } |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.uuid; |
| | | |
| | | /** |
| | | * IDçæå¨å·¥å
·ç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class IdUtils |
| | | { |
| | | /** |
| | | * è·åéæºUUID |
| | | * |
| | | * @return éæºUUID |
| | | */ |
| | | public static String randomUUID() |
| | | { |
| | | return UUID.randomUUID().toString(); |
| | | } |
| | | |
| | | /** |
| | | * ç®åçUUIDï¼å»æäºæ¨ªçº¿ |
| | | * |
| | | * @return ç®åçUUIDï¼å»æäºæ¨ªçº¿ |
| | | */ |
| | | public static String simpleUUID() |
| | | { |
| | | return UUID.randomUUID().toString(true); |
| | | } |
| | | |
| | | /** |
| | | * è·åéæºUUIDï¼ä½¿ç¨æ§è½æ´å¥½çThreadLocalRandomçæUUID |
| | | * |
| | | * @return éæºUUID |
| | | */ |
| | | public static String fastUUID() |
| | | { |
| | | return UUID.fastUUID().toString(); |
| | | } |
| | | |
| | | /** |
| | | * ç®åçUUIDï¼å»æäºæ¨ªçº¿ï¼ä½¿ç¨æ§è½æ´å¥½çThreadLocalRandomçæUUID |
| | | * |
| | | * @return ç®åçUUIDï¼å»æäºæ¨ªçº¿ |
| | | */ |
| | | public static String fastSimpleUUID() |
| | | { |
| | | return UUID.fastUUID().toString(true); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.uuid; |
| | | |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import com.terra.common.utils.DateUtils; |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * @author ruoyi åºåçæç±» |
| | | */ |
| | | public class Seq |
| | | { |
| | | // éç¨åºåç±»å |
| | | public static final String commSeqType = "COMMON"; |
| | | |
| | | // ä¸ä¼ åºåç±»å |
| | | public static final String uploadSeqType = "UPLOAD"; |
| | | |
| | | // éç¨æ¥å£åºåæ° |
| | | private static AtomicInteger commSeq = new AtomicInteger(1); |
| | | |
| | | // ä¸ä¼ æ¥å£åºåæ° |
| | | private static AtomicInteger uploadSeq = new AtomicInteger(1); |
| | | |
| | | // æºå¨æ è¯ |
| | | private static final String machineCode = "A"; |
| | | |
| | | /** |
| | | * è·åéç¨åºåå· |
| | | * |
| | | * @return åºåå¼ |
| | | */ |
| | | public static String getId() |
| | | { |
| | | return getId(commSeqType); |
| | | } |
| | | |
| | | /** |
| | | * é»è®¤16ä½åºåå· yyMMddHHmmss + ä¸ä½æºå¨æ è¯ + 3é¿åº¦å¾ªç¯éå¢å符串 |
| | | * |
| | | * @return åºåå¼ |
| | | */ |
| | | public static String getId(String type) |
| | | { |
| | | AtomicInteger atomicInt = commSeq; |
| | | if (uploadSeqType.equals(type)) |
| | | { |
| | | atomicInt = uploadSeq; |
| | | } |
| | | return getId(atomicInt, 3); |
| | | } |
| | | |
| | | /** |
| | | * éç¨æ¥å£åºåå· yyMMddHHmmss + ä¸ä½æºå¨æ è¯ + lengthé¿åº¦å¾ªç¯éå¢å符串 |
| | | * |
| | | * @param atomicInt åºåæ° |
| | | * @param length æ°å¼é¿åº¦ |
| | | * @return åºåå¼ |
| | | */ |
| | | public static String getId(AtomicInteger atomicInt, int length) |
| | | { |
| | | String result = DateUtils.dateTimeNow(); |
| | | result += machineCode; |
| | | result += getSeq(atomicInt, length); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * åºå循ç¯éå¢å符串[1, 10 ç (length)广¬¡æ¹), ç¨0左补é½length使° |
| | | * |
| | | * @return åºåå¼ |
| | | */ |
| | | private synchronized static String getSeq(AtomicInteger atomicInt, int length) |
| | | { |
| | | // å
åå¼å+1 |
| | | int value = atomicInt.getAndIncrement(); |
| | | |
| | | // å¦ææ´æ°åå¼>=10 ç (length)广¬¡æ¹åé置为1 |
| | | int maxSeq = (int) Math.pow(10, length); |
| | | if (atomicInt.get() >= maxSeq) |
| | | { |
| | | atomicInt.set(1); |
| | | } |
| | | // 转å符串ï¼ç¨0å·¦è¡¥é½ |
| | | return StringUtils.padl(value, length); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.utils.uuid; |
| | | |
| | | import java.security.MessageDigest; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.security.SecureRandom; |
| | | import java.util.Random; |
| | | import java.util.concurrent.ThreadLocalRandom; |
| | | import com.terra.common.exception.UtilException; |
| | | |
| | | /** |
| | | * æä¾éç¨å¯ä¸è¯å«ç ï¼universally unique identifierï¼ï¼UUIDï¼å®ç° |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public final class UUID implements java.io.Serializable, Comparable<UUID> |
| | | { |
| | | private static final long serialVersionUID = -1185015143654744140L; |
| | | |
| | | /** |
| | | * SecureRandom çåä¾ |
| | | * |
| | | */ |
| | | private static class Holder |
| | | { |
| | | static final SecureRandom numberGenerator = getSecureRandom(); |
| | | } |
| | | |
| | | /** æ¤UUIDçæé«64ææä½ */ |
| | | private final long mostSigBits; |
| | | |
| | | /** æ¤UUIDçæä½64ææä½ */ |
| | | private final long leastSigBits; |
| | | |
| | | /** |
| | | * ç§ææé |
| | | * |
| | | * @param data æ°æ® |
| | | */ |
| | | private UUID(byte[] data) |
| | | { |
| | | long msb = 0; |
| | | long lsb = 0; |
| | | assert data.length == 16 : "data must be 16 bytes in length"; |
| | | for (int i = 0; i < 8; i++) |
| | | { |
| | | msb = (msb << 8) | (data[i] & 0xff); |
| | | } |
| | | for (int i = 8; i < 16; i++) |
| | | { |
| | | lsb = (lsb << 8) | (data[i] & 0xff); |
| | | } |
| | | this.mostSigBits = msb; |
| | | this.leastSigBits = lsb; |
| | | } |
| | | |
| | | /** |
| | | * ä½¿ç¨æå®çæ°æ®æé æ°ç UUIDã |
| | | * |
| | | * @param mostSigBits ç¨äº {@code UUID} çæé«ææ 64 ä½ |
| | | * @param leastSigBits ç¨äº {@code UUID} çæä½ææ 64 ä½ |
| | | */ |
| | | public UUID(long mostSigBits, long leastSigBits) |
| | | { |
| | | this.mostSigBits = mostSigBits; |
| | | this.leastSigBits = leastSigBits; |
| | | } |
| | | |
| | | /** |
| | | * è·åç±»å 4ï¼ä¼ªéæºçæçï¼UUID çéæå·¥åã |
| | | * |
| | | * @return éæºçæç {@code UUID} |
| | | */ |
| | | public static UUID fastUUID() |
| | | { |
| | | return randomUUID(false); |
| | | } |
| | | |
| | | /** |
| | | * è·åç±»å 4ï¼ä¼ªéæºçæçï¼UUID çéæå·¥åã 使ç¨å å¯çå¼ºä¼ªéæºæ°çæå¨çæè¯¥ UUIDã |
| | | * |
| | | * @return éæºçæç {@code UUID} |
| | | */ |
| | | public static UUID randomUUID() |
| | | { |
| | | return randomUUID(true); |
| | | } |
| | | |
| | | /** |
| | | * è·åç±»å 4ï¼ä¼ªéæºçæçï¼UUID çéæå·¥åã 使ç¨å å¯çå¼ºä¼ªéæºæ°çæå¨çæè¯¥ UUIDã |
| | | * |
| | | * @param isSecure æ¯å¦ä½¿ç¨{@link SecureRandom}妿æ¯å¯ä»¥è·å¾æ´å®å
¨çéæºç ï¼å¦åå¯ä»¥å¾å°æ´å¥½çæ§è½ |
| | | * @return éæºçæç {@code UUID} |
| | | */ |
| | | public static UUID randomUUID(boolean isSecure) |
| | | { |
| | | final Random ng = isSecure ? Holder.numberGenerator : getRandom(); |
| | | |
| | | byte[] randomBytes = new byte[16]; |
| | | ng.nextBytes(randomBytes); |
| | | randomBytes[6] &= 0x0f; /* clear version */ |
| | | randomBytes[6] |= 0x40; /* set to version 4 */ |
| | | randomBytes[8] &= 0x3f; /* clear variant */ |
| | | randomBytes[8] |= 0x80; /* set to IETF variant */ |
| | | return new UUID(randomBytes); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®æå®çåèæ°ç»è·åç±»å 3ï¼åºäºåç§°çï¼UUID çéæå·¥åã |
| | | * |
| | | * @param name ç¨äºæé UUID çåèæ°ç»ã |
| | | * |
| | | * @return æ ¹æ®æå®æ°ç»çæç {@code UUID} |
| | | */ |
| | | public static UUID nameUUIDFromBytes(byte[] name) |
| | | { |
| | | MessageDigest md; |
| | | try |
| | | { |
| | | md = MessageDigest.getInstance("MD5"); |
| | | } |
| | | catch (NoSuchAlgorithmException nsae) |
| | | { |
| | | throw new InternalError("MD5 not supported"); |
| | | } |
| | | byte[] md5Bytes = md.digest(name); |
| | | md5Bytes[6] &= 0x0f; /* clear version */ |
| | | md5Bytes[6] |= 0x30; /* set to version 3 */ |
| | | md5Bytes[8] &= 0x3f; /* clear variant */ |
| | | md5Bytes[8] |= 0x80; /* set to IETF variant */ |
| | | return new UUID(md5Bytes); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ® {@link #toString()} æ¹æ³ä¸æè¿°çå符串æ å表示形å¼å建{@code UUID}ã |
| | | * |
| | | * @param name æå® {@code UUID} å符串 |
| | | * @return å
·ææå®å¼ç {@code UUID} |
| | | * @throws IllegalArgumentException 妿 name ä¸ {@link #toString} ä¸æè¿°çå符串表示形å¼ä¸ç¬¦æåºæ¤å¼å¸¸ |
| | | * |
| | | */ |
| | | public static UUID fromString(String name) |
| | | { |
| | | String[] components = name.split("-"); |
| | | if (components.length != 5) |
| | | { |
| | | throw new IllegalArgumentException("Invalid UUID string: " + name); |
| | | } |
| | | for (int i = 0; i < 5; i++) |
| | | { |
| | | components[i] = "0x" + components[i]; |
| | | } |
| | | |
| | | long mostSigBits = Long.decode(components[0]).longValue(); |
| | | mostSigBits <<= 16; |
| | | mostSigBits |= Long.decode(components[1]).longValue(); |
| | | mostSigBits <<= 16; |
| | | mostSigBits |= Long.decode(components[2]).longValue(); |
| | | |
| | | long leastSigBits = Long.decode(components[3]).longValue(); |
| | | leastSigBits <<= 48; |
| | | leastSigBits |= Long.decode(components[4]).longValue(); |
| | | |
| | | return new UUID(mostSigBits, leastSigBits); |
| | | } |
| | | |
| | | /** |
| | | * è¿åæ¤ UUID ç 128 ä½å¼ä¸çæä½ææ 64 ä½ã |
| | | * |
| | | * @return æ¤ UUID ç 128 ä½å¼ä¸çæä½ææ 64 ä½ã |
| | | */ |
| | | public long getLeastSignificantBits() |
| | | { |
| | | return leastSigBits; |
| | | } |
| | | |
| | | /** |
| | | * è¿åæ¤ UUID ç 128 ä½å¼ä¸çæé«ææ 64 ä½ã |
| | | * |
| | | * @return æ¤ UUID ç 128 ä½å¼ä¸æé«ææ 64 ä½ã |
| | | */ |
| | | public long getMostSignificantBits() |
| | | { |
| | | return mostSigBits; |
| | | } |
| | | |
| | | /** |
| | | * 䏿¤ {@code UUID} ç¸å
³èççæ¬å·. çæ¬å·æè¿°æ¤ {@code UUID} æ¯å¦ä½çæçã |
| | | * <p> |
| | | * çæ¬å·å
·æä»¥ä¸å«æ: |
| | | * <ul> |
| | | * <li>1 åºäºæ¶é´ç UUID |
| | | * <li>2 DCE å®å
¨ UUID |
| | | * <li>3 åºäºåç§°ç UUID |
| | | * <li>4 éæºçæç UUID |
| | | * </ul> |
| | | * |
| | | * @return æ¤ {@code UUID} ççæ¬å· |
| | | */ |
| | | public int version() |
| | | { |
| | | // Version is bits masked by 0x000000000000F000 in MS long |
| | | return (int) ((mostSigBits >> 12) & 0x0f); |
| | | } |
| | | |
| | | /** |
| | | * 䏿¤ {@code UUID} ç¸å
³èçåä½å·ãåä½å·æè¿° {@code UUID} çå¸å±ã |
| | | * <p> |
| | | * åä½å·å
·æä»¥ä¸å«æï¼ |
| | | * <ul> |
| | | * <li>0 为 NCS ååå
¼å®¹ä¿ç |
| | | * <li>2 <a href="http://www.ietf.org/rfc/rfc4122.txt">IETF RFC 4122</a>(Leach-Salz), ç¨äºæ¤ç±» |
| | | * <li>6 ä¿çï¼å¾®è½¯ååå
¼å®¹ |
| | | * <li>7 ä¿çä¾ä»¥åå®ä¹ä½¿ç¨ |
| | | * </ul> |
| | | * |
| | | * @return æ¤ {@code UUID} ç¸å
³èçåä½å· |
| | | */ |
| | | public int variant() |
| | | { |
| | | // This field is composed of a varying number of bits. |
| | | // 0 - - Reserved for NCS backward compatibility |
| | | // 1 0 - The IETF aka Leach-Salz variant (used by this class) |
| | | // 1 1 0 Reserved, Microsoft backward compatibility |
| | | // 1 1 1 Reserved for future definition. |
| | | return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62))) & (leastSigBits >> 63)); |
| | | } |
| | | |
| | | /** |
| | | * 䏿¤ UUID ç¸å
³èçæ¶é´æ³å¼ã |
| | | * |
| | | * <p> |
| | | * 60 ä½çæ¶é´æ³å¼æ ¹æ®æ¤ {@code UUID} ç time_lowãtime_mid å time_hi åæ®µæé ã<br> |
| | | * æå¾å°çæ¶é´æ³ä»¥ 100 毫微ç§ä¸ºåä½ï¼ä» UTCï¼éç¨åè°æ¶é´ï¼ 1582 å¹´ 10 æ 15 æ¥é¶æ¶å¼å§ã |
| | | * |
| | | * <p> |
| | | * æ¶é´æ³å¼ä»
å¨å¨åºäºæ¶é´ç UUIDï¼å
¶ version ç±»å为 1ï¼ä¸æææä¹ã<br> |
| | | * å¦ææ¤ {@code UUID} 䏿¯åºäºæ¶é´ç UUIDï¼åæ¤æ¹æ³æåº UnsupportedOperationExceptionã |
| | | * |
| | | * @throws UnsupportedOperationException å¦ææ¤ {@code UUID} 䏿¯ version 为 1 ç UUIDã |
| | | */ |
| | | public long timestamp() throws UnsupportedOperationException |
| | | { |
| | | checkTimeBase(); |
| | | return (mostSigBits & 0x0FFFL) << 48// |
| | | | ((mostSigBits >> 16) & 0x0FFFFL) << 32// |
| | | | mostSigBits >>> 32; |
| | | } |
| | | |
| | | /** |
| | | * 䏿¤ UUID ç¸å
³èçæ¶éåºåå¼ã |
| | | * |
| | | * <p> |
| | | * 14 ä½çæ¶éåºå弿 ¹æ®æ¤ UUID ç clock_seq åæ®µæé ãclock_seq åæ®µç¨äºä¿è¯å¨åºäºæ¶é´ç UUID ä¸çæ¶é´å¯ä¸æ§ã |
| | | * <p> |
| | | * {@code clockSequence} å¼ä»
å¨åºäºæ¶é´ç UUIDï¼å
¶ version ç±»å为 1ï¼ä¸æææä¹ã å¦ææ¤ UUID 䏿¯åºäºæ¶é´ç UUIDï¼åæ¤æ¹æ³æåº |
| | | * UnsupportedOperationExceptionã |
| | | * |
| | | * @return æ¤ {@code UUID} çæ¶éåºå |
| | | * |
| | | * @throws UnsupportedOperationException å¦ææ¤ UUID ç version ä¸ä¸º 1 |
| | | */ |
| | | public int clockSequence() throws UnsupportedOperationException |
| | | { |
| | | checkTimeBase(); |
| | | return (int) ((leastSigBits & 0x3FFF000000000000L) >>> 48); |
| | | } |
| | | |
| | | /** |
| | | * 䏿¤ UUID ç¸å
³çèç¹å¼ã |
| | | * |
| | | * <p> |
| | | * 48 ä½çèç¹å¼æ ¹æ®æ¤ UUID ç node åæ®µæé ãæ¤åæ®µæ¨å¨ç¨äºä¿åæºå¨ç IEEE 802 å°åï¼è¯¥å°åç¨äºçææ¤ UUID 以ä¿è¯ç©ºé´å¯ä¸æ§ã |
| | | * <p> |
| | | * èç¹å¼ä»
å¨åºäºæ¶é´ç UUIDï¼å
¶ version ç±»å为 1ï¼ä¸æææä¹ã<br> |
| | | * å¦ææ¤ UUID 䏿¯åºäºæ¶é´ç UUIDï¼åæ¤æ¹æ³æåº UnsupportedOperationExceptionã |
| | | * |
| | | * @return æ¤ {@code UUID} çèç¹å¼ |
| | | * |
| | | * @throws UnsupportedOperationException å¦ææ¤ UUID ç version ä¸ä¸º 1 |
| | | */ |
| | | public long node() throws UnsupportedOperationException |
| | | { |
| | | checkTimeBase(); |
| | | return leastSigBits & 0x0000FFFFFFFFFFFFL; |
| | | } |
| | | |
| | | /** |
| | | * è¿åæ¤{@code UUID} çå符串表ç°å½¢å¼ã |
| | | * |
| | | * <p> |
| | | * UUID çå符串表示形å¼ç±æ¤ BNF æè¿°ï¼ |
| | | * |
| | | * <pre> |
| | | * {@code |
| | | * UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node> |
| | | * time_low = 4*<hexOctet> |
| | | * time_mid = 2*<hexOctet> |
| | | * time_high_and_version = 2*<hexOctet> |
| | | * variant_and_sequence = 2*<hexOctet> |
| | | * node = 6*<hexOctet> |
| | | * hexOctet = <hexDigit><hexDigit> |
| | | * hexDigit = [0-9a-fA-F] |
| | | * } |
| | | * </pre> |
| | | * |
| | | * </blockquote> |
| | | * |
| | | * @return æ¤{@code UUID} çå符串表ç°å½¢å¼ |
| | | * @see #toString(boolean) |
| | | */ |
| | | @Override |
| | | public String toString() |
| | | { |
| | | return toString(false); |
| | | } |
| | | |
| | | /** |
| | | * è¿åæ¤{@code UUID} çå符串表ç°å½¢å¼ã |
| | | * |
| | | * <p> |
| | | * UUID çå符串表示形å¼ç±æ¤ BNF æè¿°ï¼ |
| | | * |
| | | * <pre> |
| | | * {@code |
| | | * UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node> |
| | | * time_low = 4*<hexOctet> |
| | | * time_mid = 2*<hexOctet> |
| | | * time_high_and_version = 2*<hexOctet> |
| | | * variant_and_sequence = 2*<hexOctet> |
| | | * node = 6*<hexOctet> |
| | | * hexOctet = <hexDigit><hexDigit> |
| | | * hexDigit = [0-9a-fA-F] |
| | | * } |
| | | * </pre> |
| | | * |
| | | * </blockquote> |
| | | * |
| | | * @param isSimple æ¯å¦ç®å模å¼ï¼ç®å模å¼ä¸ºä¸å¸¦'-'çUUIDå符串 |
| | | * @return æ¤{@code UUID} çå符串表ç°å½¢å¼ |
| | | */ |
| | | public String toString(boolean isSimple) |
| | | { |
| | | final StringBuilder builder = new StringBuilder(isSimple ? 32 : 36); |
| | | // time_low |
| | | builder.append(digits(mostSigBits >> 32, 8)); |
| | | if (!isSimple) |
| | | { |
| | | builder.append('-'); |
| | | } |
| | | // time_mid |
| | | builder.append(digits(mostSigBits >> 16, 4)); |
| | | if (!isSimple) |
| | | { |
| | | builder.append('-'); |
| | | } |
| | | // time_high_and_version |
| | | builder.append(digits(mostSigBits, 4)); |
| | | if (!isSimple) |
| | | { |
| | | builder.append('-'); |
| | | } |
| | | // variant_and_sequence |
| | | builder.append(digits(leastSigBits >> 48, 4)); |
| | | if (!isSimple) |
| | | { |
| | | builder.append('-'); |
| | | } |
| | | // node |
| | | builder.append(digits(leastSigBits, 12)); |
| | | |
| | | return builder.toString(); |
| | | } |
| | | |
| | | /** |
| | | * è¿åæ¤ UUID çåå¸ç ã |
| | | * |
| | | * @return UUID çåå¸ç å¼ã |
| | | */ |
| | | @Override |
| | | public int hashCode() |
| | | { |
| | | long hilo = mostSigBits ^ leastSigBits; |
| | | return ((int) (hilo >> 32)) ^ (int) hilo; |
| | | } |
| | | |
| | | /** |
| | | * å°æ¤å¯¹è±¡ä¸æå®å¯¹è±¡æ¯è¾ã |
| | | * <p> |
| | | * å½ä¸ä»
å½åæ°ä¸ä¸º {@code null}ãèæ¯ä¸ä¸ª UUID 对象ãå
·æä¸æ¤ UUID ç¸åç varriantãå
å«ç¸åçå¼ï¼æ¯ä¸ä½åç¸åï¼æ¶ï¼ç»ææä¸º {@code true}ã |
| | | * |
| | | * @param obj è¦ä¸ä¹æ¯è¾ç对象 |
| | | * |
| | | * @return å¦æå¯¹è±¡ç¸åï¼åè¿å {@code true}ï¼å¦åè¿å {@code false} |
| | | */ |
| | | @Override |
| | | public boolean equals(Object obj) |
| | | { |
| | | if ((null == obj) || (obj.getClass() != UUID.class)) |
| | | { |
| | | return false; |
| | | } |
| | | UUID id = (UUID) obj; |
| | | return (mostSigBits == id.mostSigBits && leastSigBits == id.leastSigBits); |
| | | } |
| | | |
| | | // Comparison Operations |
| | | |
| | | /** |
| | | * å°æ¤ UUID 䏿å®ç UUID æ¯è¾ã |
| | | * |
| | | * <p> |
| | | * å¦æä¸¤ä¸ª UUID ä¸åï¼ä¸ç¬¬ä¸ä¸ª UUID çæé«ææå段大äºç¬¬äºä¸ª UUID ç对åºå段ï¼å第ä¸ä¸ª UUID 大äºç¬¬äºä¸ª UUIDã |
| | | * |
| | | * @param val 䏿¤ UUID æ¯è¾ç UUID |
| | | * |
| | | * @return 卿¤ UUID å°äºãçäºæå¤§äº val æ¶ï¼åå«è¿å -1ã0 æ 1ã |
| | | * |
| | | */ |
| | | @Override |
| | | public int compareTo(UUID val) |
| | | { |
| | | // The ordering is intentionally set up so that the UUIDs |
| | | // can simply be numerically compared as two numbers |
| | | return (this.mostSigBits < val.mostSigBits ? -1 : // |
| | | (this.mostSigBits > val.mostSigBits ? 1 : // |
| | | (this.leastSigBits < val.leastSigBits ? -1 : // |
| | | (this.leastSigBits > val.leastSigBits ? 1 : // |
| | | 0)))); |
| | | } |
| | | |
| | | // ------------------------------------------------------------------------------------------------------------------- |
| | | // Private method start |
| | | /** |
| | | * è¿åæå®æ°å对åºçhexå¼ |
| | | * |
| | | * @param val å¼ |
| | | * @param digits ä½ |
| | | * @return å¼ |
| | | */ |
| | | private static String digits(long val, int digits) |
| | | { |
| | | long hi = 1L << (digits * 4); |
| | | return Long.toHexString(hi | (val & (hi - 1))).substring(1); |
| | | } |
| | | |
| | | /** |
| | | * æ£æ¥æ¯å¦ä¸ºtime-basedçæ¬UUID |
| | | */ |
| | | private void checkTimeBase() |
| | | { |
| | | if (version() != 1) |
| | | { |
| | | throw new UnsupportedOperationException("Not a time-based UUID"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·å{@link SecureRandom}ï¼ç±»æä¾å å¯çå¼ºéæºæ°çæå¨ (RNG) |
| | | * |
| | | * @return {@link SecureRandom} |
| | | */ |
| | | public static SecureRandom getSecureRandom() |
| | | { |
| | | try |
| | | { |
| | | return SecureRandom.getInstance("SHA1PRNG"); |
| | | } |
| | | catch (NoSuchAlgorithmException e) |
| | | { |
| | | throw new UtilException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åéæºæ°çæå¨å¯¹è±¡<br> |
| | | * ThreadLocalRandomæ¯JDK 7ä¹åæä¾å¹¶å产çéæºæ°ï¼è½å¤è§£å³å¤ä¸ªçº¿ç¨åççç«äºäºå¤ºã |
| | | * |
| | | * @return {@link ThreadLocalRandom} |
| | | */ |
| | | public static ThreadLocalRandom getRandom() |
| | | { |
| | | return ThreadLocalRandom.current(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.web.controller; |
| | | |
| | | import java.beans.PropertyEditorSupport; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.web.bind.WebDataBinder; |
| | | import org.springframework.web.bind.annotation.InitBinder; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.terra.common.constant.HttpStatus; |
| | | import com.terra.common.utils.DateUtils; |
| | | import com.terra.common.utils.PageUtils; |
| | | import com.terra.common.utils.StringUtils; |
| | | import com.terra.common.utils.sql.SqlUtil; |
| | | import com.terra.common.web.domain.AjaxResult; |
| | | import com.terra.common.web.page.PageDomain; |
| | | import com.terra.common.web.page.TableDataInfo; |
| | | import com.terra.common.web.page.TableSupport; |
| | | |
| | | /** |
| | | * webå±éç¨æ°æ®å¤ç |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class BaseController |
| | | { |
| | | protected final Logger logger = LoggerFactory.getLogger(this.getClass()); |
| | | |
| | | /** |
| | | * å°åå°ä¼ éè¿æ¥çæ¥ææ ¼å¼çå符串ï¼èªå¨è½¬å为Dateç±»å |
| | | */ |
| | | @InitBinder |
| | | public void initBinder(WebDataBinder binder) |
| | | { |
| | | // Date ç±»åè½¬æ¢ |
| | | binder.registerCustomEditor(Date.class, new PropertyEditorSupport() |
| | | { |
| | | @Override |
| | | public void setAsText(String text) |
| | | { |
| | | setValue(DateUtils.parseDate(text)); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 设置请æ±åé¡µæ°æ® |
| | | */ |
| | | protected void startPage() |
| | | { |
| | | PageUtils.startPage(); |
| | | } |
| | | |
| | | /** |
| | | * è®¾ç½®è¯·æ±æåºæ°æ® |
| | | */ |
| | | protected void startOrderBy() |
| | | { |
| | | PageDomain pageDomain = TableSupport.buildPageRequest(); |
| | | if (StringUtils.isNotEmpty(pageDomain.getOrderBy())) |
| | | { |
| | | String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy()); |
| | | PageHelper.orderBy(orderBy); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¸
çå页ç线ç¨åé |
| | | */ |
| | | protected void clearPage() |
| | | { |
| | | PageUtils.clearPage(); |
| | | } |
| | | |
| | | /** |
| | | * ååºè¯·æ±åé¡µæ°æ® |
| | | */ |
| | | @SuppressWarnings({ "rawtypes", "unchecked" }) |
| | | protected TableDataInfo getDataTable(List<?> list) |
| | | { |
| | | TableDataInfo rspData = new TableDataInfo(); |
| | | rspData.setCode(HttpStatus.SUCCESS); |
| | | rspData.setRows(list); |
| | | rspData.setMsg("æ¥è¯¢æå"); |
| | | rspData.setTotal(new PageInfo(list).getTotal()); |
| | | return rspData; |
| | | } |
| | | |
| | | /** |
| | | * è¿åæå |
| | | */ |
| | | public AjaxResult success() |
| | | { |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | /** |
| | | * è¿åæåæ¶æ¯ |
| | | */ |
| | | public AjaxResult success(String message) |
| | | { |
| | | return AjaxResult.success(message); |
| | | } |
| | | |
| | | /** |
| | | * è¿åæåæ¶æ¯ |
| | | */ |
| | | public AjaxResult success(Object data) |
| | | { |
| | | return AjaxResult.success(data); |
| | | } |
| | | |
| | | /** |
| | | * è¿åå¤±è´¥æ¶æ¯ |
| | | */ |
| | | public AjaxResult error() |
| | | { |
| | | return AjaxResult.error(); |
| | | } |
| | | |
| | | /** |
| | | * è¿åå¤±è´¥æ¶æ¯ |
| | | */ |
| | | public AjaxResult error(String message) |
| | | { |
| | | return AjaxResult.error(message); |
| | | } |
| | | |
| | | /** |
| | | * è¿åè¦åæ¶æ¯ |
| | | */ |
| | | public AjaxResult warn(String message) |
| | | { |
| | | return AjaxResult.warn(message); |
| | | } |
| | | |
| | | /** |
| | | * ååºè¿åç»æ |
| | | * |
| | | * @param rows å½±åè¡æ° |
| | | * @return æä½ç»æ |
| | | */ |
| | | protected AjaxResult toAjax(int rows) |
| | | { |
| | | return rows > 0 ? AjaxResult.success() : AjaxResult.error(); |
| | | } |
| | | |
| | | /** |
| | | * ååºè¿åç»æ |
| | | * |
| | | * @param result ç»æ |
| | | * @return æä½ç»æ |
| | | */ |
| | | protected AjaxResult toAjax(boolean result) |
| | | { |
| | | return result ? success() : error(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.web.domain; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Objects; |
| | | import com.terra.common.constant.HttpStatus; |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * æä½æ¶æ¯æé |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class AjaxResult extends HashMap<String, Object> |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** ç¶æç */ |
| | | public static final String CODE_TAG = "code"; |
| | | |
| | | /** è¿åå
容 */ |
| | | public static final String MSG_TAG = "msg"; |
| | | |
| | | /** æ°æ®å¯¹è±¡ */ |
| | | public static final String DATA_TAG = "data"; |
| | | |
| | | /** |
| | | * åå§åä¸ä¸ªæ°å建ç AjaxResult 对象ï¼ä½¿å
¶è¡¨ç¤ºä¸ä¸ªç©ºæ¶æ¯ã |
| | | */ |
| | | public AjaxResult() |
| | | { |
| | | } |
| | | |
| | | /** |
| | | * åå§åä¸ä¸ªæ°å建ç AjaxResult 对象 |
| | | * |
| | | * @param code ç¶æç |
| | | * @param msg è¿åå
容 |
| | | */ |
| | | public AjaxResult(int code, String msg) |
| | | { |
| | | super.put(CODE_TAG, code); |
| | | super.put(MSG_TAG, msg); |
| | | } |
| | | |
| | | /** |
| | | * åå§åä¸ä¸ªæ°å建ç AjaxResult 对象 |
| | | * |
| | | * @param code ç¶æç |
| | | * @param msg è¿åå
容 |
| | | * @param data æ°æ®å¯¹è±¡ |
| | | */ |
| | | public AjaxResult(int code, String msg, Object data) |
| | | { |
| | | super.put(CODE_TAG, code); |
| | | super.put(MSG_TAG, msg); |
| | | if (StringUtils.isNotNull(data)) |
| | | { |
| | | super.put(DATA_TAG, data); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è¿åæåæ¶æ¯ |
| | | * |
| | | * @return æåæ¶æ¯ |
| | | */ |
| | | public static AjaxResult success() |
| | | { |
| | | return AjaxResult.success("æä½æå"); |
| | | } |
| | | |
| | | /** |
| | | * è¿åæåæ°æ® |
| | | * |
| | | * @return æåæ¶æ¯ |
| | | */ |
| | | public static AjaxResult success(Object data) |
| | | { |
| | | return AjaxResult.success("æä½æå", data); |
| | | } |
| | | |
| | | /** |
| | | * è¿åæåæ¶æ¯ |
| | | * |
| | | * @param msg è¿åå
容 |
| | | * @return æåæ¶æ¯ |
| | | */ |
| | | public static AjaxResult success(String msg) |
| | | { |
| | | return AjaxResult.success(msg, null); |
| | | } |
| | | |
| | | /** |
| | | * è¿åæåæ¶æ¯ |
| | | * |
| | | * @param msg è¿åå
容 |
| | | * @param data æ°æ®å¯¹è±¡ |
| | | * @return æåæ¶æ¯ |
| | | */ |
| | | public static AjaxResult success(String msg, Object data) |
| | | { |
| | | return new AjaxResult(HttpStatus.SUCCESS, msg, data); |
| | | } |
| | | |
| | | /** |
| | | * è¿åè¦åæ¶æ¯ |
| | | * |
| | | * @param msg è¿åå
容 |
| | | * @return è¦åæ¶æ¯ |
| | | */ |
| | | public static AjaxResult warn(String msg) |
| | | { |
| | | return AjaxResult.warn(msg, null); |
| | | } |
| | | |
| | | /** |
| | | * è¿åè¦åæ¶æ¯ |
| | | * |
| | | * @param msg è¿åå
容 |
| | | * @param data æ°æ®å¯¹è±¡ |
| | | * @return è¦åæ¶æ¯ |
| | | */ |
| | | public static AjaxResult warn(String msg, Object data) |
| | | { |
| | | return new AjaxResult(HttpStatus.WARN, msg, data); |
| | | } |
| | | |
| | | /** |
| | | * è¿åéè¯¯æ¶æ¯ |
| | | * |
| | | * @return éè¯¯æ¶æ¯ |
| | | */ |
| | | public static AjaxResult error() |
| | | { |
| | | return AjaxResult.error("æä½å¤±è´¥"); |
| | | } |
| | | |
| | | /** |
| | | * è¿åéè¯¯æ¶æ¯ |
| | | * |
| | | * @param msg è¿åå
容 |
| | | * @return éè¯¯æ¶æ¯ |
| | | */ |
| | | public static AjaxResult error(String msg) |
| | | { |
| | | return AjaxResult.error(msg, null); |
| | | } |
| | | |
| | | /** |
| | | * è¿åéè¯¯æ¶æ¯ |
| | | * |
| | | * @param msg è¿åå
容 |
| | | * @param data æ°æ®å¯¹è±¡ |
| | | * @return éè¯¯æ¶æ¯ |
| | | */ |
| | | public static AjaxResult error(String msg, Object data) |
| | | { |
| | | return new AjaxResult(HttpStatus.ERROR, msg, data); |
| | | } |
| | | |
| | | /** |
| | | * è¿åéè¯¯æ¶æ¯ |
| | | * |
| | | * @param code ç¶æç |
| | | * @param msg è¿åå
容 |
| | | * @return éè¯¯æ¶æ¯ |
| | | */ |
| | | public static AjaxResult error(int code, String msg) |
| | | { |
| | | return new AjaxResult(code, msg, null); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦ä¸ºæåæ¶æ¯ |
| | | * |
| | | * @return ç»æ |
| | | */ |
| | | public boolean isSuccess() |
| | | { |
| | | return Objects.equals(HttpStatus.SUCCESS, this.get(CODE_TAG)); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦ä¸ºè¦åæ¶æ¯ |
| | | * |
| | | * @return ç»æ |
| | | */ |
| | | public boolean isWarn() |
| | | { |
| | | return Objects.equals(HttpStatus.WARN, this.get(CODE_TAG)); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦ä¸ºéè¯¯æ¶æ¯ |
| | | * |
| | | * @return ç»æ |
| | | */ |
| | | public boolean isError() |
| | | { |
| | | return Objects.equals(HttpStatus.ERROR, this.get(CODE_TAG)); |
| | | } |
| | | |
| | | /** |
| | | * æ¹ä¾¿é¾å¼è°ç¨ |
| | | * |
| | | * @param key |
| | | * @param value |
| | | * @return |
| | | */ |
| | | @Override |
| | | public AjaxResult put(String key, Object value) |
| | | { |
| | | super.put(key, value); |
| | | return this; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.web.domain; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import com.fasterxml.jackson.annotation.JsonFormat; |
| | | import com.fasterxml.jackson.annotation.JsonIgnore; |
| | | import com.fasterxml.jackson.annotation.JsonInclude; |
| | | |
| | | /** |
| | | * Entityåºç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class BaseEntity implements Serializable |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** æç´¢å¼ */ |
| | | @JsonIgnore |
| | | private String searchValue; |
| | | |
| | | /** å建è
*/ |
| | | private String createBy; |
| | | |
| | | /** å建æ¶é´ */ |
| | | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
| | | private Date createTime; |
| | | |
| | | /** æ´æ°è
*/ |
| | | private String updateBy; |
| | | |
| | | /** æ´æ°æ¶é´ */ |
| | | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
| | | private Date updateTime; |
| | | |
| | | /** 夿³¨ */ |
| | | private String remark; |
| | | |
| | | /** 请æ±åæ° */ |
| | | @JsonInclude(JsonInclude.Include.NON_EMPTY) |
| | | private Map<String, Object> params; |
| | | |
| | | public String getSearchValue() |
| | | { |
| | | return searchValue; |
| | | } |
| | | |
| | | public void setSearchValue(String searchValue) |
| | | { |
| | | this.searchValue = searchValue; |
| | | } |
| | | |
| | | public String getCreateBy() |
| | | { |
| | | return createBy; |
| | | } |
| | | |
| | | public void setCreateBy(String createBy) |
| | | { |
| | | this.createBy = createBy; |
| | | } |
| | | |
| | | public Date getCreateTime() |
| | | { |
| | | return createTime; |
| | | } |
| | | |
| | | public void setCreateTime(Date createTime) |
| | | { |
| | | this.createTime = createTime; |
| | | } |
| | | |
| | | public String getUpdateBy() |
| | | { |
| | | return updateBy; |
| | | } |
| | | |
| | | public void setUpdateBy(String updateBy) |
| | | { |
| | | this.updateBy = updateBy; |
| | | } |
| | | |
| | | public Date getUpdateTime() |
| | | { |
| | | return updateTime; |
| | | } |
| | | |
| | | public void setUpdateTime(Date updateTime) |
| | | { |
| | | this.updateTime = updateTime; |
| | | } |
| | | |
| | | public String getRemark() |
| | | { |
| | | return remark; |
| | | } |
| | | |
| | | public void setRemark(String remark) |
| | | { |
| | | this.remark = remark; |
| | | } |
| | | |
| | | public Map<String, Object> getParams() |
| | | { |
| | | if (params == null) |
| | | { |
| | | params = new HashMap<>(); |
| | | } |
| | | return params; |
| | | } |
| | | |
| | | public void setParams(Map<String, Object> params) |
| | | { |
| | | this.params = params; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.web.domain; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * Treeåºç±» |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class TreeEntity extends BaseEntity |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** ç¶èååç§° */ |
| | | private String parentName; |
| | | |
| | | /** ç¶èåID */ |
| | | private Long parentId; |
| | | |
| | | /** æ¾ç¤ºé¡ºåº */ |
| | | private Integer orderNum; |
| | | |
| | | /** ç¥çº§å表 */ |
| | | private String ancestors; |
| | | |
| | | /** åé¨é¨ */ |
| | | private List<?> children = new ArrayList<>(); |
| | | |
| | | public String getParentName() |
| | | { |
| | | return parentName; |
| | | } |
| | | |
| | | public void setParentName(String parentName) |
| | | { |
| | | this.parentName = parentName; |
| | | } |
| | | |
| | | public Long getParentId() |
| | | { |
| | | return parentId; |
| | | } |
| | | |
| | | public void setParentId(Long parentId) |
| | | { |
| | | this.parentId = parentId; |
| | | } |
| | | |
| | | public Integer getOrderNum() |
| | | { |
| | | return orderNum; |
| | | } |
| | | |
| | | public void setOrderNum(Integer orderNum) |
| | | { |
| | | this.orderNum = orderNum; |
| | | } |
| | | |
| | | public String getAncestors() |
| | | { |
| | | return ancestors; |
| | | } |
| | | |
| | | public void setAncestors(String ancestors) |
| | | { |
| | | this.ancestors = ancestors; |
| | | } |
| | | |
| | | public List<?> getChildren() |
| | | { |
| | | return children; |
| | | } |
| | | |
| | | public void setChildren(List<?> children) |
| | | { |
| | | this.children = children; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.web.page; |
| | | |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * åé¡µæ°æ® |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class PageDomain |
| | | { |
| | | /** å½åè®°å½èµ·å§ç´¢å¼ */ |
| | | private Integer pageNum; |
| | | |
| | | /** æ¯é¡µæ¾ç¤ºè®°å½æ° */ |
| | | private Integer pageSize; |
| | | |
| | | /** æåºå */ |
| | | private String orderByColumn; |
| | | |
| | | /** æåºçæ¹ådescæè
asc */ |
| | | private String isAsc = "asc"; |
| | | |
| | | /** å页忰åçå */ |
| | | private Boolean reasonable = true; |
| | | |
| | | public String getOrderBy() |
| | | { |
| | | if (StringUtils.isEmpty(orderByColumn)) |
| | | { |
| | | return ""; |
| | | } |
| | | return StringUtils.toUnderScoreCase(orderByColumn) + " " + isAsc; |
| | | } |
| | | |
| | | public Integer getPageNum() |
| | | { |
| | | return pageNum; |
| | | } |
| | | |
| | | public void setPageNum(Integer pageNum) |
| | | { |
| | | this.pageNum = pageNum; |
| | | } |
| | | |
| | | public Integer getPageSize() |
| | | { |
| | | return pageSize; |
| | | } |
| | | |
| | | public void setPageSize(Integer pageSize) |
| | | { |
| | | this.pageSize = pageSize; |
| | | } |
| | | |
| | | public String getOrderByColumn() |
| | | { |
| | | return orderByColumn; |
| | | } |
| | | |
| | | public void setOrderByColumn(String orderByColumn) |
| | | { |
| | | this.orderByColumn = orderByColumn; |
| | | } |
| | | |
| | | public String getIsAsc() |
| | | { |
| | | return isAsc; |
| | | } |
| | | |
| | | public void setIsAsc(String isAsc) |
| | | { |
| | | if (StringUtils.isNotEmpty(isAsc)) |
| | | { |
| | | // å
¼å®¹å端æåºç±»å |
| | | if ("ascending".equals(isAsc)) |
| | | { |
| | | isAsc = "asc"; |
| | | } |
| | | else if ("descending".equals(isAsc)) |
| | | { |
| | | isAsc = "desc"; |
| | | } |
| | | this.isAsc = isAsc; |
| | | } |
| | | } |
| | | |
| | | public Boolean getReasonable() |
| | | { |
| | | if (StringUtils.isNull(reasonable)) |
| | | { |
| | | return Boolean.TRUE; |
| | | } |
| | | return reasonable; |
| | | } |
| | | |
| | | public void setReasonable(Boolean reasonable) |
| | | { |
| | | this.reasonable = reasonable; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.web.page; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * è¡¨æ ¼åé¡µæ°æ®å¯¹è±¡ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class TableDataInfo implements Serializable |
| | | { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** æ»è®°å½æ° */ |
| | | private long total; |
| | | |
| | | /** åè¡¨æ°æ® */ |
| | | private List<?> rows; |
| | | |
| | | /** æ¶æ¯ç¶æç */ |
| | | private int code; |
| | | |
| | | /** æ¶æ¯å
容 */ |
| | | private String msg; |
| | | |
| | | /** |
| | | * è¡¨æ ¼æ°æ®å¯¹è±¡ |
| | | */ |
| | | public TableDataInfo() |
| | | { |
| | | } |
| | | |
| | | /** |
| | | * å页 |
| | | * |
| | | * @param list åè¡¨æ°æ® |
| | | * @param total æ»è®°å½æ° |
| | | */ |
| | | public TableDataInfo(List<?> list, long total) |
| | | { |
| | | this.rows = list; |
| | | this.total = total; |
| | | } |
| | | |
| | | public long getTotal() |
| | | { |
| | | return total; |
| | | } |
| | | |
| | | public void setTotal(long total) |
| | | { |
| | | this.total = total; |
| | | } |
| | | |
| | | public List<?> getRows() |
| | | { |
| | | return rows; |
| | | } |
| | | |
| | | public void setRows(List<?> rows) |
| | | { |
| | | this.rows = rows; |
| | | } |
| | | |
| | | public int getCode() |
| | | { |
| | | return code; |
| | | } |
| | | |
| | | public void setCode(int code) |
| | | { |
| | | this.code = code; |
| | | } |
| | | |
| | | public String getMsg() |
| | | { |
| | | return msg; |
| | | } |
| | | |
| | | public void setMsg(String msg) |
| | | { |
| | | this.msg = msg; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.web.page; |
| | | |
| | | import com.terra.common.text.Convert; |
| | | import com.terra.common.utils.ServletUtils; |
| | | |
| | | /** |
| | | * è¡¨æ ¼æ°æ®å¤ç |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class TableSupport |
| | | { |
| | | /** |
| | | * å½åè®°å½èµ·å§ç´¢å¼ |
| | | */ |
| | | public static final String PAGE_NUM = "pageNum"; |
| | | |
| | | /** |
| | | * æ¯é¡µæ¾ç¤ºè®°å½æ° |
| | | */ |
| | | public static final String PAGE_SIZE = "pageSize"; |
| | | |
| | | /** |
| | | * æåºå |
| | | */ |
| | | public static final String ORDER_BY_COLUMN = "orderByColumn"; |
| | | |
| | | /** |
| | | * æåºçæ¹å "desc" æè
"asc". |
| | | */ |
| | | public static final String IS_ASC = "isAsc"; |
| | | |
| | | /** |
| | | * å页忰åçå |
| | | */ |
| | | public static final String REASONABLE = "reasonable"; |
| | | |
| | | /** |
| | | * å°è£
å页对象 |
| | | */ |
| | | public static PageDomain getPageDomain() |
| | | { |
| | | PageDomain pageDomain = new PageDomain(); |
| | | pageDomain.setPageNum(Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1)); |
| | | pageDomain.setPageSize(Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10)); |
| | | pageDomain.setOrderByColumn(ServletUtils.getParameter(ORDER_BY_COLUMN)); |
| | | pageDomain.setIsAsc(ServletUtils.getParameter(IS_ASC)); |
| | | pageDomain.setReasonable(ServletUtils.getParameterToBool(REASONABLE)); |
| | | return pageDomain; |
| | | } |
| | | |
| | | public static PageDomain buildPageRequest() |
| | | { |
| | | return getPageDomain(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.xss; |
| | | |
| | | import javax.validation.Constraint; |
| | | import javax.validation.Payload; |
| | | import java.lang.annotation.ElementType; |
| | | import java.lang.annotation.Retention; |
| | | import java.lang.annotation.RetentionPolicy; |
| | | import java.lang.annotation.Target; |
| | | |
| | | /** |
| | | * èªå®ä¹xssæ ¡éªæ³¨è§£ |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Target(value = { ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER }) |
| | | @Constraint(validatedBy = { XssValidator.class }) |
| | | public @interface Xss |
| | | { |
| | | String message() |
| | | |
| | | default "ä¸å
许任ä½èæ¬è¿è¡"; |
| | | |
| | | Class<?>[] groups() default {}; |
| | | |
| | | Class<? extends Payload>[] payload() default {}; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.common.xss; |
| | | |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | import javax.validation.ConstraintValidator; |
| | | import javax.validation.ConstraintValidatorContext; |
| | | import com.terra.common.utils.StringUtils; |
| | | |
| | | /** |
| | | * èªå®ä¹xssæ ¡éªæ³¨è§£å®ç° |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class XssValidator implements ConstraintValidator<Xss, String> |
| | | { |
| | | private static final String HTML_PATTERN = "<(\\S*?)[^>]*>.*?|<.*? />"; |
| | | |
| | | @Override |
| | | public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) |
| | | { |
| | | if (StringUtils.isBlank(value)) |
| | | { |
| | | return true; |
| | | } |
| | | return !containsHtml(value); |
| | | } |
| | | |
| | | public static boolean containsHtml(String value) |
| | | { |
| | | StringBuilder sHtml = new StringBuilder(); |
| | | Pattern pattern = Pattern.compile(HTML_PATTERN); |
| | | Matcher matcher = pattern.matcher(value); |
| | | while (matcher.find()) |
| | | { |
| | | sHtml.append(matcher.group()); |
| | | } |
| | | return pattern.matcher(sHtml).matches(); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | com.terra.common.utils.SpringUtils |