| | |
| | | package com.ruoyi.common.utils.file; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | |
| | | import cn.hutool.core.lang.Validator; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | 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 com.ruoyi.common.utils.StringUtils; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | |
| | | /** |
| | | * 文件上传工具类 |
| | |
| | | { |
| | | File desc = new File(uploadDir + File.separator + fileName); |
| | | |
| | | if (!desc.getParentFile().exists()) |
| | | if (!desc.exists()) |
| | | { |
| | | desc.getParentFile().mkdirs(); |
| | | if (!desc.getParentFile().exists()) |
| | | { |
| | | desc.getParentFile().mkdirs(); |
| | | } |
| | | } |
| | | // 解决undertow文件上传bug,因底层实现不同,无需创建新文件 |
| | | // if (!desc.exists()) |
| | | // { |
| | | // desc.createNewFile(); |
| | | // } |
| | | 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 currentDir = StrUtil.subSuf(uploadDir, dirLastIndex); |
| | | String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName; |
| | | return pathFileName; |
| | | } |
| | |
| | | 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); |
| | |
| | | public static final String getExtension(MultipartFile file) |
| | | { |
| | | String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | if (StringUtils.isEmpty(extension)) |
| | | if (Validator.isEmpty(extension)) |
| | | { |
| | | extension = MimeTypeUtils.getExtension(file.getContentType()); |
| | | } |