From 30cb122bdf0c44e2dd2f082a8611fc678111cc51 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 17 May 2019 17:35:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0blob?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/file/FileUploadUtils.java | 70 +++++++++---------- .../common/utils/file/MimeTypeUtils.java | 55 +++++++++++++++ 2 files changed, 89 insertions(+), 36 deletions(-) create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MimeTypeUtils.java 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 496fb5425..8fc602fe2 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 @@ -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; + } + } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MimeTypeUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/MimeTypeUtils.java new file mode 100644 index 000000000..698464128 --- /dev/null +++ b/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 ""; + } + } +}