From feb7c85d57c5e47fd40f87ee94c707fc61042e04 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期三, 16 六月 2021 13:29:33 +0800
Subject: [PATCH] update mybatis-plus 升级 3.4.3.1 fixbug 版本
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java | 466 +++++++++++++++++++++++++++++----------------------------
1 files changed, 239 insertions(+), 227 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java
index daa9498..22d53c7 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java
@@ -1,227 +1,239 @@
-package com.ruoyi.common.utils.file;
-
-import java.io.File;
-import java.io.IOException;
-import org.apache.commons.io.FilenameUtils;
-import org.springframework.web.multipart.MultipartFile;
-import com.ruoyi.common.config.RuoYiConfig;
-import com.ruoyi.common.constant.Constants;
-import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
-import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
-import com.ruoyi.common.exception.file.InvalidExtensionException;
-import com.ruoyi.common.utils.DateUtils;
-import com.ruoyi.common.utils.StringUtils;
-import com.ruoyi.common.utils.uuid.IdUtils;
-
-/**
- * 鏂囦欢涓婁紶宸ュ叿绫�
- *
- * @author ruoyi
- */
-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 = RuoYiConfig.getProfile();
-
- public static void setDefaultBaseDir(String defaultBaseDir)
- {
- FileUploadUtils.defaultBaseDir = defaultBaseDir;
- }
-
- public static String getDefaultBaseDir()
- {
- return defaultBaseDir;
- }
-
- /**
- * 浠ラ粯璁ら厤缃繘琛屾枃浠朵笂浼�
- *
- * @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 = 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);
-
- File desc = getAbsoluteFile(baseDir, fileName);
- file.transferTo(desc);
- String pathFileName = getPathFileName(baseDir, fileName);
- return pathFileName;
- }
-
- /**
- * 缂栫爜鏂囦欢鍚�
- */
- public static final String extractFilename(MultipartFile file)
- {
- String fileName = file.getOriginalFilename();
- String extension = getExtension(file);
- fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
- return fileName;
- }
-
- private 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;
- }
-
- private static final String getPathFileName(String uploadDir, String fileName) throws IOException
- {
- int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
- String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
- String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
- return pathFileName;
- }
-
- /**
- * 鏂囦欢澶у皬鏍¢獙
- *
- * @param file 涓婁紶鐨勬枃浠�
- * @return
- * @throws FileSizeLimitExceededException 濡傛灉瓒呭嚭鏈�澶уぇ灏�
- * @throws InvalidExtensionException
- */
- public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
- throws FileSizeLimitExceededException, InvalidExtensionException
- {
- long size = file.getSize();
- if (DEFAULT_MAX_SIZE != -1 && 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
- {
- 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(file.getContentType());
- }
- return extension;
- }
-}
+package com.ruoyi.common.utils.file;
+
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.lang.Validator;
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.StrUtil;
+import com.ruoyi.common.config.RuoYiConfig;
+import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
+import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
+import com.ruoyi.common.exception.file.InvalidExtensionException;
+import com.ruoyi.common.utils.DateUtils;
+import org.apache.commons.io.FilenameUtils;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * 鏂囦欢涓婁紶宸ュ叿绫�
+ *
+ * @author ruoyi
+ */
+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 = RuoYiConfig.getProfile();
+
+ public static void setDefaultBaseDir(String defaultBaseDir)
+ {
+ FileUploadUtils.defaultBaseDir = defaultBaseDir;
+ }
+
+ public static String getDefaultBaseDir()
+ {
+ return defaultBaseDir;
+ }
+
+ /**
+ * 浠ラ粯璁ら厤缃繘琛屾枃浠朵笂浼�
+ *
+ * @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 = 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);
+
+ File desc = getAbsoluteFile(baseDir, fileName);
+ desc = FileUtil.touch(desc);
+ FileUtil.writeFromStream(file.getInputStream(), desc);
+ String pathFileName = getPathFileName(baseDir, fileName);
+ return pathFileName;
+ }
+
+ /**
+ * 缂栫爜鏂囦欢鍚�
+ */
+ public static final String extractFilename(MultipartFile file)
+ {
+ String fileName = file.getOriginalFilename();
+ String extension = getExtension(file);
+ fileName = DateUtils.datePath() + "/" + IdUtil.fastUUID() + "." + extension;
+ return fileName;
+ }
+
+ private 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;
+ }
+
+ private static final String getPathFileName(String uploadDir, String fileName) throws IOException
+ {
+ int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
+ String currentDir = StrUtil.subSuf(uploadDir, dirLastIndex);
+ String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
+ return pathFileName;
+ }
+
+ /**
+ * 鏂囦欢澶у皬鏍¢獙
+ *
+ * @param file 涓婁紶鐨勬枃浠�
+ * @return
+ * @throws FileSizeLimitExceededException 濡傛灉瓒呭嚭鏈�澶уぇ灏�
+ * @throws InvalidExtensionException
+ */
+ public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
+ throws FileSizeLimitExceededException, InvalidExtensionException
+ {
+ long size = file.getSize();
+ if (DEFAULT_MAX_SIZE != -1 && 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 (Validator.isEmpty(extension))
+ {
+ extension = MimeTypeUtils.getExtension(file.getContentType());
+ }
+ return extension;
+ }
+}
--
Gitblit v1.9.3