ret) {
+ return R.SUCCESS == ret.getCode();
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/enums/UserStatus.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/enums/UserStatus.java
index 9ff84960d..372ebd098 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/enums/UserStatus.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/enums/UserStatus.java
@@ -2,29 +2,27 @@ package com.ruoyi.common.core.enums;
/**
* 鐢ㄦ埛鐘舵
- *
+ *
* @author ruoyi
*/
-public enum UserStatus
-{
- OK("0", "姝e父"), DISABLE("1", "鍋滅敤"), DELETED("2", "鍒犻櫎");
+public enum UserStatus {
+ OK("0", "姝e父"),
+ DISABLE("1", "鍋滅敤"),
+ DELETED("2", "鍒犻櫎");
- private final String code;
- private final String info;
+ private final String code;
+ private final String info;
- UserStatus(String code, String info)
- {
- this.code = code;
- this.info = info;
- }
+ UserStatus(String code, String info) {
+ this.code = code;
+ this.info = info;
+ }
- public String getCode()
- {
- return code;
- }
+ public String getCode() {
+ return code;
+ }
- public String getInfo()
- {
- return info;
- }
+ public String getInfo() {
+ return info;
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CaptchaException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CaptchaException.java
index 3e0185100..4cd395700 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CaptchaException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CaptchaException.java
@@ -2,15 +2,13 @@ package com.ruoyi.common.core.exception;
/**
* 楠岃瘉鐮侀敊璇紓甯哥被
- *
+ *
* @author ruoyi
*/
-public class CaptchaException extends RuntimeException
-{
- private static final long serialVersionUID = 1L;
+public class CaptchaException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
- public CaptchaException(String msg)
- {
- super(msg);
- }
+ public CaptchaException(String msg) {
+ super(msg);
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CheckedException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CheckedException.java
index 862eefa50..c70eaf842 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CheckedException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/CheckedException.java
@@ -2,30 +2,26 @@ package com.ruoyi.common.core.exception;
/**
* 妫鏌ュ紓甯
- *
+ *
* @author ruoyi
*/
-public class CheckedException extends RuntimeException
-{
- private static final long serialVersionUID = 1L;
+public class CheckedException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
- public CheckedException(String message)
- {
- super(message);
- }
+ public CheckedException(String message) {
+ super(message);
+ }
- public CheckedException(Throwable cause)
- {
- super(cause);
- }
+ public CheckedException(Throwable cause) {
+ super(cause);
+ }
- public CheckedException(String message, Throwable cause)
- {
- super(message, 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);
- }
+ public CheckedException(
+ String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
+ super(message, cause, enableSuppression, writableStackTrace);
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/DemoModeException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/DemoModeException.java
index ac2a19e9a..4ae2b531a 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/DemoModeException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/DemoModeException.java
@@ -2,14 +2,11 @@ package com.ruoyi.common.core.exception;
/**
* 婕旂ず妯″紡寮傚父
- *
+ *
* @author ruoyi
*/
-public class DemoModeException extends RuntimeException
-{
- private static final long serialVersionUID = 1L;
+public class DemoModeException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
- public DemoModeException()
- {
- }
+ public DemoModeException() {}
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/GlobalException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/GlobalException.java
index b55c7cd29..6596cc180 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/GlobalException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/GlobalException.java
@@ -2,57 +2,45 @@ package com.ruoyi.common.core.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;
- }
-}
\ No newline at end of file
+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;
+ }
+}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/InnerAuthException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/InnerAuthException.java
index 8a7abe9f5..0e1a42955 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/InnerAuthException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/InnerAuthException.java
@@ -2,15 +2,13 @@ package com.ruoyi.common.core.exception;
/**
* 鍐呴儴璁よ瘉寮傚父
- *
+ *
* @author ruoyi
*/
-public class InnerAuthException extends RuntimeException
-{
- private static final long serialVersionUID = 1L;
+public class InnerAuthException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
- public InnerAuthException(String message)
- {
- super(message);
- }
+ public InnerAuthException(String message) {
+ super(message);
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/PreAuthorizeException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/PreAuthorizeException.java
index 3d420c415..3f1f4dfca 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/PreAuthorizeException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/PreAuthorizeException.java
@@ -2,14 +2,11 @@ package com.ruoyi.common.core.exception;
/**
* 鏉冮檺寮傚父
- *
+ *
* @author ruoyi
*/
-public class PreAuthorizeException extends RuntimeException
-{
- private static final long serialVersionUID = 1L;
+public class PreAuthorizeException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
- public PreAuthorizeException()
- {
- }
+ public PreAuthorizeException() {}
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/ServiceException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/ServiceException.java
index 4983866d5..454a415b6 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/ServiceException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/ServiceException.java
@@ -2,73 +2,57 @@ package com.ruoyi.common.core.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;
- }
-}
\ No newline at end of file
+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;
+ }
+}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/UtilException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/UtilException.java
index a1c16b533..db79f269d 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/UtilException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/UtilException.java
@@ -2,25 +2,21 @@ package com.ruoyi.common.core.exception;
/**
* 宸ュ叿绫诲紓甯
- *
+ *
* @author ruoyi
*/
-public class UtilException extends RuntimeException
-{
- private static final long serialVersionUID = 8247610319171014183L;
+public class UtilException extends RuntimeException {
+ private static final long serialVersionUID = 8247610319171014183L;
- public UtilException(Throwable e)
- {
- super(e.getMessage(), e);
- }
+ public UtilException(Throwable e) {
+ super(e.getMessage(), e);
+ }
- public UtilException(String message)
- {
- super(message);
- }
+ public UtilException(String message) {
+ super(message);
+ }
- public UtilException(String message, Throwable throwable)
- {
- super(message, throwable);
- }
+ public UtilException(String message, Throwable throwable) {
+ super(message, throwable);
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotLoginException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotLoginException.java
index eb6c3ae9e..2eac1f650 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotLoginException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotLoginException.java
@@ -2,15 +2,13 @@ package com.ruoyi.common.core.exception.auth;
/**
* 鏈兘閫氳繃鐨勭櫥褰曡璇佸紓甯
- *
+ *
* @author ruoyi
*/
-public class NotLoginException extends RuntimeException
-{
- private static final long serialVersionUID = 1L;
+public class NotLoginException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
- public NotLoginException(String message)
- {
- super(message);
- }
+ public NotLoginException(String message) {
+ super(message);
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotPermissionException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotPermissionException.java
index a578a42ec..c5e6e3cda 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotPermissionException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotPermissionException.java
@@ -4,20 +4,17 @@ import org.apache.commons.lang3.StringUtils;
/**
* 鏈兘閫氳繃鐨勬潈闄愯璇佸紓甯
- *
+ *
* @author ruoyi
*/
-public class NotPermissionException extends RuntimeException
-{
- private static final long serialVersionUID = 1L;
+public class NotPermissionException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
- public NotPermissionException(String permission)
- {
- super(permission);
- }
+ public NotPermissionException(String permission) {
+ super(permission);
+ }
- public NotPermissionException(String[] permissions)
- {
- super(StringUtils.join(permissions, ","));
- }
+ public NotPermissionException(String[] permissions) {
+ super(StringUtils.join(permissions, ","));
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotRoleException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotRoleException.java
index d07118fba..6d39b9636 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotRoleException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/auth/NotRoleException.java
@@ -4,20 +4,17 @@ import org.apache.commons.lang3.StringUtils;
/**
* 鏈兘閫氳繃鐨勮鑹茶璇佸紓甯
- *
+ *
* @author ruoyi
*/
-public class NotRoleException extends RuntimeException
-{
- private static final long serialVersionUID = 1L;
+public class NotRoleException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
- public NotRoleException(String role)
- {
- super(role);
- }
+ public NotRoleException(String role) {
+ super(role);
+ }
- public NotRoleException(String[] roles)
- {
- super(StringUtils.join(roles, ","));
- }
+ public NotRoleException(String[] roles) {
+ super(StringUtils.join(roles, ","));
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/base/BaseException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/base/BaseException.java
index 3d2fa1052..cd597659d 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/base/BaseException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/base/BaseException.java
@@ -2,78 +2,60 @@ package com.ruoyi.common.core.exception.base;
/**
* 鍩虹寮傚父
- *
+ *
* @author ruoyi
*/
-public class BaseException extends RuntimeException
-{
- private static final long serialVersionUID = 1L;
+public class BaseException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
- /**
- * 鎵灞炴ā鍧
- */
- private String module;
+ /** 鎵灞炴ā鍧 */
+ private String module;
- /**
- * 閿欒鐮
- */
- private String code;
+ /** 閿欒鐮 */
+ private String code;
- /**
- * 閿欒鐮佸搴旂殑鍙傛暟
- */
- private Object[] args;
+ /** 閿欒鐮佸搴旂殑鍙傛暟 */
+ private Object[] args;
- /**
- * 閿欒娑堟伅
- */
- private String defaultMessage;
+ /** 閿欒娑堟伅 */
+ 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, 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 code, Object[] args) {
+ this(module, code, args, null);
+ }
- public BaseException(String module, String defaultMessage)
- {
- this(module, null, null, defaultMessage);
- }
+ 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 code, Object[] args) {
+ this(null, code, args, null);
+ }
- public BaseException(String defaultMessage)
- {
- this(null, null, null, defaultMessage);
- }
+ public BaseException(String defaultMessage) {
+ this(null, null, null, defaultMessage);
+ }
- public String getModule()
- {
- return module;
- }
+ public String getModule() {
+ return module;
+ }
- public String getCode()
- {
- return code;
- }
+ public String getCode() {
+ return code;
+ }
- public Object[] getArgs()
- {
- return args;
- }
+ public Object[] getArgs() {
+ return args;
+ }
- public String getDefaultMessage()
- {
- return defaultMessage;
- }
+ public String getDefaultMessage() {
+ return defaultMessage;
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileException.java
index b19f2d826..ba1c5adf7 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileException.java
@@ -4,16 +4,13 @@ import com.ruoyi.common.core.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);
- }
+public class FileException extends BaseException {
+ private static final long serialVersionUID = 1L;
+ public FileException(String code, Object[] args, String msg) {
+ super("file", code, args, msg);
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileNameLengthLimitExceededException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileNameLengthLimitExceededException.java
index 0962ca5ac..1aacd0092 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileNameLengthLimitExceededException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileNameLengthLimitExceededException.java
@@ -2,15 +2,16 @@ package com.ruoyi.common.core.exception.file;
/**
* 鏂囦欢鍚嶇О瓒呴暱闄愬埗寮傚父绫
- *
+ *
* @author ruoyi
*/
-public class FileNameLengthLimitExceededException extends FileException
-{
- private static final long serialVersionUID = 1L;
+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");
- }
+ public FileNameLengthLimitExceededException(int defaultFileNameLength) {
+ super(
+ "upload.filename.exceed.length",
+ new Object[] {defaultFileNameLength},
+ "the filename is too long");
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileSizeLimitExceededException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileSizeLimitExceededException.java
index f9a66b78c..a1f5740bb 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileSizeLimitExceededException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileSizeLimitExceededException.java
@@ -2,15 +2,13 @@ package com.ruoyi.common.core.exception.file;
/**
* 鏂囦欢鍚嶅ぇ灏忛檺鍒跺紓甯哥被
- *
+ *
* @author ruoyi
*/
-public class FileSizeLimitExceededException extends FileException
-{
- private static final long serialVersionUID = 1L;
+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");
- }
+ public FileSizeLimitExceededException(long defaultMaxSize) {
+ super("upload.exceed.maxSize", new Object[] {defaultMaxSize}, "the filesize is too large");
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileUploadException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileUploadException.java
index c0f57cb5d..9f5d0e43c 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileUploadException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/FileUploadException.java
@@ -5,57 +5,48 @@ import java.io.PrintWriter;
/**
* 鏂囦欢涓婁紶寮傚父绫
- *
+ *
* @author ruoyi
*/
-public class FileUploadException extends Exception
-{
+public class FileUploadException extends Exception {
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 1L;
- private final Throwable cause;
+ private final Throwable cause;
- public FileUploadException()
- {
- this(null, null);
- }
+ public FileUploadException() {
+ this(null, null);
+ }
- public FileUploadException(final String msg)
- {
- this(msg, null);
- }
+ public FileUploadException(final String msg) {
+ this(msg, null);
+ }
- public FileUploadException(String msg, Throwable cause)
- {
- super(msg);
- this.cause = cause;
- }
+ 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(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 void printStackTrace(PrintWriter writer) {
+ super.printStackTrace(writer);
+ if (cause != null) {
+ writer.println("Caused by:");
+ cause.printStackTrace(writer);
}
+ }
- @Override
- public Throwable getCause()
- {
- return cause;
- }
+ @Override
+ public Throwable getCause() {
+ return cause;
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/InvalidExtensionException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/InvalidExtensionException.java
index 2e978da36..7545d9068 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/InvalidExtensionException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/file/InvalidExtensionException.java
@@ -4,77 +4,75 @@ import java.util.Arrays;
/**
* 鏂囦欢涓婁紶 璇紓甯哥被
- *
+ *
* @author ruoyi
*/
-public class InvalidExtensionException extends FileUploadException
-{
- private static final long serialVersionUID = 1L;
+public class InvalidExtensionException extends FileUploadException {
+ private static final long serialVersionUID = 1L;
- private String[] allowedExtension;
- private String extension;
- private String filename;
+ 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 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[] getAllowedExtension() {
+ return allowedExtension;
+ }
- public String getExtension()
- {
- return extension;
- }
+ public String getExtension() {
+ return extension;
+ }
- public String getFilename()
- {
- return filename;
- }
+ public String getFilename() {
+ return filename;
+ }
- public static class InvalidImageExtensionException extends InvalidExtensionException
- {
- private static final long serialVersionUID = 1L;
+ 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 InvalidImageExtensionException(
+ String[] allowedExtension, String extension, String filename) {
+ super(allowedExtension, extension, filename);
}
+ }
- public static class InvalidFlashExtensionException extends InvalidExtensionException
- {
- private static final long serialVersionUID = 1L;
+ 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 InvalidFlashExtensionException(
+ String[] allowedExtension, String extension, String filename) {
+ super(allowedExtension, extension, filename);
}
+ }
- public static class InvalidMediaExtensionException extends InvalidExtensionException
- {
- private static final long serialVersionUID = 1L;
+ 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 InvalidMediaExtensionException(
+ String[] allowedExtension, String extension, String filename) {
+ super(allowedExtension, extension, filename);
}
-
- public static class InvalidVideoExtensionException extends InvalidExtensionException
- {
- private static final long serialVersionUID = 1L;
+ }
+
+ public static class InvalidVideoExtensionException extends InvalidExtensionException {
+ private static final long serialVersionUID = 1L;
- public InvalidVideoExtensionException(String[] allowedExtension, String extension, String filename)
- {
- super(allowedExtension, extension, filename);
- }
+ public InvalidVideoExtensionException(
+ String[] allowedExtension, String extension, String filename) {
+ super(allowedExtension, extension, filename);
}
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/job/TaskException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/job/TaskException.java
index c98397c1e..ed7358b17 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/job/TaskException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/job/TaskException.java
@@ -2,33 +2,33 @@ package com.ruoyi.common.core.exception.job;
/**
* 璁″垝绛栫暐寮傚父
- *
+ *
* @author ruoyi
*/
-public class TaskException extends Exception
-{
- private static final long serialVersionUID = 1L;
+public class TaskException extends Exception {
+ private static final long serialVersionUID = 1L;
- private Code code;
+ private Code code;
- public TaskException(String msg, Code code)
- {
- this(msg, code, null);
- }
+ 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 TaskException(String msg, Code code, Exception nestedEx) {
+ super(msg, nestedEx);
+ this.code = code;
+ }
- public Code getCode()
- {
- return code;
- }
+ public Code getCode() {
+ return code;
+ }
- public enum Code
- {
- TASK_EXISTS, NO_TASK_EXISTS, TASK_ALREADY_STARTED, UNKNOWN, CONFIG_ERROR, TASK_NODE_NOT_AVAILABLE
- }
-}
\ No newline at end of file
+ public enum Code {
+ TASK_EXISTS,
+ NO_TASK_EXISTS,
+ TASK_ALREADY_STARTED,
+ UNKNOWN,
+ CONFIG_ERROR,
+ TASK_NODE_NOT_AVAILABLE
+ }
+}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/CaptchaExpireException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/CaptchaExpireException.java
index 6924d7e54..53b925b1e 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/CaptchaExpireException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/CaptchaExpireException.java
@@ -2,15 +2,13 @@ package com.ruoyi.common.core.exception.user;
/**
* 楠岃瘉鐮佸け鏁堝紓甯哥被
- *
+ *
* @author ruoyi
*/
-public class CaptchaExpireException extends UserException
-{
- private static final long serialVersionUID = 1L;
+public class CaptchaExpireException extends UserException {
+ private static final long serialVersionUID = 1L;
- public CaptchaExpireException()
- {
- super("user.jcaptcha.expire", null);
- }
+ public CaptchaExpireException() {
+ super("user.jcaptcha.expire", null);
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/UserException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/UserException.java
index 5493804cc..79ad950a4 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/UserException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/UserException.java
@@ -4,15 +4,13 @@ import com.ruoyi.common.core.exception.base.BaseException;
/**
* 鐢ㄦ埛淇℃伅寮傚父绫
- *
+ *
* @author ruoyi
*/
-public class UserException extends BaseException
-{
- private static final long serialVersionUID = 1L;
+public class UserException extends BaseException {
+ private static final long serialVersionUID = 1L;
- public UserException(String code, Object[] args)
- {
- super("user", code, args, null);
- }
+ public UserException(String code, Object[] args) {
+ super("user", code, args, null);
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/UserPasswordNotMatchException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/UserPasswordNotMatchException.java
index 7f514a604..d581920e4 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/UserPasswordNotMatchException.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/exception/user/UserPasswordNotMatchException.java
@@ -2,15 +2,13 @@ package com.ruoyi.common.core.exception.user;
/**
* 鐢ㄦ埛瀵嗙爜涓嶆纭垨涓嶇鍚堣鑼冨紓甯哥被
- *
+ *
* @author ruoyi
*/
-public class UserPasswordNotMatchException extends UserException
-{
- private static final long serialVersionUID = 1L;
+public class UserPasswordNotMatchException extends UserException {
+ private static final long serialVersionUID = 1L;
- public UserPasswordNotMatchException()
- {
- super("user.password.not.match", null);
- }
+ public UserPasswordNotMatchException() {
+ super("user.password.not.match", null);
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/CharsetKit.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/CharsetKit.java
index 662f817f7..9d1315eab 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/CharsetKit.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/CharsetKit.java
@@ -1,86 +1,82 @@
package com.ruoyi.common.core.text;
+import com.ruoyi.common.core.utils.StringUtils;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
-import com.ruoyi.common.core.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";
+public class CharsetKit {
+ /** ISO-8859-1 */
+ public static final String ISO_8859_1 = "ISO-8859-1";
- /** 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);
+ /** UTF-8 */
+ public static final String UTF_8 = "UTF-8";
- /**
- * 杞崲涓篊harset瀵硅薄
- *
- * @param charset 瀛楃闆嗭紝涓虹┖鍒欒繑鍥為粯璁ゅ瓧绗﹂泦
- * @return Charset
- */
- public static Charset charset(String charset)
- {
- return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset);
- }
+ /** GBK */
+ public static final String GBK = "GBK";
- /**
- * 杞崲瀛楃涓茬殑瀛楃闆嗙紪鐮
- *
- * @param source 瀛楃涓
- * @param srcCharset 婧愬瓧绗﹂泦锛岄粯璁SO-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));
- }
+ /** 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);
- /**
- * 杞崲瀛楃涓茬殑瀛楃闆嗙紪鐮
- *
- * @param source 瀛楃涓
- * @param srcCharset 婧愬瓧绗﹂泦锛岄粯璁SO-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;
- }
+ /** GBK */
+ public static final Charset CHARSET_GBK = Charset.forName(GBK);
- if (null == destCharset)
- {
- destCharset = StandardCharsets.UTF_8;
- }
+ /**
+ * 杞崲涓篊harset瀵硅薄
+ *
+ * @param charset 瀛楃闆嗭紝涓虹┖鍒欒繑鍥為粯璁ゅ瓧绗﹂泦
+ * @return Charset
+ */
+ public static Charset charset(String charset) {
+ return StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset);
+ }
- if (StringUtils.isEmpty(source) || srcCharset.equals(destCharset))
- {
- return source;
- }
- return new String(source.getBytes(srcCharset), destCharset);
+ /**
+ * 杞崲瀛楃涓茬殑瀛楃闆嗙紪鐮
+ *
+ * @param source 瀛楃涓
+ * @param srcCharset 婧愬瓧绗﹂泦锛岄粯璁SO-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 婧愬瓧绗﹂泦锛岄粯璁SO-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;
}
- /**
- * @return 绯荤粺瀛楃闆嗙紪鐮
- */
- public static String systemCharset()
- {
- return Charset.defaultCharset().name();
+ 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();
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/Convert.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/Convert.java
index 9ccdcf4e9..924454b72 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/Convert.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/Convert.java
@@ -1,5 +1,6 @@
package com.ruoyi.common.core.text;
+import com.ruoyi.common.core.utils.StringUtils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
@@ -7,1012 +8,861 @@ import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import java.util.Set;
-import com.ruoyi.common.core.utils.StringUtils;
/**
* 绫诲瀷杞崲鍣
- *
+ *
* @author ruoyi
*/
-public class Convert
-{
- /**
- * 杞崲涓哄瓧绗︿覆
- * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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();
- }
-
- /**
- * 杞崲涓哄瓧绗︿覆
- * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static String toStr(Object value)
- {
- return toStr(value, null);
- }
-
- /**
- * 杞崲涓哄瓧绗
- * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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);
- }
-
- /**
- * 杞崲涓哄瓧绗
- * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Character toChar(Object value)
- {
- return toChar(value, null);
- }
-
- /**
- * 杞崲涓篵yte
- * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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;
- }
- }
-
- /**
- * 杞崲涓篵yte
- * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Byte toByte(Object value)
- {
- return toByte(value, null);
- }
-
- /**
- * 杞崲涓篠hort
- * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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;
- }
- }
-
- /**
- * 杞崲涓篠hort
- * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Short toShort(Object value)
- {
- return toShort(value, null);
- }
-
- /**
- * 杞崲涓篘umber
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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;
- }
- }
-
- /**
- * 杞崲涓篘umber
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Number toNumber(Object value)
- {
- return toNumber(value, null);
- }
-
- /**
- * 杞崲涓篿nt
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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;
- }
- }
-
- /**
- * 杞崲涓篿nt
- * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Integer toInt(Object value)
- {
- return toInt(value, null);
- }
-
- /**
- * 杞崲涓篒nteger鏁扮粍
- *
- * @param str 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Integer[] toIntArray(String str)
- {
- return toIntArray(",", str);
- }
-
- /**
- * 杞崲涓篖ong鏁扮粍
- *
- * @param str 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Long[] toLongArray(String str)
- {
- return toLongArray(",", str);
- }
-
- /**
- * 杞崲涓篒nteger鏁扮粍
- *
- * @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;
- }
-
- /**
- * 杞崲涓篖ong鏁扮粍
- *
- * @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;
- }
-
- /**
- * 杞崲涓篠tring鏁扮粍
- *
- * @param str 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static String[] toStrArray(String str)
- {
- if (StringUtils.isEmpty(str))
- {
- return new String[] {};
- }
- return toStrArray(",", str);
- }
-
- /**
- * 杞崲涓篠tring鏁扮粍
- *
- * @param split 鍒嗛殧绗
- * @param str 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static String[] toStrArray(String split, String str)
- {
- return str.split(split);
- }
-
- /**
- * 杞崲涓簂ong
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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;
- }
- }
-
- /**
- * 杞崲涓簂ong
- * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Long toLong(Object value)
- {
- return toLong(value, null);
- }
-
- /**
- * 杞崲涓篸ouble
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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;
- }
- }
-
- /**
- * 杞崲涓篸ouble
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Double toDouble(Object value)
- {
- return toDouble(value, null);
- }
-
- /**
- * 杞崲涓篎loat
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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;
- }
- }
-
- /**
- * 杞崲涓篎loat
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Float toFloat(Object value)
- {
- return toFloat(value, null);
- }
-
- /**
- * 杞崲涓篵oolean
- * String鏀寔鐨勫间负锛歵rue銆乫alse銆亂es銆乷k銆乶o銆1銆0銆佹槸銆佸惁, 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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;
- }
- }
-
- /**
- * 杞崲涓篵oolean
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static Boolean toBool(Object value)
- {
- return toBool(value, null);
- }
-
- /**
- * 杞崲涓篍num瀵硅薄
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- *
- * @param clazz Enum鐨凜lass
- * @param value 鍊
- * @param defaultValue 榛樿鍊
- * @return Enum
- */
- public static > E toEnum(Class 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;
- }
- }
-
- /**
- * 杞崲涓篍num瀵硅薄
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- *
- * @param clazz Enum鐨凜lass
- * @param value 鍊
- * @return Enum
- */
- public static > E toEnum(Class clazz, Object value)
- {
- return toEnum(clazz, value, null);
- }
-
- /**
- * 杞崲涓築igInteger
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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;
- }
- }
-
- /**
- * 杞崲涓築igInteger
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static BigInteger toBigInteger(Object value)
- {
- return toBigInteger(value, null);
- }
-
- /**
- * 杞崲涓築igDecimal
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @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;
- }
- }
-
- /**
- * 杞崲涓築igDecimal
- * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
- * 杞崲澶辫触涓嶄細鎶ラ敊
- *
- * @param value 琚浆鎹㈢殑鍊
- * @return 缁撴灉
- */
- public static BigDecimal toBigDecimal(Object value)
- {
- return toBigDecimal(value, null);
- }
-
- /**
- * 灏嗗璞¤浆涓哄瓧绗︿覆
- * 1銆丅yte鏁扮粍鍜孊yteBuffer浼氳杞崲涓哄搴斿瓧绗︿覆鐨勬暟缁 2銆佸璞℃暟缁勪細璋冪敤Arrays.toString鏂规硶
- *
- * @param obj 瀵硅薄
- * @return 瀛楃涓
- */
- public static String utf8Str(Object obj)
- {
- return str(obj, CharsetKit.CHARSET_UTF_8);
- }
-
- /**
- * 灏嗗璞¤浆涓哄瓧绗︿覆
- * 1銆丅yte鏁扮粍鍜孊yteBuffer浼氳杞崲涓哄搴斿瓧绗︿覆鐨勬暟缁 2銆佸璞℃暟缁勪細璋冪敤Arrays.toString鏂规硶
- *
- * @param obj 瀵硅薄
- * @param charsetName 瀛楃闆
- * @return 瀛楃涓
- */
- public static String str(Object obj, String charsetName)
- {
- return str(obj, Charset.forName(charsetName));
- }
-
- /**
- * 灏嗗璞¤浆涓哄瓧绗︿覆
- * 1銆丅yte鏁扮粍鍜孊yteBuffer浼氳杞崲涓哄搴斿瓧绗︿覆鐨勬暟缁 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();
- }
-
- /**
- * 灏哹yte鏁扮粍杞负瀛楃涓
- *
- * @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));
- }
-
- /**
- * 瑙g爜瀛楄妭鐮
- *
- * @param data 瀛楃涓
- * @param charset 瀛楃闆嗭紝濡傛灉姝ゅ瓧娈典负绌猴紝鍒欒В鐮佺殑缁撴灉鍙栧喅浜庡钩鍙
- * @return 瑙g爜鍚庣殑瀛楃涓
- */
- 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 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 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("^鏁$", "闆跺厓鏁");
- }
+public class Convert {
+ /**
+ * 杞崲涓哄瓧绗︿覆
+ * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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();
+ }
+
+ /**
+ * 杞崲涓哄瓧绗︿覆
+ * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static String toStr(Object value) {
+ return toStr(value, null);
+ }
+
+ /**
+ * 杞崲涓哄瓧绗
+ * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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);
+ }
+
+ /**
+ * 杞崲涓哄瓧绗
+ * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Character toChar(Object value) {
+ return toChar(value, null);
+ }
+
+ /**
+ * 杞崲涓篵yte
+ * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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;
+ }
+ }
+
+ /**
+ * 杞崲涓篵yte
+ * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Byte toByte(Object value) {
+ return toByte(value, null);
+ }
+
+ /**
+ * 杞崲涓篠hort
+ * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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;
+ }
+ }
+
+ /**
+ * 杞崲涓篠hort
+ * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Short toShort(Object value) {
+ return toShort(value, null);
+ }
+
+ /**
+ * 杞崲涓篘umber
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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;
+ }
+ }
+
+ /**
+ * 杞崲涓篘umber
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Number toNumber(Object value) {
+ return toNumber(value, null);
+ }
+
+ /**
+ * 杞崲涓篿nt
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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;
+ }
+ }
+
+ /**
+ * 杞崲涓篿nt
+ * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Integer toInt(Object value) {
+ return toInt(value, null);
+ }
+
+ /**
+ * 杞崲涓篒nteger鏁扮粍
+ *
+ * @param str 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Integer[] toIntArray(String str) {
+ return toIntArray(",", str);
+ }
+
+ /**
+ * 杞崲涓篖ong鏁扮粍
+ *
+ * @param str 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Long[] toLongArray(String str) {
+ return toLongArray(",", str);
+ }
+
+ /**
+ * 杞崲涓篒nteger鏁扮粍
+ *
+ * @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;
+ }
+
+ /**
+ * 杞崲涓篖ong鏁扮粍
+ *
+ * @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;
+ }
+
+ /**
+ * 杞崲涓篠tring鏁扮粍
+ *
+ * @param str 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static String[] toStrArray(String str) {
+ if (StringUtils.isEmpty(str)) {
+ return new String[] {};
+ }
+ return toStrArray(",", str);
+ }
+
+ /**
+ * 杞崲涓篠tring鏁扮粍
+ *
+ * @param split 鍒嗛殧绗
+ * @param str 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static String[] toStrArray(String split, String str) {
+ return str.split(split);
+ }
+
+ /**
+ * 杞崲涓簂ong
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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;
+ }
+ }
+
+ /**
+ * 杞崲涓簂ong
+ * 濡傛灉缁欏畾鐨勫间负null锛屾垨鑰呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Long toLong(Object value) {
+ return toLong(value, null);
+ }
+
+ /**
+ * 杞崲涓篸ouble
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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;
+ }
+ }
+
+ /**
+ * 杞崲涓篸ouble
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Double toDouble(Object value) {
+ return toDouble(value, null);
+ }
+
+ /**
+ * 杞崲涓篎loat
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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;
+ }
+ }
+
+ /**
+ * 杞崲涓篎loat
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Float toFloat(Object value) {
+ return toFloat(value, null);
+ }
+
+ /**
+ * 杞崲涓篵oolean
+ * String鏀寔鐨勫间负锛歵rue銆乫alse銆亂es銆乷k銆乶o銆1銆0銆佹槸銆佸惁, 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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;
+ }
+ }
+
+ /**
+ * 杞崲涓篵oolean
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static Boolean toBool(Object value) {
+ return toBool(value, null);
+ }
+
+ /**
+ * 杞崲涓篍num瀵硅薄
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ *
+ * @param clazz Enum鐨凜lass
+ * @param value 鍊
+ * @param defaultValue 榛樿鍊
+ * @return Enum
+ */
+ public static > E toEnum(Class 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;
+ }
+ }
+
+ /**
+ * 杞崲涓篍num瀵硅薄
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ *
+ * @param clazz Enum鐨凜lass
+ * @param value 鍊
+ * @return Enum
+ */
+ public static > E toEnum(Class clazz, Object value) {
+ return toEnum(clazz, value, null);
+ }
+
+ /**
+ * 杞崲涓築igInteger
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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;
+ }
+ }
+
+ /**
+ * 杞崲涓築igInteger
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊null
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static BigInteger toBigInteger(Object value) {
+ return toBigInteger(value, null);
+ }
+
+ /**
+ * 杞崲涓築igDecimal
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @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;
+ }
+ }
+
+ /**
+ * 杞崲涓築igDecimal
+ * 濡傛灉缁欏畾鐨勫间负绌猴紝鎴栬呰浆鎹㈠け璐ワ紝杩斿洖榛樿鍊
+ * 杞崲澶辫触涓嶄細鎶ラ敊
+ *
+ * @param value 琚浆鎹㈢殑鍊
+ * @return 缁撴灉
+ */
+ public static BigDecimal toBigDecimal(Object value) {
+ return toBigDecimal(value, null);
+ }
+
+ /**
+ * 灏嗗璞¤浆涓哄瓧绗︿覆
+ * 1銆丅yte鏁扮粍鍜孊yteBuffer浼氳杞崲涓哄搴斿瓧绗︿覆鐨勬暟缁 2銆佸璞℃暟缁勪細璋冪敤Arrays.toString鏂规硶
+ *
+ * @param obj 瀵硅薄
+ * @return 瀛楃涓
+ */
+ public static String utf8Str(Object obj) {
+ return str(obj, CharsetKit.CHARSET_UTF_8);
+ }
+
+ /**
+ * 灏嗗璞¤浆涓哄瓧绗︿覆
+ * 1銆丅yte鏁扮粍鍜孊yteBuffer浼氳杞崲涓哄搴斿瓧绗︿覆鐨勬暟缁 2銆佸璞℃暟缁勪細璋冪敤Arrays.toString鏂规硶
+ *
+ * @param obj 瀵硅薄
+ * @param charsetName 瀛楃闆
+ * @return 瀛楃涓
+ */
+ public static String str(Object obj, String charsetName) {
+ return str(obj, Charset.forName(charsetName));
+ }
+
+ /**
+ * 灏嗗璞¤浆涓哄瓧绗︿覆
+ * 1銆丅yte鏁扮粍鍜孊yteBuffer浼氳杞崲涓哄搴斿瓧绗︿覆鐨勬暟缁 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();
+ }
+
+ /**
+ * 灏哹yte鏁扮粍杞负瀛楃涓
+ *
+ * @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));
+ }
+
+ /**
+ * 瑙g爜瀛楄妭鐮
+ *
+ * @param data 瀛楃涓
+ * @param charset 瀛楃闆嗭紝濡傛灉姝ゅ瓧娈典负绌猴紝鍒欒В鐮佺殑缁撴灉鍙栧喅浜庡钩鍙
+ * @return 瑙g爜鍚庣殑瀛楃涓
+ */
+ 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 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 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("^鏁$", "闆跺厓鏁");
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/StrFormatter.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/StrFormatter.java
index a7d4d84cc..7ec017385 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/StrFormatter.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/text/StrFormatter.java
@@ -4,89 +4,73 @@ import com.ruoyi.common.core.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 = '}';
+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 = '}';
- /**
- * 鏍煎紡鍖栧瓧绗︿覆
- * 姝ゆ柟娉曞彧鏄畝鍗曞皢鍗犱綅绗 {} 鎸夌収椤哄簭鏇挎崲涓哄弬鏁
- * 濡傛灉鎯宠緭鍑 {} 浣跨敤 \\杞箟 { 鍗冲彲锛屽鏋滄兂杈撳嚭 {} 涔嬪墠鐨 \ 浣跨敤鍙岃浆涔夌 \\\\ 鍗冲彲
- * 渚嬶細
- * 閫氬父浣跨敤锛歠ormat("this is {} for {}", "a", "b") -> this is a for b
- * 杞箟{}锛 format("this is \\{} for {}", "a", "b") -> this is \{} for a
- * 杞箟\锛 format("this is \\\\{} for {}", "a", "b") -> this is \a for b
- *
- * @param strPattern 瀛楃涓叉ā鏉
- * @param argArray 鍙傛暟鍒楄〃
- * @return 缁撴灉
- */
- public static String format(final String strPattern, final Object... argArray)
- {
- if (StringUtils.isEmpty(strPattern) || StringUtils.isEmpty(argArray))
- {
- return strPattern;
- }
- final int strPatternLength = strPattern.length();
+ /**
+ * 鏍煎紡鍖栧瓧绗︿覆
+ * 姝ゆ柟娉曞彧鏄畝鍗曞皢鍗犱綅绗 {} 鎸夌収椤哄簭鏇挎崲涓哄弬鏁
+ * 濡傛灉鎯宠緭鍑 {} 浣跨敤 \\杞箟 { 鍗冲彲锛屽鏋滄兂杈撳嚭 {} 涔嬪墠鐨 \ 浣跨敤鍙岃浆涔夌 \\\\ 鍗冲彲
+ * 渚嬶細
+ * 閫氬父浣跨敤锛歠ormat("this is {} for {}", "a", "b") -> this is a for b
+ * 杞箟{}锛 format("this is \\{} for {}", "a", "b") -> this is \{} for a
+ * 杞箟\锛 format("this is \\\\{} for {}", "a", "b") -> this is \a for b
+ *
+ * @param strPattern 瀛楃涓叉ā鏉
+ * @param argArray 鍙傛暟鍒楄〃
+ * @return 缁撴灉
+ */
+ public static String format(final String strPattern, final Object... argArray) {
+ if (StringUtils.isEmpty(strPattern) || StringUtils.isEmpty(argArray)) {
+ return strPattern;
+ }
+ final int strPatternLength = strPattern.length();
- // 鍒濆鍖栧畾涔夊ソ鐨勯暱搴︿互鑾峰緱鏇村ソ鐨勬ц兘
- StringBuilder sbuf = new StringBuilder(strPatternLength + 50);
+ // 鍒濆鍖栧畾涔夊ソ鐨勯暱搴︿互鑾峰緱鏇村ソ鐨勬ц兘
+ 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
- {
- // 姝e父鍗犱綅绗
- sbuf.append(strPattern, handledPosition, delimIndex);
- sbuf.append(Convert.utf8Str(argArray[argIndex]));
- handledPosition = delimIndex + 2;
- }
- }
+ 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();
}
- // 鍔犲叆鏈鍚庝竴涓崰浣嶇鍚庢墍鏈夌殑瀛楃
- sbuf.append(strPattern, handledPosition, strPattern.length());
-
- 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 {
+ // 姝e父鍗犱綅绗
+ sbuf.append(strPattern, handledPosition, delimIndex);
+ sbuf.append(Convert.utf8Str(argArray[argIndex]));
+ handledPosition = delimIndex + 2;
+ }
+ }
}
+ // 鍔犲叆鏈鍚庝竴涓崰浣嶇鍚庢墍鏈夌殑瀛楃
+ sbuf.append(strPattern, handledPosition, strPattern.length());
+
+ return sbuf.toString();
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/DateUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/DateUtils.java
index ba680b2cb..7c0dadd64 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/DateUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/DateUtils.java
@@ -16,176 +16,139 @@ 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();
- }
-
- /**
- * 鑾峰彇褰撳墠鏃ユ湡, 榛樿鏍煎紡涓簓yyy-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());
- }
+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();
+ }
+
+ /**
+ * 鑾峰彇褰撳墠鏃ユ湡, 榛樿鏍煎紡涓簓yyy-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());
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ExceptionUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ExceptionUtil.java
index f75e55972..b07f21683 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ExceptionUtil.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ExceptionUtil.java
@@ -9,31 +9,24 @@ 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 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);
+ 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);
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JwtUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JwtUtils.java
index abe93c0c0..fedd366c6 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JwtUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/JwtUtils.java
@@ -1,123 +1,114 @@
package com.ruoyi.common.core.utils;
-import java.util.Map;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.constant.TokenConstants;
import com.ruoyi.common.core.text.Convert;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
+import java.util.Map;
/**
* Jwt宸ュ叿绫
*
* @author ruoyi
*/
-public class JwtUtils
-{
- public static String secret = TokenConstants.SECRET;
+public class JwtUtils {
+ public static String secret = TokenConstants.SECRET;
- /**
- * 浠庢暟鎹0鏄庣敓鎴愪护鐗
- *
- * @param claims 鏁版嵁澹版槑
- * @return 浠ょ墝
- */
- public static String createToken(Map claims)
- {
- String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact();
- return token;
- }
+ /**
+ * 浠庢暟鎹0鏄庣敓鎴愪护鐗
+ *
+ * @param claims 鏁版嵁澹版槑
+ * @return 浠ょ墝
+ */
+ public static String createToken(Map 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 鏁版嵁澹版槑
+ */
+ 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 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);
- }
+ /**
+ * 鏍规嵁浠ょ墝鑾峰彇鐢ㄦ埛鏍囪瘑
+ *
+ * @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 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);
- }
+ /**
+ * 鏍规嵁韬唤淇℃伅鑾峰彇鐢ㄦ埛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 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 韬唤淇℃伅
+ * @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), "");
- }
+ /**
+ * 鏍规嵁韬唤淇℃伅鑾峰彇閿
+ *
+ * @param claims 韬唤淇℃伅
+ * @param key 閿
+ * @return 鍊
+ */
+ public static String getValue(Claims claims, String key) {
+ return Convert.toStr(claims.get(key), "");
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/PageUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/PageUtils.java
index 95ee25b33..a9bf9afed 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/PageUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/PageUtils.java
@@ -7,29 +7,22 @@ import com.ruoyi.common.core.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 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();
- }
+ /** 娓呯悊鍒嗛〉鐨勭嚎绋嬪彉閲 */
+ public static void clearPage() {
+ PageHelper.clearPage();
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ServletUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ServletUtils.java
index cda371695..6e3502ef5 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ServletUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ServletUtils.java
@@ -1,5 +1,13 @@
package com.ruoyi.common.core.utils;
+import com.alibaba.fastjson2.JSON;
+import com.ruoyi.common.core.constant.Constants;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.text.Convert;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
@@ -8,10 +16,6 @@ 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;
@@ -21,313 +25,246 @@ 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.ruoyi.common.core.constant.Constants;
-import com.ruoyi.common.core.domain.R;
-import com.ruoyi.common.core.text.Convert;
import reactor.core.publisher.Mono;
/**
* 瀹㈡埛绔伐鍏风被
- *
+ *
* @author ruoyi
*/
-public class ServletUtils
-{
- /**
- * 鑾峰彇String鍙傛暟
- */
- public static String getParameter(String name)
- {
- return getRequest().getParameter(name);
- }
+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);
- }
+ /** 鑾峰彇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) {
+ return Convert.toInt(getRequest().getParameter(name));
+ }
- /**
- * 鑾峰彇Integer鍙傛暟
- */
- public static Integer getParameterToInt(String name, Integer defaultValue)
- {
- return Convert.toInt(getRequest().getParameter(name), defaultValue);
- }
+ /** 鑾峰彇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) {
+ return Convert.toBool(getRequest().getParameter(name));
+ }
- /**
- * 鑾峰彇Boolean鍙傛暟
- */
- public static Boolean getParameterToBool(String name, Boolean defaultValue)
- {
- return Convert.toBool(getRequest().getParameter(name), defaultValue);
- }
+ /** 鑾峰彇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 getParams(ServletRequest request)
- {
- final Map map = request.getParameterMap();
- return Collections.unmodifiableMap(map);
- }
+ /**
+ * 鑾峰緱鎵鏈夎姹傚弬鏁
+ *
+ * @param request 璇锋眰瀵硅薄{@link ServletRequest}
+ * @return Map
+ */
+ public static Map getParams(ServletRequest request) {
+ final Map map = request.getParameterMap();
+ return Collections.unmodifiableMap(map);
+ }
- /**
- * 鑾峰緱鎵鏈夎姹傚弬鏁
- *
- * @param request 璇锋眰瀵硅薄{@link ServletRequest}
- * @return Map
- */
- public static Map getParamMap(ServletRequest request)
- {
- Map params = new HashMap<>();
- for (Map.Entry entry : getParams(request).entrySet())
- {
- params.put(entry.getKey(), StringUtils.join(entry.getValue(), ","));
- }
- return params;
+ /**
+ * 鑾峰緱鎵鏈夎姹傚弬鏁
+ *
+ * @param request 璇锋眰瀵硅薄{@link ServletRequest}
+ * @return Map
+ */
+ public static Map getParamMap(ServletRequest request) {
+ Map params = new HashMap<>();
+ for (Map.Entry 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;
- }
+ /** 鑾峰彇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;
- }
+ /** 鑾峰彇response */
+ public static HttpServletResponse getResponse() {
+ try {
+ return getRequestAttributes().getResponse();
+ } catch (Exception e) {
+ return null;
}
+ }
- /**
- * 鑾峰彇session
- */
- public static HttpSession getSession()
- {
- return getRequest().getSession();
- }
+ /** 鑾峰彇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 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 String getHeader(HttpServletRequest request, String name) {
+ String value = request.getHeader(name);
+ if (StringUtils.isEmpty(value)) {
+ return StringUtils.EMPTY;
}
+ return urlDecode(value);
+ }
- public static Map getHeaders(HttpServletRequest request)
- {
- Map map = new LinkedCaseInsensitiveMap<>();
- Enumeration enumeration = request.getHeaderNames();
- if (enumeration != null)
- {
- while (enumeration.hasMoreElements())
- {
- String key = enumeration.nextElement();
- String value = request.getHeader(key);
- map.put(key, value);
- }
- }
- return map;
+ public static Map getHeaders(HttpServletRequest request) {
+ Map map = new LinkedCaseInsensitiveMap<>();
+ Enumeration 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();
- }
+ /**
+ * 灏嗗瓧绗︿覆娓叉煋鍒板鎴风
+ *
+ * @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();
}
+ }
- /**
- * 鏄惁鏄疉jax寮傛璇锋眰
- *
- * @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");
+ /**
+ * 鏄惁鏄疉jax寮傛璇锋眰
+ *
+ * @param request
+ */
+ public static boolean isAjaxRequest(HttpServletRequest request) {
+ String accept = request.getHeader("accept");
+ if (accept != null && accept.contains("application/json")) {
+ return true;
}
- /**
- * 鍐呭缂栫爜
- *
- * @param str 鍐呭
- * @return 缂栫爜鍚庣殑鍐呭
- */
- public static String urlEncode(String str)
- {
- try
- {
- return URLEncoder.encode(str, Constants.UTF8);
- }
- catch (UnsupportedEncodingException e)
- {
- return StringUtils.EMPTY;
- }
+ String xRequestedWith = request.getHeader("X-Requested-With");
+ if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
+ return true;
}
- /**
- * 鍐呭瑙g爜
- *
- * @param str 鍐呭
- * @return 瑙g爜鍚庣殑鍐呭
- */
- public static String urlDecode(String str)
- {
- try
- {
- return URLDecoder.decode(str, Constants.UTF8);
- }
- catch (UnsupportedEncodingException e)
- {
- return StringUtils.EMPTY;
- }
+ String uri = request.getRequestURI();
+ if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml")) {
+ return true;
}
- /**
- * 璁剧疆webflux妯″瀷鍝嶅簲
- *
- * @param response ServerHttpResponse
- * @param value 鍝嶅簲鍐呭
- * @return Mono
- */
- public static Mono webFluxResponseWriter(ServerHttpResponse response, Object value)
- {
- return webFluxResponseWriter(response, HttpStatus.OK, value, R.FAIL);
- }
+ String ajax = request.getParameter("__ajax");
+ return StringUtils.inStringIgnoreCase(ajax, "json", "xml");
+ }
- /**
- * 璁剧疆webflux妯″瀷鍝嶅簲
- *
- * @param response ServerHttpResponse
- * @param code 鍝嶅簲鐘舵佺爜
- * @param value 鍝嶅簲鍐呭
- * @return Mono
- */
- public static Mono webFluxResponseWriter(ServerHttpResponse response, Object value, int code)
- {
- return webFluxResponseWriter(response, HttpStatus.OK, value, code);
+ /**
+ * 鍐呭缂栫爜
+ *
+ * @param str 鍐呭
+ * @return 缂栫爜鍚庣殑鍐呭
+ */
+ public static String urlEncode(String str) {
+ try {
+ return URLEncoder.encode(str, Constants.UTF8);
+ } catch (UnsupportedEncodingException e) {
+ return StringUtils.EMPTY;
}
+ }
- /**
- * 璁剧疆webflux妯″瀷鍝嶅簲
- *
- * @param response ServerHttpResponse
- * @param status http鐘舵佺爜
- * @param code 鍝嶅簲鐘舵佺爜
- * @param value 鍝嶅簲鍐呭
- * @return Mono
- */
- public static Mono webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code)
- {
- return webFluxResponseWriter(response, MediaType.APPLICATION_JSON_VALUE, status, value, code);
+ /**
+ * 鍐呭瑙g爜
+ *
+ * @param str 鍐呭
+ * @return 瑙g爜鍚庣殑鍐呭
+ */
+ public static String urlDecode(String str) {
+ try {
+ return URLDecoder.decode(str, Constants.UTF8);
+ } catch (UnsupportedEncodingException e) {
+ return StringUtils.EMPTY;
}
+ }
- /**
- * 璁剧疆webflux妯″瀷鍝嶅簲
- *
- * @param response ServerHttpResponse
- * @param contentType content-type
- * @param status http鐘舵佺爜
- * @param code 鍝嶅簲鐘舵佺爜
- * @param value 鍝嶅簲鍐呭
- * @return Mono
- */
- public static Mono 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));
- }
+ /**
+ * 璁剧疆webflux妯″瀷鍝嶅簲
+ *
+ * @param response ServerHttpResponse
+ * @param value 鍝嶅簲鍐呭
+ * @return Mono
+ */
+ public static Mono webFluxResponseWriter(ServerHttpResponse response, Object value) {
+ return webFluxResponseWriter(response, HttpStatus.OK, value, R.FAIL);
+ }
+
+ /**
+ * 璁剧疆webflux妯″瀷鍝嶅簲
+ *
+ * @param response ServerHttpResponse
+ * @param code 鍝嶅簲鐘舵佺爜
+ * @param value 鍝嶅簲鍐呭
+ * @return Mono
+ */
+ public static Mono 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
+ */
+ public static Mono 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
+ */
+ public static Mono 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));
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SpringUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SpringUtils.java
index 9dcbceb10..627da1a27 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SpringUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SpringUtils.java
@@ -9,106 +9,94 @@ import org.springframework.stereotype.Component;
/**
* spring宸ュ叿绫 鏂逛究鍦ㄩ潪spring绠$悊鐜涓幏鍙朾ean
- *
+ *
* @author ruoyi
*/
@Component
-public final class SpringUtils implements BeanFactoryPostProcessor
-{
- /** Spring搴旂敤涓婁笅鏂囩幆澧 */
- private static ConfigurableListableBeanFactory beanFactory;
+public final class SpringUtils implements BeanFactoryPostProcessor {
+ /** Spring搴旂敤涓婁笅鏂囩幆澧 */
+ private static ConfigurableListableBeanFactory beanFactory;
- @Override
- public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
- {
- SpringUtils.beanFactory = 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 getBean(String name) throws BeansException
- {
- return (T) beanFactory.getBean(name);
- }
+ /**
+ * 鑾峰彇瀵硅薄
+ *
+ * @param name
+ * @return Object 涓涓互鎵缁欏悕瀛楁敞鍐岀殑bean鐨勫疄渚
+ * @throws org.springframework.beans.BeansException
+ */
+ @SuppressWarnings("unchecked")
+ public static T getBean(String name) throws BeansException {
+ return (T) beanFactory.getBean(name);
+ }
- /**
- * 鑾峰彇绫诲瀷涓簉equiredType鐨勫璞
- *
- * @param clz
- * @return
- * @throws org.springframework.beans.BeansException
- *
- */
- public static T getBean(Class clz) throws BeansException
- {
- T result = (T) beanFactory.getBean(clz);
- return result;
- }
+ /**
+ * 鑾峰彇绫诲瀷涓簉equiredType鐨勫璞
+ *
+ * @param clz
+ * @return
+ * @throws org.springframework.beans.BeansException
+ */
+ public static T getBean(Class 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);
- }
+ /**
+ * 濡傛灉BeanFactory鍖呭惈涓涓笌鎵缁欏悕绉板尮閰嶇殑bean瀹氫箟锛屽垯杩斿洖true
+ *
+ * @param name
+ * @return boolean
+ */
+ public static boolean containsBean(String name) {
+ return beanFactory.containsBean(name);
+ }
- /**
- * 鍒ゆ柇浠ョ粰瀹氬悕瀛楁敞鍐岀殑bean瀹氫箟鏄竴涓猻ingleton杩樻槸涓涓猵rototype銆 濡傛灉涓庣粰瀹氬悕瀛楃浉搴旂殑bean瀹氫箟娌℃湁琚壘鍒帮紝灏嗕細鎶涘嚭涓涓紓甯革紙NoSuchBeanDefinitionException锛
- *
- * @param name
- * @return boolean
- * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
- *
- */
- public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
- {
- return beanFactory.isSingleton(name);
- }
+ /**
+ * 鍒ゆ柇浠ョ粰瀹氬悕瀛楁敞鍐岀殑bean瀹氫箟鏄竴涓猻ingleton杩樻槸涓涓猵rototype銆
+ * 濡傛灉涓庣粰瀹氬悕瀛楃浉搴旂殑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);
- }
+ /**
+ * @param name
+ * @return Class 娉ㄥ唽瀵硅薄鐨勭被鍨
+ * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
+ */
+ public static Class> getType(String name) throws NoSuchBeanDefinitionException {
+ return beanFactory.getType(name);
+ }
- /**
- * 濡傛灉缁欏畾鐨刡ean鍚嶅瓧鍦╞ean瀹氫箟涓湁鍒悕锛屽垯杩斿洖杩欎簺鍒悕
- *
- * @param name
- * @return
- * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
- *
- */
- public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
- {
- return beanFactory.getAliases(name);
- }
+ /**
+ * 濡傛灉缁欏畾鐨刡ean鍚嶅瓧鍦╞ean瀹氫箟涓湁鍒悕锛屽垯杩斿洖杩欎簺鍒悕
+ *
+ * @param name
+ * @return
+ * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
+ */
+ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
+ return beanFactory.getAliases(name);
+ }
- /**
- * 鑾峰彇aop浠g悊瀵硅薄
- *
- * @param invoker
- * @return
- */
- @SuppressWarnings("unchecked")
- public static T getAopProxy(T invoker)
- {
- return (T) AopContext.currentProxy();
- }
+ /**
+ * 鑾峰彇aop浠g悊瀵硅薄
+ *
+ * @param invoker
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ public static T getAopProxy(T invoker) {
+ return (T) AopContext.currentProxy();
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/StringUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/StringUtils.java
index a27ab625f..b38f31207 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/StringUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/StringUtils.java
@@ -1,633 +1,525 @@
package com.ruoyi.common.core.utils;
+import com.ruoyi.common.core.constant.Constants;
+import com.ruoyi.common.core.text.StrFormatter;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.springframework.util.AntPathMatcher;
-import com.ruoyi.common.core.constant.Constants;
-import com.ruoyi.common.core.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 nvl(T value, T defaultValue)
- {
- return value != null ? value : defaultValue;
- }
-
- /**
- * * 鍒ゆ柇涓涓狢ollection鏄惁涓虹┖锛 鍖呭惈List锛孲et锛孮ueue
- *
- * @param coll 瑕佸垽鏂殑Collection
- * @return true锛氫负绌 false锛氶潪绌
- */
- public static boolean isEmpty(Collection> coll)
- {
- return isNull(coll) || coll.isEmpty();
- }
-
- /**
- * * 鍒ゆ柇涓涓狢ollection鏄惁闈炵┖锛屽寘鍚獿ist锛孲et锛孮ueue
- *
- * @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);
- }
-
- /**
- * * 鍒ゆ柇涓涓狹ap鏄惁涓虹┖
- *
- * @param map 瑕佸垽鏂殑Map
- * @return true锛氫负绌 false锛氶潪绌
- */
- public static boolean isEmpty(Map, ?> map)
- {
- return isNull(map) || map.isEmpty();
- }
-
- /**
- * * 鍒ゆ柇涓涓狹ap鏄惁涓虹┖
- *
- * @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);
- }
-
- /**
- * * 鍒ゆ柇涓涓璞℃槸鍚︽槸鏁扮粍绫诲瀷锛圝ava鍩烘湰鍨嬪埆鐨勬暟缁勶級
- *
- * @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;
- }
+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 nvl(T value, T defaultValue) {
+ return value != null ? value : defaultValue;
+ }
+
+ /**
+ * * 鍒ゆ柇涓涓狢ollection鏄惁涓虹┖锛 鍖呭惈List锛孲et锛孮ueue
+ *
+ * @param coll 瑕佸垽鏂殑Collection
+ * @return true锛氫负绌 false锛氶潪绌
+ */
+ public static boolean isEmpty(Collection> coll) {
+ return isNull(coll) || coll.isEmpty();
+ }
+
+ /**
+ * * 鍒ゆ柇涓涓狢ollection鏄惁闈炵┖锛屽寘鍚獿ist锛孲et锛孮ueue
+ *
+ * @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);
+ }
+
+ /**
+ * * 鍒ゆ柇涓涓狹ap鏄惁涓虹┖
+ *
+ * @param map 瑕佸垽鏂殑Map
+ * @return true锛氫负绌 false锛氶潪绌
+ */
+ public static boolean isEmpty(Map, ?> map) {
+ return isNull(map) || map.isEmpty();
+ }
+
+ /**
+ * * 鍒ゆ柇涓涓狹ap鏄惁涓虹┖
+ *
+ * @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);
+ }
+
+ /**
+ * * 鍒ゆ柇涓涓璞℃槸鍚︽槸鏁扮粍绫诲瀷锛圝ava鍩烘湰鍨嬪埆鐨勬暟缁勶級
+ *
+ * @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 = str.length() + start;
+ }
- if (start < 0)
- {
- start = 0;
- }
- if (start > str.length())
- {
- return NULLSTR;
- }
+ 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;
- }
+ 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 < 0) {
+ end = str.length() + end;
+ }
+ if (start < 0) {
+ start = str.length() + start;
+ }
- if (end > str.length())
- {
- end = str.length();
- }
+ if (end > str.length()) {
+ end = str.length();
+ }
- if (start > end)
- {
- return NULLSTR;
- }
+ if (start > end) {
+ return NULLSTR;
+ }
- if (start < 0)
- {
- start = 0;
- }
- if (end < 0)
- {
- end = 0;
- }
+ 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;
- }
-
- /**
- * 鏍煎紡鍖栨枃鏈, {} 琛ㄧず鍗犱綅绗
- * 姝ゆ柟娉曞彧鏄畝鍗曞皢鍗犱綅绗 {} 鎸夌収椤哄簭鏇挎崲涓哄弬鏁
- * 濡傛灉鎯宠緭鍑 {} 浣跨敤 \\杞箟 { 鍗冲彲锛屽鏋滄兂杈撳嚭 {} 涔嬪墠鐨 \ 浣跨敤鍙岃浆涔夌 \\\\ 鍗冲彲
- * 渚嬶細
- * 閫氬父浣跨敤锛歠ormat("this is {} for {}", "a", "b") -> this is a for b
- * 杞箟{}锛 format("this is \\{} for {}", "a", "b") -> this is \{} for a
- * 杞箟\锛 format("this is \\\\{} for {}", "a", "b") -> this is \a for b
- *
- * @param template 鏂囨湰妯℃澘锛岃鏇挎崲鐨勯儴鍒嗙敤 {} 琛ㄧず
- * @param params 鍙傛暟鍊
- * @return 鏍煎紡鍖栧悗鐨勬枃鏈
- */
- public static String format(String template, Object... params)
- {
- if (isEmpty(params) || isEmpty(template))
- {
- return template;
- }
- return StrFormatter.format(template, params);
- }
-
- /**
- * 鏄惁涓篽ttp(s)://寮澶
- *
- * @param link 閾炬帴
- * @return 缁撴灉
- */
- public static boolean ishttp(String link)
- {
- return StringUtils.startsWithAny(link, Constants.HTTP, Constants.HTTPS);
- }
-
- /**
- * 鍒ゆ柇缁欏畾鐨刢ollection鍒楄〃涓槸鍚﹀寘鍚暟缁刟rray 鍒ゆ柇缁欏畾鐨勬暟缁刟rray涓槸鍚﹀寘鍚粰瀹氱殑鍏冪礌value
- *
- * @param collection 缁欏畾鐨勯泦鍚
- * @param array 缁欏畾鐨勬暟缁
- * @return boolean 缁撴灉
- */
- public static boolean containsAny(Collection collection, String... array)
- {
- if (isEmpty(collection) || isEmpty(array))
- {
- return false;
- }
- else
- {
- for (String str : array)
- {
- if (collection.contains(str))
- {
- return true;
- }
- }
- return false;
- }
+ 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;
+ }
+
+ /**
+ * 鏍煎紡鍖栨枃鏈, {} 琛ㄧず鍗犱綅绗
+ * 姝ゆ柟娉曞彧鏄畝鍗曞皢鍗犱綅绗 {} 鎸夌収椤哄簭鏇挎崲涓哄弬鏁
+ * 濡傛灉鎯宠緭鍑 {} 浣跨敤 \\杞箟 { 鍗冲彲锛屽鏋滄兂杈撳嚭 {} 涔嬪墠鐨 \ 浣跨敤鍙岃浆涔夌 \\\\ 鍗冲彲
+ * 渚嬶細
+ * 閫氬父浣跨敤锛歠ormat("this is {} for {}", "a", "b") -> this is a for b
+ * 杞箟{}锛 format("this is \\{} for {}", "a", "b") -> this is \{} for a
+ * 杞箟\锛 format("this is \\\\{} for {}", "a", "b") -> this is \a for b
+ *
+ * @param template 鏂囨湰妯℃澘锛岃鏇挎崲鐨勯儴鍒嗙敤 {} 琛ㄧず
+ * @param params 鍙傛暟鍊
+ * @return 鏍煎紡鍖栧悗鐨勬枃鏈
+ */
+ public static String format(String template, Object... params) {
+ if (isEmpty(params) || isEmpty(template)) {
+ return template;
+ }
+ return StrFormatter.format(template, params);
+ }
+
+ /**
+ * 鏄惁涓篽ttp(s)://寮澶
+ *
+ * @param link 閾炬帴
+ * @return 缁撴灉
+ */
+ public static boolean ishttp(String link) {
+ return StringUtils.startsWithAny(link, Constants.HTTP, Constants.HTTPS);
+ }
+
+ /**
+ * 鍒ゆ柇缁欏畾鐨刢ollection鍒楄〃涓槸鍚﹀寘鍚暟缁刟rray 鍒ゆ柇缁欏畾鐨勬暟缁刟rray涓槸鍚﹀寘鍚粰瀹氱殑鍏冪礌value
+ *
+ * @param collection 缁欏畾鐨勯泦鍚
+ * @param array 缁欏畾鐨勬暟缁
+ * @return boolean 缁撴灉
+ */
+ public static boolean containsAny(Collection 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));
- }
+ /** 椹煎嘲杞笅鍒掔嚎鍛藉悕 */
+ 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;
- }
-
- /**
- * 灏嗕笅鍒掔嚎澶у啓鏂瑰紡鍛藉悕鐨勫瓧绗︿覆杞崲涓洪┘宄板紡銆傚鏋滆浆鎹㈠墠鐨勪笅鍒掔嚎澶у啓鏂瑰紡鍛藉悕鐨勫瓧绗︿覆涓虹┖锛屽垯杩斿洖绌哄瓧绗︿覆銆 渚嬪锛欻ELLO_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();
- }
-
- /**
- * 椹煎嘲寮忓懡鍚嶆硶
- * 渚嬪锛歶ser_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 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 cast(Object obj)
- {
- return (T) obj;
- }
-
- /**
- * 鏁板瓧宸﹁竟琛ラ綈0锛屼娇涔嬭揪鍒版寚瀹氶暱搴︺傛敞鎰忥紝濡傛灉鏁板瓧杞崲涓哄瓧绗︿覆鍚庯紝闀垮害澶т簬size锛屽垯鍙繚鐣 鏈鍚巗ize涓瓧绗︺
- *
- * @param num 鏁板瓧瀵硅薄
- * @param size 瀛楃涓叉寚瀹氶暱搴
- * @return 杩斿洖鏁板瓧鐨勫瓧绗︿覆鏍煎紡锛岃瀛楃涓蹭负鎸囧畾闀垮害銆
- */
- public static final String padl(final Number num, final int size)
- {
- return padl(num.toString(), size, '0');
- }
-
- /**
- * 瀛楃涓插乏琛ラ綈銆傚鏋滃師濮嬪瓧绗︿覆s闀垮害澶т簬size锛屽垯鍙繚鐣欐渶鍚巗ize涓瓧绗︺
- *
- * @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();
+ 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;
+ }
+
+ /**
+ * 灏嗕笅鍒掔嚎澶у啓鏂瑰紡鍛藉悕鐨勫瓧绗︿覆杞崲涓洪┘宄板紡銆傚鏋滆浆鎹㈠墠鐨勪笅鍒掔嚎澶у啓鏂瑰紡鍛藉悕鐨勫瓧绗︿覆涓虹┖锛屽垯杩斿洖绌哄瓧绗︿覆銆 渚嬪锛欻ELLO_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();
+ }
+
+ /** 椹煎嘲寮忓懡鍚嶆硶 渚嬪锛歶ser_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 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 cast(Object obj) {
+ return (T) obj;
+ }
+
+ /**
+ * 鏁板瓧宸﹁竟琛ラ綈0锛屼娇涔嬭揪鍒版寚瀹氶暱搴︺傛敞鎰忥紝濡傛灉鏁板瓧杞崲涓哄瓧绗︿覆鍚庯紝闀垮害澶т簬size锛屽垯鍙繚鐣 鏈鍚巗ize涓瓧绗︺
+ *
+ * @param num 鏁板瓧瀵硅薄
+ * @param size 瀛楃涓叉寚瀹氶暱搴
+ * @return 杩斿洖鏁板瓧鐨勫瓧绗︿覆鏍煎紡锛岃瀛楃涓蹭负鎸囧畾闀垮害銆
+ */
+ public static final String padl(final Number num, final int size) {
+ return padl(num.toString(), size, '0');
+ }
+
+ /**
+ * 瀛楃涓插乏琛ラ綈銆傚鏋滃師濮嬪瓧绗︿覆s闀垮害澶т簬size锛屽垯鍙繚鐣欐渶鍚巗ize涓瓧绗︺
+ *
+ * @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();
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/bean/BeanUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/bean/BeanUtils.java
index d027ceffa..bf3209167 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/bean/BeanUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/bean/BeanUtils.java
@@ -8,103 +8,89 @@ 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;
+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*)");
+ /** * 鍖归厤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*)");
+ /** * 鍖归厤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();
- }
+ /**
+ * Bean灞炴у鍒跺伐鍏锋柟娉曘
+ *
+ * @param dest 鐩爣瀵硅薄
+ * @param src 婧愬璞
+ */
+ public static void copyBeanProp(Object dest, Object src) {
+ try {
+ copyProperties(src, dest);
+ } catch (Exception e) {
+ e.printStackTrace();
}
+ }
- /**
- * 鑾峰彇瀵硅薄鐨剆etter鏂规硶銆
- *
- * @param obj 瀵硅薄
- * @return 瀵硅薄鐨剆etter鏂规硶鍒楄〃
- */
- public static List getSetterMethods(Object obj)
- {
- // setter鏂规硶鍒楄〃
- List setterMethods = new ArrayList();
+ /**
+ * 鑾峰彇瀵硅薄鐨剆etter鏂规硶銆
+ *
+ * @param obj 瀵硅薄
+ * @return 瀵硅薄鐨剆etter鏂规硶鍒楄〃
+ */
+ public static List getSetterMethods(Object obj) {
+ // setter鏂规硶鍒楄〃
+ List setterMethods = new ArrayList();
- // 鑾峰彇鎵鏈夋柟娉
- Method[] methods = obj.getClass().getMethods();
+ // 鑾峰彇鎵鏈夋柟娉
+ Method[] methods = obj.getClass().getMethods();
- // 鏌ユ壘setter鏂规硶
+ // 鏌ユ壘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;
+ for (Method method : methods) {
+ Matcher m = SET_PATTERN.matcher(method.getName());
+ if (m.matches() && (method.getParameterTypes().length == 1)) {
+ setterMethods.add(method);
+ }
}
+ // 杩斿洖setter鏂规硶鍒楄〃
+ return setterMethods;
+ }
- /**
- * 鑾峰彇瀵硅薄鐨刧etter鏂规硶銆
- *
- * @param obj 瀵硅薄
- * @return 瀵硅薄鐨刧etter鏂规硶鍒楄〃
- */
-
- public static List getGetterMethods(Object obj)
- {
- // getter鏂规硶鍒楄〃
- List getterMethods = new ArrayList();
- // 鑾峰彇鎵鏈夋柟娉
- 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;
+ /**
+ * 鑾峰彇瀵硅薄鐨刧etter鏂规硶銆
+ *
+ * @param obj 瀵硅薄
+ * @return 瀵硅薄鐨刧etter鏂规硶鍒楄〃
+ */
+ public static List getGetterMethods(Object obj) {
+ // getter鏂规硶鍒楄〃
+ List getterMethods = new ArrayList();
+ // 鑾峰彇鎵鏈夋柟娉
+ 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;
+ }
- /**
- * 妫鏌ean鏂规硶鍚嶄腑鐨勫睘鎬у悕鏄惁鐩哥瓑銆
- * 濡俫etName()鍜宻etName()灞炴у悕涓鏍凤紝getName()鍜宻etAge()灞炴у悕涓嶄竴鏍枫
- *
- * @param m1 鏂规硶鍚1
- * @param m2 鏂规硶鍚2
- * @return 灞炴у悕涓鏍疯繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse
- */
-
- public static boolean isMethodPropEquals(String m1, String m2)
- {
- return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
- }
+ /**
+ * 妫鏌ean鏂规硶鍚嶄腑鐨勫睘鎬у悕鏄惁鐩哥瓑銆
+ * 濡俫etName()鍜宻etName()灞炴у悕涓鏍凤紝getName()鍜宻etAge()灞炴у悕涓嶄竴鏍枫
+ *
+ * @param m1 鏂规硶鍚1
+ * @param m2 鏂规硶鍚2
+ * @return 灞炴у悕涓鏍疯繑鍥瀟rue锛屽惁鍒欒繑鍥瀎alse
+ */
+ public static boolean isMethodPropEquals(String m1, String m2) {
+ return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/bean/BeanValidators.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/bean/BeanValidators.java
index 758772620..a61eab62e 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/bean/BeanValidators.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/bean/BeanValidators.java
@@ -1,24 +1,21 @@
package com.ruoyi.common.core.utils.bean;
+import jakarta.validation.ConstraintViolation;
+import jakarta.validation.ConstraintViolationException;
+import jakarta.validation.Validator;
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> constraintViolations = validator.validate(object, groups);
- if (!constraintViolations.isEmpty())
- {
- throw new ConstraintViolationException(constraintViolations);
- }
+public class BeanValidators {
+ public static void validateWithException(Validator validator, Object object, Class>... groups)
+ throws ConstraintViolationException {
+ Set> constraintViolations = validator.validate(object, groups);
+ if (!constraintViolations.isEmpty()) {
+ throw new ConstraintViolationException(constraintViolations);
}
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/FileTypeUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/FileTypeUtils.java
index fa494b50b..d14637590 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/FileTypeUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/FileTypeUtils.java
@@ -11,85 +11,77 @@ import org.springframework.web.multipart.MultipartFile;
*
* @author ruoyi
*/
-public class FileTypeUtils
-{
- /**
- * 鑾峰彇鏂囦欢绫诲瀷
- *
- * 渚嬪: ruoyi.txt, 杩斿洖: txt
- *
- * @param file 鏂囦欢鍚
- * @return 鍚庣紑锛堜笉鍚".")
- */
- public static String getFileType(File file)
- {
- if (null == file)
- {
- return StringUtils.EMPTY;
- }
- return getFileType(file.getName());
+public class FileTypeUtils {
+ /**
+ * 鑾峰彇鏂囦欢绫诲瀷
+ *
+ *
渚嬪: ruoyi.txt, 杩斿洖: txt
+ *
+ * @param file 鏂囦欢鍚
+ * @return 鍚庣紑锛堜笉鍚".")
+ */
+ public static String getFileType(File file) {
+ if (null == file) {
+ return StringUtils.EMPTY;
}
+ return getFileType(file.getName());
+ }
- /**
- * 鑾峰彇鏂囦欢绫诲瀷
- *
- * 渚嬪: 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();
+ /**
+ * 鑾峰彇鏂囦欢绫诲瀷
+ *
+ *
渚嬪: 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 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;
+ /**
+ * 鑾峰彇鏂囦欢绫诲瀷
+ *
+ * @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";
}
-}
\ No newline at end of file
+ return strFileExtendName;
+ }
+}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/FileUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/FileUtils.java
index aabb4fb73..f4ee5f798 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/FileUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/FileUtils.java
@@ -1,5 +1,8 @@
package com.ruoyi.common.core.utils.file;
+import com.ruoyi.common.core.utils.StringUtils;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -8,246 +11,208 @@ 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.ruoyi.common.core.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]+";
-
- /**
- * 杈撳嚭鎸囧畾鏂囦欢鐨刡yte鏁扮粍
- *
- * @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;
+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]+";
+
+ /**
+ * 杈撳嚭鎸囧畾鏂囦欢鐨刡yte鏁扮粍
+ *
+ * @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();
}
- finally
- {
- if (os != null)
- {
- try
- {
- os.close();
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- }
- if (fis != null)
- {
- try
- {
- fis.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 filePath 鏂囦欢
+ * @return
+ */
+ public static boolean deleteFile(String filePath) {
+ boolean flag = false;
+ File file = new File(filePath);
+ // 璺緞涓烘枃浠朵笖涓嶄负绌哄垯杩涜鍒犻櫎
+ if (file.isFile() && file.exists()) {
+ flag = file.delete();
}
-
- /**
- * 鏂囦欢鍚嶇О楠岃瘉
- *
- * @param filename 鏂囦欢鍚嶇О
- * @return true 姝e父 false 闈炴硶
- */
- public static boolean isValidFilename(String filename)
- {
- return filename.matches(FILENAME_PATTERN);
+ return flag;
+ }
+
+ /**
+ * 鏂囦欢鍚嶇О楠岃瘉
+ *
+ * @param filename 鏂囦欢鍚嶇О
+ * @return true 姝e父 false 闈炴硶
+ */
+ public static boolean isValidFilename(String filename) {
+ return filename.matches(FILENAME_PATTERN);
+ }
+
+ /**
+ * 鏍¢獙鏂囦欢璺緞鍚堟硶鎬э紙瀹夊叏鎬т笌鎵╁睍鍚嶏級
+ *
+ * @param fileUrl 寰呮牎楠岀殑鏂囦欢鍦板潃
+ * @return true 姝e父 false 闈炴硶
+ */
+ public static boolean validateFilePath(String fileUrl) {
+ // 绂佹鐩綍涓婅烦绾у埆
+ if (StringUtils.contains(fileUrl, "..")) {
+ return false;
}
-
- /**
- * 鏍¢獙鏂囦欢璺緞鍚堟硶鎬э紙瀹夊叏鎬т笌鎵╁睍鍚嶏級
- *
- * @param fileUrl 寰呮牎楠岀殑鏂囦欢鍦板潃
- * @return true 姝e父 false 闈炴硶
- */
- public static boolean validateFilePath(String fileUrl)
- {
- // 绂佹鐩綍涓婅烦绾у埆
- if (StringUtils.contains(fileUrl, ".."))
- {
- return false;
- }
- // 鍒ゆ柇鏄惁鍦ㄥ厑璁镐笅杞界殑鏂囦欢瑙勫垯鍐
- return ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(fileUrl));
+ // 鍒ゆ柇鏄惁鍦ㄥ厑璁镐笅杞界殑鏂囦欢瑙勫垯鍐
+ 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");
}
-
- /**
- * 涓嬭浇鏂囦欢鍚嶉噸鏂扮紪鐮
- *
- * @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;
+ return filename;
+ }
+
+ /**
+ * 杩斿洖鏂囦欢鍚
+ *
+ * @param filePath 鏂囦欢
+ * @return 鏂囦欢鍚
+ */
+ public static String getName(String filePath) {
+ if (null == filePath) {
+ return null;
}
-
- /**
- * 杩斿洖鏂囦欢鍚
- *
- * @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);
+ int len = filePath.length();
+ if (0 == len) {
+ return filePath;
}
-
- /**
- * 鏄惁涓篧indows鎴栬匧inux锛圲nix锛夋枃浠跺垎闅旂
- * Windows骞冲彴涓嬪垎闅旂涓篭锛孡inux锛圲nix锛変负/
- *
- * @param c 瀛楃
- * @return 鏄惁涓篧indows鎴栬匧inux锛圲nix锛夋枃浠跺垎闅旂
- */
- public static boolean isFileSeparator(char c)
- {
- return SLASH == c || BACKSLASH == c;
+ if (isFileSeparator(filePath.charAt(len - 1))) {
+ // 浠ュ垎闅旂缁撳熬鐨勫幓鎺夌粨灏惧垎闅旂
+ len--;
}
- /**
- * 涓嬭浇鏂囦欢鍚嶉噸鏂扮紪鐮
- *
- * @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);
+ int begin = 0;
+ char c;
+ for (int i = len - 1; i > -1; i--) {
+ c = filePath.charAt(i);
+ if (isFileSeparator(c)) {
+ // 鏌ユ壘鏈鍚庝竴涓矾寰勫垎闅旂锛/鎴栬匼锛
+ begin = i + 1;
+ break;
+ }
}
- /**
- * 鐧惧垎鍙风紪鐮佸伐鍏锋柟娉
- *
- * @param s 闇瑕佺櫨鍒嗗彿缂栫爜鐨勫瓧绗︿覆
- * @return 鐧惧垎鍙风紪鐮佸悗鐨勫瓧绗︿覆
- */
- public static String percentEncode(String s) throws UnsupportedEncodingException
- {
- String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
- return encode.replaceAll("\\+", "%20");
- }
+ return filePath.substring(begin, len);
+ }
+
+ /**
+ * 鏄惁涓篧indows鎴栬匧inux锛圲nix锛夋枃浠跺垎闅旂
+ * Windows骞冲彴涓嬪垎闅旂涓篭锛孡inux锛圲nix锛変负/
+ *
+ * @param c 瀛楃
+ * @return 鏄惁涓篧indows鎴栬匧inux锛圲nix锛夋枃浠跺垎闅旂
+ */
+ 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");
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/ImageUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/ImageUtils.java
index c3b19fc44..f65603570 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/ImageUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/ImageUtils.java
@@ -14,71 +14,54 @@ import org.slf4j.LoggerFactory;
*
* @author ruoyi
*/
-public class ImageUtils
-{
- private static final Logger log = LoggerFactory.getLogger(ImageUtils.class);
+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 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;
+ 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);
- }
+ /**
+ * 璇诲彇鏂囦欢涓哄瓧鑺傛暟鎹
+ *
+ * @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);
}
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/MimeTypeUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/MimeTypeUtils.java
index b8a3bc390..5ccfd4fc4 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/MimeTypeUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/file/MimeTypeUtils.java
@@ -2,58 +2,74 @@ package com.ruoyi.common.core.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 "";
- }
+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 "";
}
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/html/EscapeUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/html/EscapeUtil.java
index 2f5a8721b..a51ade0f6 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/html/EscapeUtil.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/html/EscapeUtil.java
@@ -4,164 +4,137 @@ import com.ruoyi.common.core.utils.StringUtils;
/**
* 杞箟鍜屽弽杞箟宸ュ叿绫
- *
+ *
* @author ruoyi
*/
-public class EscapeUtil
-{
- public static final String RE_HTML_MARK = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)";
+public class EscapeUtil {
+ public static final String RE_HTML_MARK = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)";
- private static final char[][] TEXT = new char[64][];
+ 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(); // 澶т簬鍙
+ static {
+ for (int i = 0; i < 64; i++) {
+ TEXT[i] = new char[] {(char) i};
}
- /**
- * 杞箟鏂囨湰涓殑HTML瀛楃涓哄畨鍏ㄧ殑瀛楃
- *
- * @param text 琚浆涔夌殑鏂囨湰
- * @return 杞箟鍚庣殑鏂囨湰
- */
- public static String escape(String text)
- {
- return encode(text);
- }
+ // special HTML characters
+ TEXT['\''] = "'".toCharArray(); // 鍗曞紩鍙
+ TEXT['"'] = """.toCharArray(); // 鍙屽紩鍙
+ TEXT['&'] = "&".toCharArray(); // &绗
+ TEXT['<'] = "<".toCharArray(); // 灏忎簬鍙
+ TEXT['>'] = ">".toCharArray(); // 澶т簬鍙
+ }
- /**
- * 杩樺師琚浆涔夌殑HTML鐗规畩瀛楃
- *
- * @param content 鍖呭惈杞箟绗︾殑HTML鍐呭
- * @return 杞崲鍚庣殑瀛楃涓
- */
- public static String unescape(String content)
- {
- return decode(content);
- }
+ /**
+ * 杞箟鏂囨湰涓殑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);
+ }
+
+ /**
+ * 娓呴櫎鎵鏈塇TML鏍囩锛屼絾鏄笉鍒犻櫎鏍囩鍐呯殑鍐呭
+ *
+ * @param content 鏂囨湰
+ * @return 娓呴櫎鏍囩鍚庣殑鏂囨湰
+ */
+ public static String clean(String content) {
+ return new HTMLFilter().filter(content);
+ }
- /**
- * 娓呴櫎鎵鏈塇TML鏍囩锛屼絾鏄笉鍒犻櫎鏍囩鍐呯殑鍐呭
- *
- * @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;
}
- /**
- * 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");
}
-
- 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));
- }
+ tmp.append(Integer.toString(c, 16));
+ } else {
+ tmp.append("%u");
+ if (c <= 0xfff) {
+ // issue#I49JU8@Gitee
+ tmp.append("0");
}
- return tmp.toString();
+ tmp.append(Integer.toString(c, 16));
+ }
}
+ return tmp.toString();
+ }
- /**
- * Escape瑙g爜
- *
- * @param content 琚浆涔夌殑鍐呭
- * @return 瑙g爜鍚庣殑瀛楃涓
- */
- public static String decode(String content)
- {
- if (StringUtils.isEmpty(content))
- {
- return content;
- }
+ /**
+ * Escape瑙g爜
+ *
+ * @param content 琚浆涔夌殑鍐呭
+ * @return 瑙g爜鍚庣殑瀛楃涓
+ */
+ 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;
- }
- }
+ 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;
}
- return tmp.toString();
+ } 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 = "";
- String escape = EscapeUtil.escape(html);
- // String html = "ipt>alert(\"XSS\")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));
- }
+ public static void main(String[] args) {
+ String html = "";
+ String escape = EscapeUtil.escape(html);
+ // String html = "ipt>alert(\"XSS\")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));
+ }
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/html/HTMLFilter.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/html/HTMLFilter.java
index ba53dc930..fae025290 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/html/HTMLFilter.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/html/HTMLFilter.java
@@ -15,556 +15,485 @@ import java.util.regex.Pattern;
*
* @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("([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 P_REMOVE_PAIR_BLANKS = new ConcurrentHashMap<>();
- private static final ConcurrentMap P_REMOVE_SELF_BLANKS = new ConcurrentHashMap<>();
-
- /**
- * set of allowed html elements, along with allowed attributes for each element
- **/
- private final Map> vAllowed;
- /**
- * counts of open tags for each (allowable) html element
- **/
- private final Map vTagCounts = new HashMap<>();
-
- /**
- * html elements which must always be self-closing (e.g. "
")
- **/
- private final String[] vSelfClosingTags;
- /**
- * html elements which must always have separate opening and closing tags (e.g. "")
- **/
- 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. "" or "")
- **/
- 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. ""
- * becomes " text "). If set to false, unbalanced angle brackets will be html escaped.
- */
- private final boolean alwaysMakeTags;
-
- /**
- * Default constructor.
- */
- public HTMLFilter()
- {
- vAllowed = new HashMap<>();
-
- final ArrayList a_atts = new ArrayList<>();
- a_atts.add("href");
- a_atts.add("target");
- vAllowed.put("a", a_atts);
-
- final ArrayList 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 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;
+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("([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 P_REMOVE_PAIR_BLANKS =
+ new ConcurrentHashMap<>();
+ private static final ConcurrentMap P_REMOVE_SELF_BLANKS =
+ new ConcurrentHashMap<>();
+
+ /** set of allowed html elements, along with allowed attributes for each element */
+ private final Map> vAllowed;
+
+ /** counts of open tags for each (allowable) html element */
+ private final Map vTagCounts = new HashMap<>();
+
+ /** html elements which must always be self-closing (e.g. "
") */
+ private final String[] vSelfClosingTags;
+
+ /** html elements which must always have separate opening and closing tags (e.g. "") */
+ 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. "" or "") */
+ 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. "" becomes " text "). If set to false, unbalanced angle brackets will
+ * be html escaped.
+ */
+ private final boolean alwaysMakeTags;
+
+ /** Default constructor. */
+ public HTMLFilter() {
+ vAllowed = new HashMap<>();
+
+ final ArrayList a_atts = new ArrayList<>();
+ a_atts.add("href");
+ a_atts.add("target");
+ vAllowed.put("a", a_atts);
+
+ final ArrayList 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 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 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>) 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(""));
}
-
- /**
- * Map-parameter configurable constructor.
- *
- * @param conf map containing configuration. keys match field names.
- */
- @SuppressWarnings("unchecked")
- public HTMLFilter(final Map 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>) 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;
+ 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;
+ }
- /**
- * 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;
+ private String checkTags(String s) {
+ Matcher m = P_TAGS.matcher(s);
- s = escapeComments(s);
-
- s = balanceHTML(s);
-
- s = checkTags(s);
-
- s = processRemoveBlanks(s);
-
- // s = validateEntities(s);
-
- return s;
+ final StringBuffer buf = new StringBuffer();
+ while (m.find()) {
+ String replaceStr = m.group(1);
+ replaceStr = processTag(replaceStr);
+ m.appendReplacement(buf, Matcher.quoteReplacement(replaceStr));
}
-
- public boolean isAlwaysMakeTags()
- {
- return alwaysMakeTags;
+ 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(">");
+ }
}
-
- public boolean isStripComments()
- {
- return stripComment;
+ 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);
}
- 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(""));
+ 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 + ">";
+ }
}
- 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);
-
+ // 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 paramNames = new ArrayList<>();
+ final List paramValues = new ArrayList<>();
+ while (m2.find()) {
+ paramNames.add(m2.group(1)); // ([a-z0-9]+)
+ paramValues.add(m2.group(3)); // (.*?)
}
- 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);
+ while (m3.find()) {
+ paramNames.add(m3.group(1)); // ([a-z0-9]+)
+ paramValues.add(m3.group(3)); // ([^\"\\s']+)
}
- return s;
- }
+ String paramName, paramValue;
+ for (int ii = 0; ii < paramNames.size(); ii++) {
+ paramName = paramNames.get(ii).toLowerCase();
+ paramValue = paramValues.get(ii);
- private String checkTags(String s)
- {
- Matcher m = P_TAGS.matcher(s);
+ // debug( "paramName='" + paramName + "'" );
+ // debug( "paramValue='" + paramValue + "'" );
+ // debug( "allowed? " + vAllowed.get( name ).contains( paramName ) );
- 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(">");
+ if (allowedAttribute(name, paramName)) {
+ if (inArray(paramName, vProtocolAtts)) {
+ paramValue = processParamProtocol(paramValue);
}
+ params.append(' ').append(paramName).append("=\\\"").append(paramValue).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 + ">";
- }
- }
- }
+ if (inArray(name, vSelfClosingTags)) {
+ ending = " /";
}
- // 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 paramNames = new ArrayList<>();
- final List 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 "";
- }
+ if (inArray(name, vNeedClosingTags)) {
+ ending = "";
}
- // comments
- m = P_COMMENT.matcher(s);
- if (!stripComment && m.find())
- {
- return "<" + m.group() + ">";
+ 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 "";
+ }
}
- 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;
+ // comments
+ m = P_COMMENT.matcher(s);
+ if (!stripComment && m.find()) {
+ return "<" + m.group() + ">";
}
- 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)));
+ 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);
}
- 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 s;
+ }
- return encodeQuotes(buf.toString());
- }
+ private String decodeEntities(String s) {
+ StringBuffer buf = new StringBuffer();
- 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); // (<|$)
- // 涓嶆浛鎹㈠弻寮曞彿涓"锛岄槻姝son鏍煎紡鏃犳晥 regexReplace(P_QUOTE, """, two)
- m.appendReplacement(buf, Matcher.quoteReplacement(one + two + three));
- }
- m.appendTail(buf);
- return buf.toString();
- }
- else
- {
- return s;
- }
+ 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); // (<|$)
+ // 涓嶆浛鎹㈠弻寮曞彿涓"锛岄槻姝son鏍煎紡鏃犳晥 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)
- {
+ private String checkEntity(final String preamble, final String term) {
- return ";".equals(term) && isValidEntity(preamble) ? '&' + preamble : "&" + preamble;
- }
+ return ";".equals(term) && isValidEntity(preamble) ? '&' + preamble : "&" + preamble;
+ }
- private boolean isValidEntity(final String entity)
- {
- return inArray(entity, vAllowedEntities);
- }
+ 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 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 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));
- }
-}
\ No newline at end of file
+ private boolean allowedAttribute(final String name, final String paramName) {
+ return allowed(name) && (vAllowed.isEmpty() || vAllowed.get(name).contains(paramName));
+ }
+}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/IpUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/IpUtils.java
index 43366ec71..c9d0fae3e 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/IpUtils.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/IpUtils.java
@@ -1,382 +1,323 @@
package com.ruoyi.common.core.utils.ip;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import javax.servlet.http.HttpServletRequest;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.StringUtils;
+import jakarta.servlet.http.HttpServletRequest;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
/**
* 鑾峰彇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 + ")";
+public class IpUtils {
+ public static final String REGX_0_255 = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)";
+ // 鍖归厤 ip
+ public static final String REGX_IP = "((" + REGX_0_255 + "\\.){3}" + REGX_0_255 + ")";
+ public static final String REGX_IP_WILDCARD =
+ "(((\\*\\.){3}\\*)|("
+ + REGX_0_255
+ + "(\\.\\*){3})|("
+ + REGX_0_255
+ + "\\."
+ + REGX_0_255
+ + ")(\\.\\*){2}"
+ + "|(("
+ + REGX_0_255
+ + "\\.){3}\\*))";
+ // 鍖归厤缃戞
+ public static final String REGX_IP_SEG = "(" + REGX_IP + "\\-" + REGX_IP + ")";
- /**
- * 鑾峰彇瀹㈡埛绔疘P
- *
- * @return IP鍦板潃
- */
- public static String getIpAddr()
- {
- return getIpAddr(ServletUtils.getRequest());
+ /**
+ * 鑾峰彇瀹㈡埛绔疘P
+ *
+ * @return IP鍦板潃
+ */
+ public static String getIpAddr() {
+ return getIpAddr(ServletUtils.getRequest());
+ }
+
+ /**
+ * 鑾峰彇瀹㈡埛绔疘P
+ *
+ * @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");
}
- /**
- * 鑾峰彇瀹㈡埛绔疘P
- *
- * @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();
+ }
- 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);
+ }
- 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 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;
}
-
- /**
- * 妫鏌ユ槸鍚︿负鍐呴儴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;
}
- 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;
+ case SECTION_5:
+ switch (b1) {
+ case SECTION_6:
+ return true;
}
+ default:
+ return false;
}
+ }
- /**
- * 灏咺Pv4鍦板潃杞崲鎴愬瓧鑺
- *
- * @param text IPv4鍦板潃
- * @return byte 瀛楄妭
- */
- public static byte[] textToNumericFormatV4(String text)
- {
- if (text.length() == 0)
- {
- return null;
- }
+ /**
+ * 灏咺Pv4鍦板潃杞崲鎴愬瓧鑺
+ *
+ * @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;
+ 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;
}
- }
- catch (NumberFormatException e)
- {
+ bytes[i] = (byte) (int) (l & 0xFF);
+ }
+ l = Integer.parseInt(elements[2]);
+ if ((l < 0L) || (l > 65535L)) {
return null;
- }
- return bytes;
+ }
+ 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";
+ /**
+ * 鑾峰彇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 "鏈煡";
+ /**
+ * 鑾峰彇涓绘満鍚
+ *
+ * @return 鏈湴涓绘満鍚
+ */
+ public static String getHostName() {
+ try {
+ return InetAddress.getLocalHost().getHostName();
+ } catch (UnknownHostException e) {
}
+ return "鏈煡";
+ }
- /**
- * 浠庡绾у弽鍚戜唬鐞嗕腑鑾峰緱绗竴涓潪unknown IP鍦板潃
- *
- * @param ip 鑾峰緱鐨処P鍦板潃
- * @return 绗竴涓潪unknown IP鍦板潃
- */
- public static String getMultistageReverseProxyIp(String ip)
- {
- // 澶氱骇鍙嶅悜浠g悊妫娴
- if (ip != null && ip.indexOf(",") > 0)
- {
- final String[] ips = ip.trim().split(",");
- for (String subIp : ips)
- {
- if (false == isUnknown(subIp))
- {
- ip = subIp;
- break;
- }
- }
+ /**
+ * 浠庡绾у弽鍚戜唬鐞嗕腑鑾峰緱绗竴涓潪unknown IP鍦板潃
+ *
+ * @param ip 鑾峰緱鐨処P鍦板潃
+ * @return 绗竴涓潪unknown IP鍦板潃
+ */
+ public static String getMultistageReverseProxyIp(String ip) {
+ // 澶氱骇鍙嶅悜浠g悊妫娴
+ 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);
+ }
}
+ return StringUtils.substring(ip, 0, 255);
+ }
- /**
- * 妫娴嬬粰瀹氬瓧绗︿覆鏄惁涓烘湭鐭ワ紝澶氱敤浜庢娴婬TTP璇锋眰鐩稿叧
- *
- * @param checkString 琚娴嬬殑瀛楃涓
- * @return 鏄惁鏈煡
- */
- public static boolean isUnknown(String checkString)
- {
- return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
- }
+ /**
+ * 妫娴嬬粰瀹氬瓧绗︿覆鏄惁涓烘湭鐭ワ紝澶氱敤浜庢娴婬TTP璇锋眰鐩稿叧
+ *
+ * @param checkString 琚娴嬬殑瀛楃涓
+ * @return 鏄惁鏈煡
+ */
+ public static boolean isUnknown(String checkString) {
+ return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
+ }
- /**
- * 鏄惁涓篒P
- */
- public static boolean isIP(String ip)
- {
- return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP);
- }
+ /** 鏄惁涓篒P */
+ public static boolean isIP(String ip) {
+ return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP);
+ }
- /**
- * 鏄惁涓篒P锛屾垨 *涓洪棿闅旂殑閫氶厤绗﹀湴鍧
- */
- public static boolean isIpWildCard(String ip)
- {
- return StringUtils.isNotBlank(ip) && ip.matches(REGX_IP_WILDCARD);
- }
+ /** 鏄惁涓篒P锛屾垨 *涓洪棿闅旂殑閫氶厤绗﹀湴鍧 */
+ 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;
+ /** 妫娴嬪弬鏁版槸鍚﹀湪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);
- }
+ /** 鏄惁涓虹壒瀹氭牸寮忓:鈥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鏄惁鍦ㄦ寚瀹氱綉娈典腑 */
+ 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;
+ /**
+ * 鏍¢獙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;
+ }
}
-}
\ No newline at end of file
+ return false;
+ }
+}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelHandlerAdapter.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelHandlerAdapter.java
index f8f863e03..3bfcb776f 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelHandlerAdapter.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelHandlerAdapter.java
@@ -5,20 +5,18 @@ 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);
+public interface ExcelHandlerAdapter {
+ /**
+ * 鏍煎紡鍖
+ *
+ * @param value 鍗曞厓鏍兼暟鎹
+ * @param args excel娉ㄨВargs鍙傛暟缁
+ * @param cell 鍗曞厓鏍煎璞
+ * @param wb 宸ヤ綔绨垮璞
+ * @return 澶勭悊鍚庣殑鍊
+ */
+ Object format(Object value, String[] args, Cell cell, Workbook wb);
}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java
index 8c2434331..129ac6d41 100644
--- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java
@@ -1,5 +1,17 @@
package com.ruoyi.common.core.utils.poi;
+import com.ruoyi.common.core.annotation.Excel;
+import com.ruoyi.common.core.annotation.Excel.ColumnType;
+import com.ruoyi.common.core.annotation.Excel.Type;
+import com.ruoyi.common.core.annotation.Excels;
+import com.ruoyi.common.core.exception.UtilException;
+import com.ruoyi.common.core.text.Convert;
+import com.ruoyi.common.core.utils.DateUtils;
+import com.ruoyi.common.core.utils.StringUtils;
+import com.ruoyi.common.core.utils.file.FileTypeUtils;
+import com.ruoyi.common.core.utils.file.ImageUtils;
+import com.ruoyi.common.core.utils.reflect.ReflectUtils;
+import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
@@ -19,7 +31,6 @@ 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;
@@ -52,1610 +63,1319 @@ import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.ruoyi.common.core.annotation.Excel;
-import com.ruoyi.common.core.annotation.Excel.ColumnType;
-import com.ruoyi.common.core.annotation.Excel.Type;
-import com.ruoyi.common.core.annotation.Excels;
-import com.ruoyi.common.core.exception.UtilException;
-import com.ruoyi.common.core.text.Convert;
-import com.ruoyi.common.core.utils.DateUtils;
-import com.ruoyi.common.core.utils.StringUtils;
-import com.ruoyi.common.core.utils.file.FileTypeUtils;
-import com.ruoyi.common.core.utils.file.ImageUtils;
-import com.ruoyi.common.core.utils.reflect.ReflectUtils;
/**
* Excel鐩稿叧澶勭悊
*
* @author ruoyi
*/
-public class ExcelUtil
-{
- 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;
-
- /**
- * 瀵煎嚭绫诲瀷锛圗XPORT:瀵煎嚭鏁版嵁锛汭MPORT锛氬鍏ユā鏉匡級
- */
- private Type type;
-
- /**
- * 宸ヤ綔钖勫璞
- */
- private Workbook wb;
-
- /**
- * 宸ヤ綔琛ㄥ璞
- */
- private Sheet sheet;
-
- /**
- * 鏍峰紡鍒楄〃
- */
- private Map styles;
-
- /**
- * 瀵煎叆瀵煎嚭鏁版嵁鍒楄〃
- */
- private List list;
-
- /**
- * 娉ㄨВ鍒楄〃
- */
- private List