From ca116c01664dae9fdeb0e5c8f2e7e11f8c7eed2a Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 17 May 2019 08:49:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=B8=8A=E4=BC=A0=E4=BB=BB?= =?UTF-8?q?=E6=84=8F=E6=A0=BC=E5=BC=8F=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/file/FileUploadUtils.java | 101 +++++++++++++++--- 1 file changed, 85 insertions(+), 16 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 8e8d77bd..496fb542 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 @@ -2,10 +2,12 @@ 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.Global; 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.security.Md5Utils; @@ -31,10 +33,29 @@ public class FileUploadUtils */ private static String defaultBaseDir = Global.getProfile(); - /** - * 默认文件类型jpg - */ - public static final String IMAGE_JPG_EXTENSION = ".jpg"; + 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; @@ -59,7 +80,7 @@ public class FileUploadUtils { try { - return upload(getDefaultBaseDir(), file, FileUploadUtils.IMAGE_JPG_EXTENSION); + return upload(getDefaultBaseDir(), file, DEFAULT_ALLOWED_EXTENSION); } catch (Exception e) { @@ -79,7 +100,7 @@ public class FileUploadUtils { try { - return upload(baseDir, file, FileUploadUtils.IMAGE_JPG_EXTENSION); + return upload(baseDir, file, DEFAULT_ALLOWED_EXTENSION); } catch (Exception e) { @@ -97,36 +118,40 @@ public class FileUploadUtils * @throws FileSizeLimitExceededException 如果超出最大大小 * @throws FileNameLengthLimitExceededException 文件名太长 * @throws IOException 比如读写文件出错时 + * @throws InvalidExtensionException 文件校验异常 */ - public static final String upload(String baseDir, MultipartFile file, String extension) - throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException + 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); + assertAllowed(file, allowedExtension); - String fileName = extractFilename(file, extension); + String fileName = extractFilename(file); - File desc = getAbsoluteFile(baseDir, baseDir + fileName); + File desc = getAbsoluteFile(baseDir, fileName); file.transferTo(desc); return fileName; } - public static final String extractFilename(MultipartFile file, String extension) + /** + * 编码文件名 + */ + public static final String extractFilename(MultipartFile file) { String filename = file.getOriginalFilename(); - filename = DateUtils.datePath() + "/" + encodingFilename(filename) + extension; + String extension = FilenameUtils.getExtension(file.getOriginalFilename()); + filename = DateUtils.datePath() + File.separator + encodingFilename(filename) + "." + extension; return filename; } private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException { - File desc = new File(File.separator + filename); + File desc = new File(uploadDir + File.separator + filename); if (!desc.getParentFile().exists()) { @@ -155,13 +180,57 @@ public class FileUploadUtils * @param file 上传的文件 * @return * @throws FileSizeLimitExceededException 如果超出最大大小 + * @throws InvalidExtensionException */ - public static final void assertAllowed(MultipartFile file) throws FileSizeLimitExceededException + 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 = FilenameUtils.getExtension(file.getOriginalFilename()); + if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) + { + if (allowedExtension == IMAGE_EXTENSION) + { + throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension, filename); + } + else if (allowedExtension == FLASH_EXTENSION) + { + throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, filename); + } + else if (allowedExtension == 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; } }