浏览代码

文件上传blob类型处理

RuoYi 5 年之前
父节点
当前提交
30cb122bdf

+ 34 - 36
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java

@@ -9,6 +9,7 @@ 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.security.Md5Utils;
 
 /**
@@ -33,30 +34,6 @@ public class FileUploadUtils
      */
     private static String defaultBaseDir = Global.getProfile();
 
-    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[] DEFAULT_ALLOWED_EXTENSION = {
-            //图片
-            "bmp", "gif", "jpg", "jpeg", "png",
-            //word excel powerpoint
-            "doc", "docx", "xls", "xlsx", "ppt", "pptx",
-            "html", "htm", "txt",
-            //压缩文件
-            "rar", "zip", "gz", "bz2",
-            //pdf
-            "pdf"
-    };
-
     private static int counter = 0;
 
     public static void setDefaultBaseDir(String defaultBaseDir)
@@ -80,7 +57,7 @@ public class FileUploadUtils
     {
         try
         {
-            return upload(getDefaultBaseDir(), file, DEFAULT_ALLOWED_EXTENSION);
+            return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
         }
         catch (Exception e)
         {
@@ -100,7 +77,7 @@ public class FileUploadUtils
     {
         try
         {
-            return upload(baseDir, file, DEFAULT_ALLOWED_EXTENSION);
+            return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
         }
         catch (Exception e)
         {
@@ -121,7 +98,8 @@ public class FileUploadUtils
      * @throws InvalidExtensionException 文件校验异常
      */
     public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
-            throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, InvalidExtensionException
+            throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
+            InvalidExtensionException
     {
         int fileNamelength = file.getOriginalFilename().length();
         if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
@@ -144,7 +122,7 @@ public class FileUploadUtils
     public static final String extractFilename(MultipartFile file)
     {
         String filename = file.getOriginalFilename();
-        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
+        String extension = getExtension(file);
         filename = DateUtils.datePath() + File.separator + encodingFilename(filename) + "." + extension;
         return filename;
     }
@@ -180,7 +158,7 @@ public class FileUploadUtils
      * @param file 上传的文件
      * @return
      * @throws FileSizeLimitExceededException 如果超出最大大小
-     * @throws InvalidExtensionException 
+     * @throws InvalidExtensionException
      */
     public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
             throws FileSizeLimitExceededException, InvalidExtensionException
@@ -192,20 +170,23 @@ public class FileUploadUtils
         }
 
         String filename = file.getOriginalFilename();
-        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
+        String extension = getExtension(file);
         if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
         {
-            if (allowedExtension == IMAGE_EXTENSION)
+            if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
             {
-                throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension, filename);
+                throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
+                        filename);
             }
-            else if (allowedExtension == FLASH_EXTENSION)
+            else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
             {
-                throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, filename);
+                throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
+                        filename);
             }
-            else if (allowedExtension == MEDIA_EXTENSION)
+            else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
             {
-                throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension, filename);
+                throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
+                        filename);
             }
             else
             {
@@ -233,4 +214,21 @@ public class FileUploadUtils
         }
         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;
+    }
+
 }

+ 55 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MimeTypeUtils.java

@@ -0,0 +1,55 @@
+package com.ruoyi.common.utils.file;
+
+/**
+ * 媒体类型工具类
+ * 
+ * @author ruoyi
+ */
+public class MimeTypeUtils
+{
+    public static final String IMAGE_PNG = "image/png";
+
+    public static final String IMAGE_JPG = "image/jpg";
+
+    public static final String IMAGE_JPEG = "image/jpeg";
+
+    public static final String IMAGE_BMP = "image/bmp";
+
+    public static final String IMAGE_GIF = "image/gif";
+    
+    public static final String[] IMAGE_EXTENSION = { "bmp", "gif", "jpg", "jpeg", "png" };
+
+    public static final String[] FLASH_EXTENSION = { "swf", "flv" };
+
+    public static final String[] MEDIA_EXTENSION = { "swf", "flv", "mp3", "wav", "wma", "wmv", "mid", "avi", "mpg",
+            "asf", "rm", "rmvb" };
+
+    public static final String[] DEFAULT_ALLOWED_EXTENSION = {
+            // 图片
+            "bmp", "gif", "jpg", "jpeg", "png",
+            // word excel powerpoint
+            "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
+            // 压缩文件
+            "rar", "zip", "gz", "bz2",
+            // 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 "";
+        }
+    }
+}