|
@@ -0,0 +1,203 @@
|
|
|
+package org.jeecg.modules.utils;
|
|
|
+
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
+import org.jeecg.modules.exception.FileNameLengthLimitExceededException;
|
|
|
+import org.jeecg.modules.exception.FileSizeLimitExceededException;
|
|
|
+import org.jeecg.modules.exception.FileUploadTypeException;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Random;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author llp
|
|
|
+ * Date: 2025-01-17 14:22
|
|
|
+ * @Description:文件上传工具类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class FileUploadUtils {
|
|
|
+ /**
|
|
|
+ * 默认最大 大小 50M
|
|
|
+ */
|
|
|
+ public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认的文件名最大长度 200
|
|
|
+ */
|
|
|
+ public static final int DEFAULT_FILE_NAME_LENGTH = 200;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 资源映射路径 前缀
|
|
|
+ */
|
|
|
+ @Value("${file.prefix}")
|
|
|
+ public String localFilePrefix;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据文件路径上传
|
|
|
+ *
|
|
|
+ * @param baseDir 相对应用的基目录
|
|
|
+ * @param file 上传的文件
|
|
|
+ * @return 文件名称
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String upload(String baseDir, MultipartFile file) throws IOException {
|
|
|
+
|
|
|
+ try {
|
|
|
+ return upload(baseDir, file, MimeTypeUtils.IMAGE_EXTENSION);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new IOException(e.getMessage(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String upload(String baseDir, MultipartFile file, String[] allowedExtension) throws IOException {
|
|
|
+
|
|
|
+
|
|
|
+ int fileNameLength = 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);
|
|
|
+ File desc = getAbsoluteFile(baseDir, fileName);
|
|
|
+ file.transferTo(desc);
|
|
|
+ String pathFileName = getPathFileName(fileName);
|
|
|
+ return pathFileName;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String upload(String baseDir, String name, ByteArrayOutputStream byteArrayOutputStream) throws IOException {
|
|
|
+
|
|
|
+ FileOutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ name = DateUtils.datePath() + "/" + name;
|
|
|
+ File desc = getAbsoluteFile(baseDir, name);
|
|
|
+ outputStream = new FileOutputStream(desc);
|
|
|
+ outputStream.write(byteArrayOutputStream.toByteArray());
|
|
|
+ return getPathFileName(name);
|
|
|
+ } finally {
|
|
|
+ if (outputStream != null) {
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static String getPathFileName(String fileName) throws IOException {
|
|
|
+ String pathFileName = "/" + fileName;
|
|
|
+ return pathFileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
|
|
|
+ File desc = new File(uploadDir + File.separator + fileName);
|
|
|
+
|
|
|
+ if (!desc.exists()) {
|
|
|
+ if (!desc.getParentFile().exists()) {
|
|
|
+ boolean mkdirs = desc.getParentFile().mkdirs();
|
|
|
+ log.info("文件是否创建" + mkdirs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return desc;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编码文件名
|
|
|
+ */
|
|
|
+ public static final String extractFilename(MultipartFile file) {
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ String extension = getExtension(file);
|
|
|
+ fileName = DateUtils.datePath() + "/" + UUID.randomUUID().toString().substring(1, 10) + "." + extension;
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件大小校验
|
|
|
+ *
|
|
|
+ * @param file 上传的文件
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ public static final void assertAllowed(MultipartFile file, String[] allowedExtension) {
|
|
|
+ long size = file.getSize();
|
|
|
+ if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE) {
|
|
|
+ throw new FileSizeLimitExceededException("文件超出最大长度" + DEFAULT_MAX_SIZE / 1024 / 1024 + "M");
|
|
|
+ }
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ //后缀名
|
|
|
+ String extension = getExtension(file);
|
|
|
+
|
|
|
+ //判断上传的附件是否符合文件类型
|
|
|
+ if (!isAllowedExtension(extension, allowedExtension)) {
|
|
|
+ String join = String.join(",", allowedExtension);
|
|
|
+ throw new FileUploadTypeException("文件名字为:" + fileName + ",上传的文件格式不正确,正确格式包含" + join);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getRandomFileName() {
|
|
|
+
|
|
|
+ SimpleDateFormat simpleDateFormat;
|
|
|
+
|
|
|
+ simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
|
|
|
+
|
|
|
+ Date date = new Date();
|
|
|
+
|
|
|
+ String str = simpleDateFormat.format(date);
|
|
|
+
|
|
|
+ Random random = new Random();
|
|
|
+
|
|
|
+ // 获取5位随机数
|
|
|
+ int ranNum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;
|
|
|
+ // 当前时间
|
|
|
+ return ranNum + str;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件名的后缀
|
|
|
+ *
|
|
|
+ * @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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|