|
|
@ -13,6 +13,7 @@ import com.ruoyi.common.exception.file.FileSizeLimitExceededException; |
|
|
import com.ruoyi.common.exception.file.InvalidExtensionException; |
|
|
import com.ruoyi.common.exception.file.InvalidExtensionException; |
|
|
import com.ruoyi.common.utils.DateUtils; |
|
|
import com.ruoyi.common.utils.DateUtils; |
|
|
import com.ruoyi.common.utils.StringUtils; |
|
|
import com.ruoyi.common.utils.StringUtils; |
|
|
|
|
|
import com.ruoyi.common.utils.uuid.IdUtils; |
|
|
import com.ruoyi.common.utils.uuid.Seq; |
|
|
import com.ruoyi.common.utils.uuid.Seq; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -102,15 +103,35 @@ public class FileUploadUtils |
|
|
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, |
|
|
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, |
|
|
InvalidExtensionException |
|
|
InvalidExtensionException |
|
|
{ |
|
|
{ |
|
|
int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length(); |
|
|
return upload(baseDir, file, allowedExtension, false); |
|
|
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 文件上传 |
|
|
|
|
|
* |
|
|
|
|
|
* @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); |
|
|
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
assertAllowed(file, allowedExtension); |
|
|
assertAllowed(file, allowedExtension); |
|
|
|
|
|
|
|
|
String fileName = extractFilename(file); |
|
|
String fileName = useCustomNaming ? uuidFilename(file) : extractFilename(file); |
|
|
|
|
|
|
|
|
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath(); |
|
|
String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath(); |
|
|
file.transferTo(Paths.get(absPath)); |
|
|
file.transferTo(Paths.get(absPath)); |
|
|
@ -118,12 +139,19 @@ public class FileUploadUtils |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 编码文件名 |
|
|
* 编码文件名(日期格式目录 + 原文件名 + 序列值 + 后缀) |
|
|
*/ |
|
|
*/ |
|
|
public static final String extractFilename(MultipartFile file) |
|
|
public static final String extractFilename(MultipartFile file) |
|
|
{ |
|
|
{ |
|
|
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(), |
|
|
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(), FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file)); |
|
|
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 |
|
|
public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException |
|
|
|