fields = new ArrayList<>();
+ ReflectionUtils.doWithFields(clazz, field -> {
+ CsvColumn annotation = field.getAnnotation(CsvColumn.class);
+ if (annotation != null) {
+ fields.add(new CsvField(
+ annotation.header(),
+ field.getName(),
+ annotation.order()
+ ));
+ }
+ });
+
+ // 按 order 排序
+ fields.sort(Comparator.comparingInt(CsvField::getOrder));
+ return fields;
+ }
+
+ @Data
+ @AllArgsConstructor
+ private static class CsvField {
+ private String header;
+ private String field;
+ private int order;
+ }
+}
diff --git a/box-common/src/main/java/com/mathvision/box/common/utils/file/DiskUtils.java b/box-common/src/main/java/com/mathvision/box/common/utils/file/DiskUtils.java
new file mode 100644
index 0000000..9c17a84
--- /dev/null
+++ b/box-common/src/main/java/com/mathvision/box/common/utils/file/DiskUtils.java
@@ -0,0 +1,83 @@
+package com.mathvision.box.common.utils.file;
+
+import com.mathvision.box.common.utils.common.OSUtils;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.FileStore;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+@Slf4j
+public class DiskUtils {
+ /**
+ * 检查磁盘使用率
+ * Windows传入盘符(如"C")
+ * Linux传入挂载路径(如"/data")
+ *
+ * @param path 磁盘路径
+ * @throws IOException
+ */
+ public static boolean checkDiskUsage(String path, Integer threshold) {
+ try {
+ double usage;
+
+ if (OSUtils.isWindows()) {
+ usage = getWindowsDiskUsage(path);
+ } else {
+ usage = getLinuxDiskUsage(path);
+ }
+
+ if (usage > threshold) {
+ log.error("[警告] 磁盘" + path + "使用率已达" + usage);
+ return true;
+ } else {
+ //log.info("[正常] 磁盘" + path + "使用率正常" + usage);
+ return false;
+ }
+ } catch (IOException e) {
+ log.error("[警告] 获取磁盘使用率失败", e);
+ return true;
+ }
+ }
+
+ public static double getDiskUsage(String path) {
+ try {
+ double usage;
+ if (OSUtils.isWindows()) {
+ usage = getWindowsDiskUsage(path);
+ } else {
+ usage = getLinuxDiskUsage(path);
+ }
+ return usage;
+ } catch (IOException e) {
+ log.error("[警告] 获取磁盘使用率失败", e);
+ return 0d;
+ }
+ }
+
+
+ // Linux磁盘容量
+ private static double getLinuxDiskUsage(String path) throws IOException {
+ Path targetPath = Paths.get(path);
+ FileStore store = Files.getFileStore(targetPath);
+ long total = store.getTotalSpace();
+ long usable = store.getUsableSpace();
+ return (total - usable) * 100.0 / total;
+ }
+
+ // Windows磁盘容量
+ private static double getWindowsDiskUsage(String path) throws IOException {
+ File file = new File(path);
+ for (File root : File.listRoots()) {
+ if (file.getAbsolutePath().startsWith(root.getAbsolutePath())) {
+ long total = root.getTotalSpace();
+ long free = root.getFreeSpace();
+ return (total - free) * 100.0 / total;
+ }
+ }
+ throw new IOException("路径未映射到有效磁盘: " + path);
+ }
+}
diff --git a/box-common/src/main/java/com/mathvision/box/common/utils/file/FileTypeUtils.java b/box-common/src/main/java/com/mathvision/box/common/utils/file/FileTypeUtils.java
new file mode 100644
index 0000000..1a2bf85
--- /dev/null
+++ b/box-common/src/main/java/com/mathvision/box/common/utils/file/FileTypeUtils.java
@@ -0,0 +1,82 @@
+package com.mathvision.box.common.utils.file;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+/**
+ * @Author: fy
+ * @Date: 2024/03/22
+ * @Description:文件类型工具类
+ */
+@Slf4j
+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();
+ }
+
+ /**
+ * 获取文件类型
+ *
+ * @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;
+ }
+
+}
diff --git a/box-common/src/main/java/com/mathvision/box/common/utils/file/FileUploadUtils.java b/box-common/src/main/java/com/mathvision/box/common/utils/file/FileUploadUtils.java
new file mode 100644
index 0000000..315dd82
--- /dev/null
+++ b/box-common/src/main/java/com/mathvision/box/common/utils/file/FileUploadUtils.java
@@ -0,0 +1,258 @@
+package com.mathvision.box.common.utils.file;
+
+import com.mathvision.box.common.config.AppConfig;
+import com.mathvision.box.common.constant.Constants;
+import com.mathvision.box.common.exception.file.*;
+import com.mathvision.box.common.utils.common.DateUtils;
+import com.mathvision.box.common.utils.common.StringUtils;
+import com.mathvision.box.common.utils.uuid.Seq;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FilenameUtils;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.*;
+import java.nio.ByteBuffer;
+import java.nio.file.Paths;
+import java.util.Objects;
+
+/**
+ * 文件上传工具类
+ */
+@Slf4j
+public class FileUploadUtils {
+ /**
+ * 默认大小 50M
+ */
+ public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
+
+ /**
+ * 默认的文件名最大长度 100
+ */
+ public static final int DEFAULT_FILE_NAME_LENGTH = 100;
+
+ /**
+ * 默认上传的地址
+ */
+ private static String defaultBaseDir = AppConfig.getProfile();
+
+ public static void setDefaultBaseDir(String defaultBaseDir) {
+ FileUploadUtils.defaultBaseDir = defaultBaseDir;
+ }
+
+ public static String getDefaultBaseDir() {
+ return defaultBaseDir;
+ }
+
+ public static final String upload(ByteBuffer buffer, int dataLen, String fileName, String profile) {
+ if (dataLen <= 0) {
+ log.error("buffer数据长度无效({}),保存失败", dataLen);
+ return "";
+ }
+ if (buffer.capacity() < dataLen) {
+ log.error("缓冲区容量不足(需要{},实际{}),保存失败", dataLen, buffer.capacity());
+ return "";
+ }
+ byte[] bytes = new byte[dataLen];
+ buffer.rewind();
+ buffer.get(bytes);
+ return upload(bytes, dataLen, fileName, profile);
+ }
+
+ public static final String upload(byte[] bytes, int dataLen, String fileName, String profile) {
+ String newFileName = StringUtils.isNotBlank(profile) ? AppConfig.getProfile() + "/" + profile + "/" + DateUtils.datePath() + "/" + fileName : AppConfig.getUploadPath() + DateUtils.datePath() + "/" + fileName;
+ File file = new File(newFileName);
+
+ // 创建文件
+ if (!file.exists()) {
+ if (!file.getParentFile().exists()) {
+ if (!file.getParentFile().mkdirs()) {
+ log.error("无法创建目录: {}", newFileName);
+ }
+ }
+ try {
+ file.createNewFile();
+ } catch (IOException e) {
+ log.error("创建文件失败:{}", e);
+ }
+ }
+
+ try (FileOutputStream fout = new FileOutputStream(newFileName)) {
+ fout.write(bytes, 0, dataLen);
+ fout.close();
+ return newFileName;
+ } catch (Exception e) {
+ log.error("保存失败:{}", e);
+ return "";
+ }
+ }
+
+ /**
+ * 以默认配置进行文件上传
+ *
+ * @param file 上传的文件
+ * @return 文件名称
+ * @throws Exception
+ */
+ public static final String upload(MultipartFile file) throws IOException {
+ try {
+ return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
+ } catch (Exception e) {
+ throw new IOException(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * 根据文件路径上传
+ *
+ * @param baseDir 相对应用的基目录
+ * @param file 上传的文件
+ * @return 文件名称
+ * @throws IOException
+ */
+ public static final String upload(String baseDir, MultipartFile file) throws IOException {
+ try {
+ return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
+ } catch (Exception e) {
+ throw new IOException(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * 文件上传
+ *
+ * @param baseDir 相对应用的基目录
+ * @param file 上传的文件
+ * @param allowedExtension 上传文件类型
+ * @return 返回上传成功的文件名
+ * @throws FileSizeLimitExceededException 如果超出最大大小
+ * @throws FileNameLengthLimitExceededException 文件名太长
+ * @throws IOException 比如读写文件出错时
+ * @throws InvalidExtensionException 文件校验异常
+ */
+ public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
+ throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
+ InvalidExtensionException {
+ int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length();
+ if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
+ throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
+ }
+
+ assertAllowed(file, allowedExtension);
+
+ String fileName = extractFilename(file);
+
+ String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
+ file.transferTo(Paths.get(absPath));
+ return getPathFileName(baseDir, fileName);
+ }
+
+ /**
+ * 编码文件名
+ */
+ public static final String extractFilename(MultipartFile file) {
+ return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
+ }
+
+ public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
+ File desc = new File(uploadDir + File.separator + fileName);
+
+ if (!desc.exists()) {
+ if (!desc.getParentFile().exists()) {
+ desc.getParentFile().mkdirs();
+ }
+ }
+ return desc;
+ }
+
+ public static final String getPathFileName(String uploadDir, String fileName) throws IOException {
+ int dirLastIndex = AppConfig.getProfile().length() + 1;
+ String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
+ return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
+ }
+
+ /**
+ * 文件大小校验
+ *
+ * @param file 上传的文件
+ * @return
+ * @throws FileSizeLimitExceededException 如果超出最大大小
+ * @throws InvalidExtensionException
+ */
+ public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
+ throws FileSizeLimitExceededException, InvalidExtensionException {
+ long size = file.getSize();
+ if (size > DEFAULT_MAX_SIZE) {
+ throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
+ }
+
+ String fileName = file.getOriginalFilename();
+ String extension = getExtension(file);
+ if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
+ if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) {
+ throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
+ fileName);
+ } else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) {
+ throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
+ fileName);
+ } else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) {
+ throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
+ fileName);
+ } else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) {
+ throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
+ fileName);
+ } else {
+ throw new InvalidExtensionException(allowedExtension, extension, fileName);
+ }
+ }
+ }
+
+ /**
+ * 判断MIME类型是否是允许的MIME类型
+ *
+ * @param extension
+ * @param allowedExtension
+ * @return
+ */
+ public static final boolean isAllowedExtension(String extension, String[] allowedExtension) {
+ for (String str : allowedExtension) {
+ if (str.equalsIgnoreCase(extension)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * 获取文件名的后缀
+ *
+ * @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;
+ }
+
+ /**
+ * 将 MultipartFile 转换为 File 并保存
+ *
+ * @param multipartFile 上传的文件
+ * @param destPath 保存的目标路径
+ * @return 保存后的 File 对象
+ * @throws IOException 如果在保存过程中发生错误
+ */
+ public static File convertMultipartFileToFile(MultipartFile multipartFile, String destPath) throws IOException {
+ File destFile = new File(destPath);
+ // 如果目标路径不存在,创建目录
+ if (!destFile.getParentFile().exists()) {
+ destFile.getParentFile().mkdirs();
+ }
+ multipartFile.transferTo(destFile);
+ return destFile;
+ }
+
+
+}
\ No newline at end of file
diff --git a/box-common/src/main/java/com/mathvision/box/common/utils/file/FileUtils.java b/box-common/src/main/java/com/mathvision/box/common/utils/file/FileUtils.java
new file mode 100644
index 0000000..e386d79
--- /dev/null
+++ b/box-common/src/main/java/com/mathvision/box/common/utils/file/FileUtils.java
@@ -0,0 +1,447 @@
+package com.mathvision.box.common.utils.file;
+
+import com.mathvision.box.common.config.AppConfig;
+import com.mathvision.box.common.utils.common.DateUtils;
+import com.mathvision.box.common.utils.common.StringUtils;
+import com.mathvision.box.common.utils.uuid.IdUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.ArrayUtils;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @Author: fy
+ * @Date: 2024/03/22
+ * @Description:文件处理工具类
+ */
+@Slf4j
+public class FileUtils {
+ public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
+
+ /**
+ * 检查级别枚举
+ */
+ public enum CheckLevel {
+ BASIC, // 基础检查(存在性、类型)
+ NORMAL, // 普通检查(基础 + 大小、权限)
+ STRICT // 严格检查(普通 + 内容、时效性)
+ }
+
+ public static boolean checkParentDir(String filePath) throws IOException {
+ File file = new File(filePath);
+ // 确保父目录存在
+ File parentDir = file.getParentFile();
+ if (parentDir.exists()){
+ return true;
+ }else {
+ if (parentDir.mkdirs()) {
+ return true;
+ }else {
+ throw new IOException("无法创建目录: " + parentDir.getAbsolutePath());
+ }
+ }
+ }
+
+
+ public static boolean isExist(String filePath){
+ File file = new File(filePath);
+ return file.exists();
+ }
+
+ /**
+ * 使用 File 类创建文件
+ */
+ public static File createFile(String filePath) throws IOException {
+ File file = new File(filePath);
+ // 确保父目录存在
+ File parentDir = file.getParentFile();
+ if (!parentDir.exists()) {
+ if (!parentDir.mkdirs()) {
+ throw new IOException("无法创建目录: " + parentDir.getAbsolutePath());
+ }
+ }
+ // 创建文件
+ if (!file.exists()) {
+ if (!file.createNewFile()) {
+ throw new IOException("无法创建文件: " + filePath);
+ }
+ }
+ return file;
+ }
+
+
+ /**
+ * 输出指定文件的byte数组
+ *
+ * @param filePath 文件路径
+ * @param os 输出流
+ * @return
+ */
+ public static void writeBytes(String filePath, OutputStream os) throws IOException {
+ FileInputStream fis = null;
+ try {
+ File file = new File(filePath);
+ if (!file.exists()) {
+ throw new FileNotFoundException(filePath);
+ }
+ fis = new FileInputStream(file);
+ byte[] b = new byte[1024];
+ int length;
+ while ((length = fis.read(b)) > 0) {
+ os.write(b, 0, length);
+ }
+ } catch (IOException e) {
+ throw e;
+ } finally {
+ IOUtils.close(os);
+ IOUtils.close(fis);
+ }
+ }
+
+ /**
+ * 写数据到文件中
+ *
+ * @param data 数据
+ * @return 目标文件
+ * @throws IOException IO异常
+ */
+ public static String writeImportBytes(byte[] data) throws IOException {
+ return writeBytes(data, AppConfig.getImportPath());
+ }
+
+ /**
+ * 写数据到文件中
+ *
+ * @param data 数据
+ * @param uploadDir 目标文件
+ * @return 目标文件
+ * @throws IOException IO异常
+ */
+ public static String writeBytes(byte[] data, String uploadDir) throws IOException {
+ FileOutputStream fos = null;
+ String pathName = "";
+ try {
+ String extension = getFileExtendName(data);
+ pathName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
+ File file = FileUploadUtils.getAbsoluteFile(uploadDir, pathName);
+ fos = new FileOutputStream(file);
+ fos.write(data);
+ } finally {
+ IOUtils.close(fos);
+ }
+ return FileUploadUtils.getPathFileName(uploadDir, pathName);
+ }
+
+ /**
+ * 删除文件
+ *
+ * @param filePath 文件
+ * @return
+ */
+ public static boolean deleteFile(String filePath) {
+ boolean flag = false;
+ File file = new File(filePath);
+ // 路径为文件且不为空则进行删除
+ if (file.isFile() && file.exists()) {
+ flag = file.delete();
+ }
+ return flag;
+ }
+
+ /**
+ * 文件名称验证
+ *
+ * @param filename 文件名称
+ * @return true 正常 false 非法
+ */
+ public static boolean isValidFilename(String filename) {
+ return filename.matches(FILENAME_PATTERN);
+ }
+
+ /**
+ * 检查文件是否可下载
+ *
+ * @param resource 需要下载的文件
+ * @return true 正常 false 非法
+ */
+ public static boolean checkAllowDownload(String resource) {
+ // 禁止目录上跳级别
+ if (StringUtils.contains(resource, "..")) {
+ return false;
+ }
+
+ // 检查允许下载的文件规则
+ if (ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource))) {
+ return true;
+ }
+
+ // 不在允许下载的文件规则
+ return false;
+ }
+
+ /**
+ * 下载文件名重新编码
+ *
+ * @param request 请求对象
+ * @param fileName 文件名
+ * @return 编码后的文件名
+ */
+ public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
+ final String agent = request.getHeader("USER-AGENT");
+ String filename = fileName;
+ if (agent.contains("MSIE")) {
+ // IE浏览器
+ filename = URLEncoder.encode(filename, "utf-8");
+ filename = filename.replace("+", " ");
+ } else if (agent.contains("Firefox")) {
+ // 火狐浏览器
+ filename = new String(fileName.getBytes(), "ISO8859-1");
+ } else if (agent.contains("Chrome")) {
+ // google浏览器
+ filename = URLEncoder.encode(filename, "utf-8");
+ } else {
+ // 其它浏览器
+ filename = URLEncoder.encode(filename, "utf-8");
+ }
+ return filename;
+ }
+
+ /**
+ * 下载文件名重新编码
+ *
+ * @param 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());
+ }
+
+ public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName, String type) throws UnsupportedEncodingException {
+ response.setCharacterEncoding("utf-8");
+ if ("pdf".equals(type)) {
+ response.setContentType("application/pdf");
+ response.setHeader("Content-Disposition", "inline; filename=\"" + URLEncoder.encode(realFileName + "_" + DateUtils.dateTimeNow() + "." + type, "UTF-8") + "\"");
+ } else if ("doc".equals(type) || "docx".equals(type)) {
+ response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(realFileName + "_" + DateUtils.dateTimeNow() + "." + type, "UTF-8") + "\"");
+ } else if ("xls".equals(type) || "xlsx".equals(type)) {
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(realFileName + "_" + DateUtils.dateTimeNow() + "." + type, "UTF-8") + "\"");
+ } else if ("zip".equals(type)) {
+ response.setContentType("application/zip");
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(realFileName + "_" + DateUtils.dateTimeNow() + "." + type, "UTF-8") + "\"");
+ }
+
+ }
+
+ /**
+ * 百分号编码工具方法
+ *
+ * @param s 需要百分号编码的字符串
+ * @return 百分号编码后的字符串
+ */
+ public static String percentEncode(String s) throws UnsupportedEncodingException {
+ String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
+ return encode.replaceAll("\\+", "%20");
+ }
+
+ /**
+ * 获取图像后缀
+ *
+ * @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;
+ }
+
+ /**
+ * 获取文件名称 /profile/upload/2022/04/16/ruoyi.png -- ruoyi.png
+ *
+ * @param fileName 路径名称
+ * @return 没有文件路径的名称
+ */
+ public static String getName(String fileName) {
+ if (fileName == null) {
+ return null;
+ }
+ int lastUnixPos = fileName.lastIndexOf('/');
+ int lastWindowsPos = fileName.lastIndexOf('\\');
+ int index = Math.max(lastUnixPos, lastWindowsPos);
+ return fileName.substring(index + 1);
+ }
+
+ /**
+ * 获取文件父路径
+ *
+ * @param fileName 文件路径
+ * @return 获取文件父路径
+ */
+ public static String getParent(String fileName) {
+ File file = new File(fileName);
+ File parentDir = file.getParentFile();
+ if (parentDir != null) {
+ return parentDir.getAbsolutePath();
+ } else {
+ return "";
+ }
+ }
+
+ /**
+ * 获取不带后缀文件名称 /profile/upload/2022/04/16/ruoyi.png -- ruoyi
+ *
+ * @param fileName 路径名称
+ * @return 没有文件路径和后缀的名称
+ */
+ public static String getNameNotSuffix(String fileName) {
+ if (fileName == null) {
+ return null;
+ }
+ String baseName = FilenameUtils.getBaseName(fileName);
+ return baseName;
+ }
+
+ /**
+ * 检查文件是否有效
+ *
+ * @param filePath 文件路径
+ * @param level 检查级别
+ * @return 文件是否有效
+ */
+ public static boolean isFileValid(String filePath, CheckLevel level) {
+ if (StringUtils.isEmpty(filePath)) {
+ log.warn("文件路径为空");
+ return false;
+ }
+
+ File file = new File(filePath);
+
+ // 基础检查
+ if (!basicCheck(file)) {
+ return false;
+ }
+
+ // 根据检查级别执行不同的检查
+ if (level.ordinal() >= CheckLevel.NORMAL.ordinal()) {
+ if (!normalCheck(file)) {
+ return false;
+ }
+ }
+
+ if (level.ordinal() >= CheckLevel.STRICT.ordinal()) {
+ if (!strictCheck(file)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * 基础检查:文件存在性和类型
+ */
+ private static boolean basicCheck(File file) {
+ if (!file.exists()) {
+ log.warn("文件不存在: {}", file.getPath());
+ return false;
+ }
+
+ if (!file.isFile()) {
+ log.warn("不是普通文件: {}", file.getPath());
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * 普通检查:文件大小和权限
+ */
+ private static boolean normalCheck(File file) {
+ if (file.length() == 0) {
+ log.warn("文件为空: {}", file.getPath());
+ return false;
+ }
+
+ if (!file.canRead()) {
+ log.warn("文件不可读: {}", file.getPath());
+ return false;
+ }
+
+ try {
+ if (!Files.isReadable(file.toPath())) {
+ log.warn("文件不可访问: {}", file.getPath());
+ return false;
+ }
+ } catch (SecurityException e) {
+ log.error("检查文件权限时出错: {}", file.getPath(), e);
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * 严格检查:文件内容和时效性
+ */
+ private static boolean strictCheck(File file) {
+ // 检查文件内容
+ try (FileInputStream fis = new FileInputStream(file)) {
+ byte[] buffer = new byte[1024];
+ if (fis.read(buffer) == -1) {
+ //logger.warn("文件可能已损坏: {}", file.getPath());
+ return false;
+ }
+ } catch (IOException e) {
+ //logger.error("读取文件时出错: {}", file.getPath(), e);
+ return false;
+ }
+
+ // 检查文件时效性(示例:30天)
+ long lastModified = file.lastModified();
+ if (lastModified == 0L ||
+ System.currentTimeMillis() - lastModified > TimeUnit.DAYS.toMillis(30)) {
+ //logger.warn("文件可能已过期: {}", file.getPath());
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * 使用默认检查级别(NORMAL)检查文件
+ */
+ public static boolean isFileValid(String filePath) {
+ return isFileValid(filePath, CheckLevel.NORMAL);
+ }
+}
\ No newline at end of file
diff --git a/box-common/src/main/java/com/mathvision/box/common/utils/file/ImageUtils.java b/box-common/src/main/java/com/mathvision/box/common/utils/file/ImageUtils.java
new file mode 100644
index 0000000..5582738
--- /dev/null
+++ b/box-common/src/main/java/com/mathvision/box/common/utils/file/ImageUtils.java
@@ -0,0 +1,265 @@
+package com.mathvision.box.common.utils.file;
+
+
+import com.mathvision.box.common.config.AppConfig;
+import com.mathvision.box.common.constant.Constants;
+import com.mathvision.box.common.utils.common.StringUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.*;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Arrays;
+import java.util.Base64;
+
+/**
+ * 图片处理工具类
+ */
+public class ImageUtils
+{
+ private static final Logger log = LoggerFactory.getLogger(ImageUtils.class);
+
+ public static byte[] getImage(String imagePath)
+ {
+ InputStream is = getFile(imagePath);
+ try
+ {
+ return IOUtils.toByteArray(is);
+ }
+ catch (Exception e)
+ {
+ log.error("图片加载异常 {}", e);
+ return null;
+ }
+ finally
+ {
+ IOUtils.closeQuietly(is);
+ }
+ }
+
+ public static InputStream getFile(String imagePath)
+ {
+ try
+ {
+ byte[] result = readFile(imagePath);
+ result = Arrays.copyOf(result, result.length);
+ return new ByteArrayInputStream(result);
+ }
+ catch (Exception e)
+ {
+ log.error("获取图片异常 {}", e);
+ }
+ return null;
+ }
+
+ /**
+ * 读取文件为字节数据
+ *
+ * @param url 地址
+ * @return 字节数据
+ */
+ public static byte[] readFile(String url)
+ {
+ InputStream in = null;
+ try
+ {
+ if (url.startsWith("http"))
+ {
+ // 网络地址
+ URL urlObj = new URL(url);
+ URLConnection urlConnection = urlObj.openConnection();
+ urlConnection.setConnectTimeout(30 * 1000);
+ urlConnection.setReadTimeout(60 * 1000);
+ urlConnection.setDoInput(true);
+ in = urlConnection.getInputStream();
+ }
+ else
+ {
+ // 本机地址
+ String localPath = AppConfig.getProfile();
+ String downloadPath = localPath + StringUtils.substringAfter(url, Constants.RESOURCE_PREFIX);
+ in = new FileInputStream(downloadPath);
+ }
+ return IOUtils.toByteArray(in);
+ }
+ catch (Exception e)
+ {
+ log.error("获取文件路径异常 {}", e);
+ return null;
+ }
+ finally
+ {
+ IOUtils.closeQuietly(in);
+ }
+ }
+
+ /**
+ * 通过BufferedImage图片流调整图片大小
+ */
+ public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
+ Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_AREA_AVERAGING);
+ BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
+ outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
+ return outputImage;
+ }
+
+ /**
+ * 返回base64图片
+ * @param data
+ * @return
+ */
+ public static String imageToBase64(byte[] data) {
+ BASE64Encoder encoder = new BASE64Encoder();
+ // 返回Base64编码过的字节数组字符串
+ return encoder.encode(data);
+ }
+
+ /**
+ * base64转换成byte数组
+ * @param base64
+ * @return
+ * @throws IOException
+ */
+ public static byte[] base64ToByte(String base64) throws IOException {
+ BASE64Decoder decoder = new BASE64Decoder();
+ // 返回Base64编码过的字节数组字符串
+ return decoder.decodeBuffer(base64);
+ }
+
+ /**
+ * BufferedImage图片流转byte[]数组
+ */
+ public static byte[] imageToBytes(BufferedImage bImage) {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try {
+ ImageIO.write(bImage, "png", out);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return out.toByteArray();
+ }
+
+ /**
+ * byte[]数组转BufferedImage图片流
+ */
+ public static BufferedImage bytesToBufferedImage(byte[] ImageByte) {
+ ByteArrayInputStream in = new ByteArrayInputStream(ImageByte);
+ BufferedImage image = null;
+ try {
+ image = ImageIO.read(in);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return image;
+ }
+
+ /**
+ * 在线图片资源转base
+ * @param imageUrl
+ * @return
+ * @throws IOException
+ */
+ public static String convertToBase64(String imageUrl) throws IOException {
+ URL url = new URL(imageUrl);
+ String fileType = imageUrl.substring(imageUrl.length()-3);
+ String base64Str = "data:" + fileType + ";base64,";
+ InputStream inputStream = url.openStream();
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ byte[] buffer = new byte[4096];
+ int bytesRead;
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
+ outputStream.write(buffer, 0, bytesRead);
+ }
+ byte[] imageBytes = outputStream.toByteArray();
+ String base64String = base64Str + Base64.getEncoder().encodeToString(imageBytes);
+ return base64String;
+ }
+
+ //图片转化成base64字符串
+ public static String getImageStr(String imgPath) throws IOException {
+ File file = new File(imgPath);
+ String fileContentBase64 = null;
+ if(file.exists()){
+ String fileType = imgPath.substring(imgPath.length()-3);
+ String base64Str = "data:" + fileType + ";base64,";
+ String content = null;
+ //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
+ InputStream in = null;
+ byte[] data = null;
+ //读取图片字节数组
+ try {
+ in = new FileInputStream(file);
+ data = new byte[in.available()];
+ in.read(data);
+ in.close();
+ //对字节数组Base64编码
+ if (data == null || data.length == 0) {
+ return null;
+ }
+ //content = Base64.encodeBytes(data);
+ content = new BASE64Encoder().encode(data);
+ if (content == null || "".equals(content)) {
+ return null;
+ }
+ // 缩小图片
+ if (StringUtils.isNotBlank(content)) {
+ BufferedImage bufferedImage = ImageUtils.bytesToBufferedImage(ImageUtils.base64ToByte(content));
+ if (bufferedImage != null){
+ int height = bufferedImage.getHeight();
+ int width = bufferedImage.getWidth();
+ // 如果图片宽度大于650,图片缩放
+ if (width > 500) {
+ //高度等比缩放
+ height = (int)(height*500.0/width);
+ BufferedImage imgZoom = ImageUtils.resizeImage(bufferedImage, 500, height);
+ content = ImageUtils.imageToBase64(ImageUtils.imageToBytes(imgZoom));
+ }
+ }
+ }
+ fileContentBase64 = base64Str + content;
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ if (in != null) {
+ in.close();
+ }
+ }
+ }
+
+ return fileContentBase64;
+ }
+
+
+ /**
+ * 验证给定的字符串是否为有效的 Base64 编码。
+ *
+ * @param base64Str 要验证的字符串
+ * @return 如果是有效的 Base64 编码,则返回 true;否则,返回 false
+ */
+ public static boolean isValidBase64(String base64Str) {
+ if (base64Str == null || base64Str.isEmpty()) {
+ return false;
+ }
+ try {
+ // 移除 Base64 数据的前缀(如果存在)
+ String actualBase64Str = base64Str;
+ if (base64Str.contains(",")) {
+ actualBase64Str = base64Str.split(",")[1];
+ }
+
+ // 尝试解码
+ Base64.getDecoder().decode(actualBase64Str);
+ return true;
+ } catch (IllegalArgumentException e) {
+ // 捕获解码异常,说明不是有效的 Base64 字符串
+ return false;
+ }
+ }
+}
diff --git a/box-common/src/main/java/com/mathvision/box/common/utils/file/MimeTypeUtils.java b/box-common/src/main/java/com/mathvision/box/common/utils/file/MimeTypeUtils.java
new file mode 100644
index 0000000..1afb3e8
--- /dev/null
+++ b/box-common/src/main/java/com/mathvision/box/common/utils/file/MimeTypeUtils.java
@@ -0,0 +1,61 @@
+package com.mathvision.box.common.utils.file;
+
+/**
+ * @Author: fy
+ * @Date: 2024/03/22
+ * @Description:媒体类型工具类
+ */
+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",
+ // 音频格式
+ "mp3", "wav", "aac",
+ // 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 "";
+ }
+ }
+}
\ No newline at end of file
diff --git a/box-common/src/main/java/com/mathvision/box/common/utils/html/EscapeUtil.java b/box-common/src/main/java/com/mathvision/box/common/utils/html/EscapeUtil.java
new file mode 100644
index 0000000..11b2d23
--- /dev/null
+++ b/box-common/src/main/java/com/mathvision/box/common/utils/html/EscapeUtil.java
@@ -0,0 +1,166 @@
+package com.mathvision.box.common.utils.html;
+
+
+import com.mathvision.box.common.utils.common.StringUtils;
+
+/**
+ * 转义和反转义工具类
+ */
+public class EscapeUtil
+{
+ public static final String RE_HTML_MARK = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)";
+
+ private static final char[][] TEXT = new char[64][];
+
+ static
+ {
+ for (int i = 0; i < 64; i++)
+ {
+ TEXT[i] = new char[] { (char) i };
+ }
+
+ // special HTML characters
+ TEXT['\''] = "'".toCharArray(); // 单引号
+ TEXT['"'] = """.toCharArray(); // 双引号
+ TEXT['&'] = "&".toCharArray(); // &符
+ TEXT['<'] = "<".toCharArray(); // 小于号
+ TEXT['>'] = ">".toCharArray(); // 大于号
+ }
+
+ /**
+ * 转义文本中的HTML字符为安全的字符
+ *
+ * @param text 被转义的文本
+ * @return 转义后的文本
+ */
+ public static String escape(String text)
+ {
+ return encode(text);
+ }
+
+ /**
+ * 还原被转义的HTML特殊字符
+ *
+ * @param content 包含转义符的HTML内容
+ * @return 转换后的字符串
+ */
+ public static String unescape(String content)
+ {
+ return decode(content);
+ }
+
+ /**
+ * 清除所有HTML标签,但是不删除标签内的内容
+ *
+ * @param content 文本
+ * @return 清除标签后的文本
+ */
+ public static String clean(String content)
+ {
+ return new HTMLFilter().filter(content);
+ }
+
+ /**
+ * Escape编码
+ *
+ * @param text 被编码的文本
+ * @return 编码后的字符
+ */
+ private static String encode(String text)
+ {
+ if (StringUtils.isEmpty(text))
+ {
+ return StringUtils.EMPTY;
+ }
+
+ final StringBuilder tmp = new StringBuilder(text.length() * 6);
+ char c;
+ for (int i = 0; i < text.length(); i++)
+ {
+ c = text.charAt(i);
+ if (c < 256)
+ {
+ tmp.append("%");
+ if (c < 16)
+ {
+ tmp.append("0");
+ }
+ tmp.append(Integer.toString(c, 16));
+ }
+ else
+ {
+ tmp.append("%u");
+ if (c <= 0xfff)
+ {
+ // issue#I49JU8@Gitee
+ tmp.append("0");
+ }
+ tmp.append(Integer.toString(c, 16));
+ }
+ }
+ return tmp.toString();
+ }
+
+ /**
+ * Escape解码
+ *
+ * @param content 被转义的内容
+ * @return 解码后的字符串
+ */
+ public static String decode(String content)
+ {
+ if (StringUtils.isEmpty(content))
+ {
+ return content;
+ }
+
+ StringBuilder tmp = new StringBuilder(content.length());
+ int lastPos = 0, pos = 0;
+ char ch;
+ while (lastPos < content.length())
+ {
+ pos = content.indexOf("%", lastPos);
+ if (pos == lastPos)
+ {
+ if (content.charAt(pos + 1) == 'u')
+ {
+ ch = (char) Integer.parseInt(content.substring(pos + 2, pos + 6), 16);
+ tmp.append(ch);
+ lastPos = pos + 6;
+ }
+ else
+ {
+ ch = (char) Integer.parseInt(content.substring(pos + 1, pos + 3), 16);
+ tmp.append(ch);
+ lastPos = pos + 3;
+ }
+ }
+ else
+ {
+ if (pos == -1)
+ {
+ tmp.append(content.substring(lastPos));
+ lastPos = content.length();
+ }
+ else
+ {
+ tmp.append(content.substring(lastPos, pos));
+ lastPos = pos;
+ }
+ }
+ }
+ return tmp.toString();
+ }
+
+ public static void main(String[] args)
+ {
+ String html = "";
+ 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/box-common/src/main/java/com/mathvision/box/common/utils/html/HTMLFilter.java b/box-common/src/main/java/com/mathvision/box/common/utils/html/HTMLFilter.java
new file mode 100644
index 0000000..f37ce82
--- /dev/null
+++ b/box-common/src/main/java/com/mathvision/box/common/utils/html/HTMLFilter.java
@@ -0,0 +1,564 @@
+package com.mathvision.box.common.utils.html;
+
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * HTML过滤器,用于去除XSS漏洞隐患。
+ */
+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(""));
+ }
+ m.appendTail(buf);
+
+ return buf.toString();
+ }
+
+ private String balanceHTML(String s)
+ {
+ if (alwaysMakeTags)
+ {
+ //
+ // try and form html
+ //
+ s = regexReplace(P_END_ARROW, "", s);
+ // 不追加结束标签
+ s = regexReplace(P_BODY_TO_END, "<$1>", s);
+ s = regexReplace(P_XML_CONTENT, "$1<$2", s);
+
+ }
+ else
+ {
+ //
+ // escape stray brackets
+ //
+ s = regexReplace(P_STRAY_LEFT_ARROW, "<$1", s);
+ s = regexReplace(P_STRAY_RIGHT_ARROW, "$1$2><", s);
+
+ //
+ // the last regexp causes '<>' entities to appear
+ // (we need to do a lookahead assertion so that the last bracket can
+ // be used in the next pass of the regexp)
+ //
+ s = regexReplace(P_BOTH_ARROWS, "", s);
+ }
+
+ return s;
+ }
+
+ private String checkTags(String s)
+ {
+ Matcher m = P_TAGS.matcher(s);
+
+ final StringBuffer buf = new StringBuffer();
+ while (m.find())
+ {
+ String replaceStr = m.group(1);
+ replaceStr = processTag(replaceStr);
+ m.appendReplacement(buf, Matcher.quoteReplacement(replaceStr));
+ }
+ m.appendTail(buf);
+
+ // these get tallied in processTag
+ // (remember to reset before subsequent calls to filter method)
+ final StringBuilder sBuilder = new StringBuilder(buf.toString());
+ for (String key : vTagCounts.keySet())
+ {
+ for (int ii = 0; ii < vTagCounts.get(key); ii++)
+ {
+ sBuilder.append("").append(key).append(">");
+ }
+ }
+ s = sBuilder.toString();
+
+ return s;
+ }
+
+ private String processRemoveBlanks(final String s)
+ {
+ String result = s;
+ for (String tag : vRemoveBlanks)
+ {
+ if (!P_REMOVE_PAIR_BLANKS.containsKey(tag))
+ {
+ P_REMOVE_PAIR_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?>" + tag + ">"));
+ }
+ result = regexReplace(P_REMOVE_PAIR_BLANKS.get(tag), "", result);
+ if (!P_REMOVE_SELF_BLANKS.containsKey(tag))
+ {
+ P_REMOVE_SELF_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?/>"));
+ }
+ result = regexReplace(P_REMOVE_SELF_BLANKS.get(tag), "", result);
+ }
+
+ return result;
+ }
+
+ private static String regexReplace(final Pattern regex_pattern, final String replacement, final String s)
+ {
+ Matcher m = regex_pattern.matcher(s);
+ return m.replaceAll(replacement);
+ }
+
+ private String processTag(final String s)
+ {
+ // ending tags
+ Matcher m = P_END_TAG.matcher(s);
+ if (m.find())
+ {
+ final String name = m.group(1).toLowerCase();
+ if (allowed(name))
+ {
+ if (false == inArray(name, vSelfClosingTags))
+ {
+ if (vTagCounts.containsKey(name))
+ {
+ vTagCounts.put(name, vTagCounts.get(name) - 1);
+ return "" + name + ">";
+ }
+ }
+ }
+ }
+
+ // starting tags
+ m = P_START_TAG.matcher(s);
+ if (m.find())
+ {
+ final String name = m.group(1).toLowerCase();
+ final String body = m.group(2);
+ String ending = m.group(3);
+
+ // debug( "in a starting tag, name='" + name + "'; body='" + body + "'; ending='" + ending + "'" );
+ if (allowed(name))
+ {
+ final StringBuilder params = new StringBuilder();
+
+ final Matcher m2 = P_QUOTED_ATTRIBUTES.matcher(body);
+ final Matcher m3 = P_UNQUOTED_ATTRIBUTES.matcher(body);
+ final List 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 "";
+ }
+ }
+
+ // comments
+ m = P_COMMENT.matcher(s);
+ if (!stripComment && m.find())
+ {
+ return "<" + m.group() + ">";
+ }
+
+ return "";
+ }
+
+ private String processParamProtocol(String s)
+ {
+ s = decodeEntities(s);
+ final Matcher m = P_PROTOCOL.matcher(s);
+ if (m.find())
+ {
+ final String protocol = m.group(1);
+ if (!inArray(protocol, vAllowedProtocols))
+ {
+ // bad protocol, turn into local anchor link instead
+ s = "#" + s.substring(protocol.length() + 1);
+ if (s.startsWith("#//"))
+ {
+ s = "#" + s.substring(3);
+ }
+ }
+ }
+
+ return s;
+ }
+
+ private String decodeEntities(String s)
+ {
+ StringBuffer buf = new StringBuffer();
+
+ Matcher m = P_ENTITY.matcher(s);
+ while (m.find())
+ {
+ final String match = m.group(1);
+ final int decimal = Integer.decode(match).intValue();
+ m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
+ }
+ m.appendTail(buf);
+ s = buf.toString();
+
+ buf = new StringBuffer();
+ m = P_ENTITY_UNICODE.matcher(s);
+ while (m.find())
+ {
+ final String match = m.group(1);
+ final int decimal = Integer.valueOf(match, 16).intValue();
+ m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
+ }
+ m.appendTail(buf);
+ s = buf.toString();
+
+ buf = new StringBuffer();
+ m = P_ENCODE.matcher(s);
+ while (m.find())
+ {
+ final String match = m.group(1);
+ final int decimal = Integer.valueOf(match, 16).intValue();
+ m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
+ }
+ m.appendTail(buf);
+ s = buf.toString();
+
+ s = validateEntities(s);
+ return s;
+ }
+
+ private String validateEntities(final String s)
+ {
+ StringBuffer buf = new StringBuffer();
+
+ // validate entities throughout the string
+ Matcher m = P_VALID_ENTITIES.matcher(s);
+ while (m.find())
+ {
+ final String one = m.group(1); // ([^&;]*)
+ final String two = m.group(2); // (?=(;|&|$))
+ m.appendReplacement(buf, Matcher.quoteReplacement(checkEntity(one, two)));
+ }
+ m.appendTail(buf);
+
+ return encodeQuotes(buf.toString());
+ }
+
+ private String encodeQuotes(final String s)
+ {
+ if (encodeQuotes)
+ {
+ StringBuffer buf = new StringBuffer();
+ Matcher m = P_VALID_QUOTES.matcher(s);
+ while (m.find())
+ {
+ final String one = m.group(1); // (>|^)
+ final String two = m.group(2); // ([^<]+?)
+ final String three = m.group(3); // (<|$)
+ // 不替换双引号为",防止json格式无效 regexReplace(P_QUOTE, """, two)
+ m.appendReplacement(buf, Matcher.quoteReplacement(one + two + three));
+ }
+ m.appendTail(buf);
+ return buf.toString();
+ }
+ else
+ {
+ return s;
+ }
+ }
+
+ private String checkEntity(final String preamble, final String term)
+ {
+
+ return ";".equals(term) && isValidEntity(preamble) ? '&' + preamble : "&" + preamble;
+ }
+
+ private boolean isValidEntity(final String entity)
+ {
+ return inArray(entity, vAllowedEntities);
+ }
+
+ private static boolean inArray(final String s, final String[] array)
+ {
+ for (String item : array)
+ {
+ if (item != null && item.equals(s))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean allowed(final String name)
+ {
+ return (vAllowed.isEmpty() || vAllowed.containsKey(name)) && !inArray(name, vDisallowed);
+ }
+
+ private boolean allowedAttribute(final String name, final String paramName)
+ {
+ return allowed(name) && (vAllowed.isEmpty() || vAllowed.get(name).contains(paramName));
+ }
+}
\ No newline at end of file
diff --git a/box-common/src/main/java/com/mathvision/box/common/utils/http/HttpReqTemp.java b/box-common/src/main/java/com/mathvision/box/common/utils/http/HttpReqTemp.java
new file mode 100644
index 0000000..f1a8e5a
--- /dev/null
+++ b/box-common/src/main/java/com/mathvision/box/common/utils/http/HttpReqTemp.java
@@ -0,0 +1,340 @@
+package com.mathvision.box.common.utils.http;
+
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.lang.Nullable;
+import org.springframework.util.Assert;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+
+import java.util.Objects;
+import java.util.regex.Pattern;
+
+/**
+ * HTTP请求模板封装(线程不安全)
+ * 提供类型安全的HTTP请求配置,支持参数校验、防御性拷贝和流畅API
+ *
+ * 使用示例:
+ * {@code
+ * HttpReqTemp reqTemp = new HttpReqTemp()
+ * .withMethod(HttpMethod.POST)
+ * .withUrl("http://192.168.1.3/api/version")
+ * .withQueryParam()
+ * .withHeader("Content-Type", "application/json")
+ * .withMediaType(MediaType.APPLICATION_JSON)
+ * .withBody("{\"name\":\"John\",\"age\":30}")
+ * .withTimeout(120)
+ * .withRetryCount(3)
+ * .withExceptionStrategy(HttpReqTemp.ExceptionHandlingStrategy.FALLBACK)
+ * .withExample("{\"name\":\"John\",\"age\":30}");
+ * }
+ */
+public class HttpReqTemp {
+ private static final Pattern URL_PATTERN = Pattern.compile("^(https?|ftp)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
+ private static final int MAX_TIMEOUT = 10000;
+ private static final int MAX_RETRY = 10;
+
+ // 核心参数
+ private HttpMethod method;
+ private String url;
+ private MediaType mediaType;
+ private String body;
+
+ // 集合参数(防御性拷贝)
+ private MultiValueMap queryParams = new LinkedMultiValueMap<>();
+ private MultiValueMap headers = new LinkedMultiValueMap<>();
+
+ // 执行策略
+ private int timeout = 120;
+ private int retryCount = 1;
+ private ExceptionHandlingStrategy exceptionStrategy = ExceptionHandlingStrategy.DEFAULT;
+
+ // MOCK数据
+ private String example;
+
+ /**
+ * 参数有效性校验
+ *
+ * @throws IllegalStateException 当配置存在矛盾时抛出
+ */
+ public void validateConfig() {
+ if (method == HttpMethod.GET && body != null) {
+ throw new IllegalStateException("GET请求不能包含请求体");
+ }
+ if (mediaType == null && body != null) {
+ throw new IllegalStateException("包含请求体时必须指定MediaType");
+ }
+ }
+
+ /*----------------------- 核心参数配置 -----------------------*/
+
+ /**
+ * 设置HTTP方法
+ *
+ * @param method HTTP方法枚举,不可为空
+ */
+ public void setMethod(HttpMethod method) {
+ this.method = Objects.requireNonNull(method, "HTTP方法不能为空");
+ }
+
+ /**
+ * 设置请求URL
+ *
+ * @param url 符合格式的完整URL(支持http/https/ftp)
+ * @throws IllegalArgumentException 当URL格式无效时抛出
+ */
+ public void setUrl(String url) {
+ Assert.hasText(url, "URL不能为空");
+ if (!URL_PATTERN.matcher(url).matches()) {
+ throw new IllegalArgumentException("无效的URL格式: " + url);
+ }
+ this.url = url;
+ }
+
+ /**
+ * 设置媒体类型
+ *
+ * @param mediaType 标准MediaType枚举值,不可为空
+ */
+ public void setMediaType(MediaType mediaType) {
+ this.mediaType = Objects.requireNonNull(mediaType, "MediaType不能为空");
+ }
+
+ public void setBody(String body) {
+ this.body = body;
+ }
+
+ public void setExample(String example) {
+ this.example = example;
+ }
+
+ /*----------------------- 集合参数操作 -----------------------*/
+
+ /**
+ * 完全替换查询参数
+ *
+ * @param params 新的参数集合(自动防御性拷贝)
+ */
+ public void setQueryParams(@Nullable MultiValueMap params) {
+ this.queryParams = params != null ?
+ new LinkedMultiValueMap<>(params) :
+ new LinkedMultiValueMap<>();
+ }
+
+ /**
+ * 添加单个查询参数
+ *
+ * @param key 参数键(自动trim,不允许空值或包含非法字符)
+ * @param value 参数值(允许null)
+ */
+ public void addQueryParam(String key, @Nullable String value) {
+ validateParameterKey(key);
+ this.queryParams.add(key.trim(), value);
+ }
+
+ /**
+ * 完全替换请求头
+ *
+ * @param headers 新的头信息集合(自动防御性拷贝)
+ */
+ public void setHeaders(@Nullable MultiValueMap headers) {
+ this.headers = headers != null ?
+ new LinkedMultiValueMap<>(headers) :
+ new LinkedMultiValueMap<>();
+ }
+
+ /**
+ * 添加请求头
+ *
+ * @param key 头字段名(自动trim,需符合规范)
+ * @param value 头字段值(允许null)
+ */
+ public void addHeader(String key, @Nullable String value) {
+ validateHeaderKey(key);
+ this.headers.add(key.trim(), value);
+ }
+
+ /*----------------------- 策略配置 -----------------------*/
+
+ /**
+ * 设置超时时间
+ *
+ * @param seconds 超时时间(0-120秒)
+ */
+ public void setTimeout(int seconds) {
+ if (seconds < 0 || seconds > MAX_TIMEOUT) {
+ throw new IllegalArgumentException("超时时间需在0-" + MAX_TIMEOUT + "秒之间");
+ }
+ this.timeout = seconds;
+ }
+
+ /**
+ * 设置重试次数
+ *
+ * @param count 重试次数(0-10次)
+ */
+ public void setRetryCount(int count) {
+ if (count < 0 || count > MAX_RETRY) {
+ throw new IllegalArgumentException("重试次数需在0-" + MAX_RETRY + "次之间");
+ }
+ this.retryCount = count;
+ }
+
+ /**
+ * 设置异常处理策略
+ *
+ * @param strategy 异常处理策略枚举
+ */
+ public void setExceptionStrategy(ExceptionHandlingStrategy strategy) {
+ this.exceptionStrategy = Objects.requireNonNull(strategy);
+ }
+
+ /*----------------------- 校验方法 -----------------------*/
+
+ private void validateParameterKey(String key) {
+ Assert.hasText(key, "查询参数键不能为空");
+ if (key.contains(" ")) {
+ throw new IllegalArgumentException("参数键包含非法空格: " + key);
+ }
+ }
+
+ private void validateHeaderKey(String key) {
+ Assert.hasText(key, "请求头键不能为空");
+ String normalized = key.trim().toLowerCase();
+ if (normalized.equals("content-length")) {
+ throw new IllegalArgumentException("禁止手动设置Content-Length头");
+ }
+ }
+
+ /*----------------------- 流畅接口方法 -----------------------*/
+
+ public HttpReqTemp withMethod(HttpMethod method) {
+ setMethod(method);
+ return this;
+ }
+
+ public HttpReqTemp withUrl(String url) {
+ setUrl(url);
+ return this;
+ }
+
+ public HttpReqTemp withQueryParam(String key, String value) {
+ addQueryParam(key, value);
+ return this;
+ }
+
+ public HttpReqTemp withHeader(String key, String value) {
+ addHeader(key, value);
+ return this;
+ }
+
+ public HttpReqTemp withOauth(String token) {
+ addHeader("Authorization", "Bearer " + token);
+ return this;
+ }
+
+ public HttpReqTemp withMediaType(MediaType mediaType) {
+ setMediaType(mediaType);
+ return this;
+ }
+
+ public HttpReqTemp withTimeout(int seconds) {
+ setTimeout(seconds);
+ return this;
+ }
+
+ public HttpReqTemp withRetryCount(int count) {
+ setRetryCount(count);
+ return this;
+ }
+
+ public HttpReqTemp withExample(String example) {
+ setExample(example);
+ return this;
+ }
+
+ public HttpReqTemp withBody(String body) {
+ setBody(body);
+ return this;
+ }
+
+ /*----------------------- 枚举定义 -----------------------*/
+
+ /**
+ * 异常处理策略
+ */
+ public enum ExceptionHandlingStrategy {
+ /**
+ * 抛出原始异常
+ */
+ DEFAULT,
+ /**
+ * 返回预定义的示例数据
+ */
+ FALLBACK
+ }
+
+ /*----------------------- Getter方法 -----------------------*/
+
+ public HttpMethod getMethod() {
+ return method;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public MediaType getMediaType() {
+ if (mediaType == null) {
+ return MediaType.APPLICATION_JSON;
+ } else {
+ return mediaType;
+ }
+ }
+
+ public MultiValueMap getQueryParams() {
+ return new LinkedMultiValueMap<>(queryParams);
+ }
+
+ public MultiValueMap getHeaders() {
+ return new LinkedMultiValueMap<>(headers);
+ }
+
+ public int getTimeout() {
+ return timeout;
+ }
+
+ public int getRetryCount() {
+ return retryCount;
+ }
+
+ public ExceptionHandlingStrategy getExceptionStrategy() {
+ return exceptionStrategy;
+ }
+
+ public String getBody() {
+ return body;
+ }
+
+ public String getExample() {
+ return example;
+ }
+
+ @Override
+ public String toString() {
+ return "HttpReqTemp{" +
+ "method=" + method +
+ ", url='" + url + '\'' +
+ ", mediaType=" + mediaType +
+ ", body='" + body + '\'' +
+ ", queryParams=" + queryParams +
+ ", headers=" + headers +
+ ", timeout=" + timeout +
+ ", retryCount=" + retryCount +
+ ", exceptionStrategy=" + exceptionStrategy +
+ ", example='" + example + '\'' +
+ '}';
+ }
+}
+
+
diff --git a/box-common/src/main/java/com/mathvision/box/common/utils/http/HttpUtil.java b/box-common/src/main/java/com/mathvision/box/common/utils/http/HttpUtil.java
new file mode 100644
index 0000000..37470be
--- /dev/null
+++ b/box-common/src/main/java/com/mathvision/box/common/utils/http/HttpUtil.java
@@ -0,0 +1,246 @@
+package com.mathvision.box.common.utils.http;
+
+import com.mathvision.box.common.utils.common.Threads;
+import com.mathvision.box.common.utils.file.FileUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.*;
+import org.springframework.http.client.SimpleClientHttpRequestFactory;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.util.UriComponentsBuilder;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+
+/**
+ * http请求工具类
+ *
+ * 中文乱码问题:json = new String(json.getBytes("ISO-8859-1"), "UTF-8");
+ */
+@Slf4j
+public class HttpUtil {
+ /**
+ * get请求
+ *
+ * @param url
+ * @param params 请求参数
+ * @return
+ */
+ public static String get(String url, MultiValueMap params) {
+ return get(url, params, null);
+ }
+
+ /**
+ * get请求
+ *
+ * @param url
+ * @param params 请求参数
+ * @param headers 请求头
+ * @return
+ */
+ public static String get(String url, MultiValueMap params, MultiValueMap headers) {
+ return request(url, params, headers, HttpMethod.GET);
+ }
+
+ /**
+ * post请求
+ *
+ * @param url
+ * @param params 请求参数
+ * @return
+ */
+ public static String post(String url, MultiValueMap params) {
+ return post(url, params, null);
+ }
+
+ /**
+ * post请求
+ *
+ * @param url
+ * @param params 请求参数
+ * @param headers 请求头
+ * @return
+ */
+ public static String post(String url, MultiValueMap params, MultiValueMap headers) {
+ return request(url, params, headers, HttpMethod.POST);
+ }
+
+ /**
+ * put请求
+ *
+ * @param url
+ * @param params 请求参数
+ * @return
+ */
+ public static String put(String url, MultiValueMap params) {
+ return put(url, params, null);
+ }
+
+ /**
+ * put请求
+ *
+ * @param url
+ * @param params 请求参数
+ * @param headers 请求头
+ * @return
+ */
+ public static String put(String url, MultiValueMap params, MultiValueMap headers) {
+ return request(url, params, headers, HttpMethod.PUT);
+ }
+
+ /**
+ * delete请求
+ *
+ * @param url
+ * @param params 请求参数
+ * @return
+ */
+ public static String delete(String url, MultiValueMap params) {
+ return delete(url, params, null);
+ }
+
+ /**
+ * delete请求
+ *
+ * @param url
+ * @param params 请求参数
+ * @param headers 请求头
+ * @return
+ */
+ public static String delete(String url, MultiValueMap params, MultiValueMap headers) {
+ return request(url, params, headers, HttpMethod.DELETE);
+ }
+
+ /**
+ * 表单请求
+ *
+ * @param url
+ * @param params 请求参数
+ * @param headers 请求头
+ * @param method 请求方式
+ * @return
+ */
+ public static String request(String url, MultiValueMap params, MultiValueMap headers, HttpMethod method) {
+ if (params == null) {
+ params = new LinkedMultiValueMap<>();
+ }
+ return request(url, params, headers, method, MediaType.APPLICATION_FORM_URLENCODED);
+ }
+
+ /**
+ * http请求
+ *
+ * @param url
+ * @param params 请求参数
+ * @param headers 请求头
+ * @param method 请求方式
+ * @param mediaType 参数类型
+ * @return
+ */
+ public static String request(String url, Object params, MultiValueMap headers, HttpMethod method, MediaType mediaType) {
+ if (url == null || url.trim().isEmpty()) {
+ return null;
+ }
+ RestTemplate client = new RestTemplate();
+ // header
+ HttpHeaders httpHeaders = new HttpHeaders();
+ if (headers != null) {
+ httpHeaders.addAll(headers);
+ }
+ // 提交方式:表单、json
+ httpHeaders.setContentType(mediaType);
+ HttpEntity