forked from aixan/RuoYi-Vue
		
	用户头像更换后移除旧头像文件
This commit is contained in:
		| @@ -13,11 +13,12 @@ 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; | ||||
| import com.ruoyi.common.utils.uuid.Seq; | ||||
|  | ||||
| /** | ||||
|  * 文件上传工具类 | ||||
|  * | ||||
|  *  | ||||
|  * @author ruoyi | ||||
|  */ | ||||
| public class FileUploadUtils | ||||
| @@ -102,15 +103,35 @@ public class FileUploadUtils | ||||
|             throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, | ||||
|             InvalidExtensionException | ||||
|     { | ||||
|         int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length(); | ||||
|         if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) | ||||
|         return upload(baseDir, file, allowedExtension, false); | ||||
|     } | ||||
|      | ||||
|     /** | ||||
|      * 文件上传 | ||||
|      * | ||||
|      * @param baseDir 相对应用的基目录 | ||||
|      * @param file 上传的文件 | ||||
|      * @param useCustomNaming 系统自定义文件名 | ||||
|      * @param allowedExtension 上传文件类型 | ||||
|      * @return 返回上传成功的文件名 | ||||
|      * @throws FileSizeLimitExceededException 如果超出最大大小 | ||||
|      * @throws FileNameLengthLimitExceededException 文件名太长 | ||||
|      * @throws IOException 比如读写文件出错时 | ||||
|      * @throws InvalidExtensionException 文件校验异常 | ||||
|      */ | ||||
|     public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension, boolean useCustomNaming) | ||||
|             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 fileName = useCustomNaming ? uuidFilename(file) : extractFilename(file); | ||||
|  | ||||
|         String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath(); | ||||
|         file.transferTo(Paths.get(absPath)); | ||||
| @@ -118,12 +139,19 @@ public class FileUploadUtils | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 编码文件名 | ||||
|      * 编码文件名(日期格式目录 + 原文件名 + 序列值 + 后缀) | ||||
|      */ | ||||
|     public static final String extractFilename(MultipartFile file) | ||||
|     { | ||||
|         return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(), | ||||
|                 FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file)); | ||||
|         return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(), FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 编编码文件名(日期格式目录 + UUID + 后缀) | ||||
|      */ | ||||
|     public static final String uuidFilename(MultipartFile file) | ||||
|     { | ||||
|         return StringUtils.format("{}/{}.{}", DateUtils.datePath(), IdUtils.fastSimpleUUID(), getExtension(file)); | ||||
|     } | ||||
|  | ||||
|     public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException | ||||
| @@ -216,7 +244,7 @@ public class FileUploadUtils | ||||
|  | ||||
|     /** | ||||
|      * 获取文件名的后缀 | ||||
|      * | ||||
|      *  | ||||
|      * @param file 表单文件 | ||||
|      * @return 后缀名 | ||||
|      */ | ||||
|   | ||||
| @@ -11,13 +11,14 @@ import java.net.URLEncoder; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import jakarta.servlet.http.HttpServletRequest; | ||||
| import jakarta.servlet.http.HttpServletResponse; | ||||
| import org.apache.commons.io.FilenameUtils; | ||||
| import org.apache.commons.io.IOUtils; | ||||
| import org.apache.commons.lang3.ArrayUtils; | ||||
| import com.ruoyi.common.config.RuoYiConfig; | ||||
| import com.ruoyi.common.constant.Constants; | ||||
| import com.ruoyi.common.utils.DateUtils; | ||||
| import com.ruoyi.common.utils.StringUtils; | ||||
| import com.ruoyi.common.utils.uuid.IdUtils; | ||||
| import org.apache.commons.io.FilenameUtils; | ||||
|  | ||||
| /** | ||||
|  * 文件处理工具类 | ||||
| @@ -103,6 +104,17 @@ public class FileUtils | ||||
|         return FileUploadUtils.getPathFileName(uploadDir, pathName); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 移除路径中的请求前缀片段 | ||||
|      *  | ||||
|      * @param filePath 文件路径 | ||||
|      * @return 移除后的文件路径 | ||||
|      */ | ||||
|     public static String stripPrefix(String filePath) | ||||
|     { | ||||
|         return StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 删除文件 | ||||
|      *  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 RuoYi
					RuoYi