| | |
| | | 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 cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.core.util.IdUtil; |
| | | 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.InvalidExtensionException; |
| | | 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; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | |
| | | /** |
| | | * 文件上传工具类 |
| | |
| | | String fileName = extractFilename(file); |
| | | |
| | | File desc = getAbsoluteFile(baseDir, fileName); |
| | | file.transferTo(desc); |
| | | desc = FileUtil.touch(desc); |
| | | FileUtil.writeFromStream(file.getInputStream(), desc); |
| | | String pathFileName = getPathFileName(baseDir, fileName); |
| | | return pathFileName; |
| | | } |
| | |
| | | { |
| | | String fileName = file.getOriginalFilename(); |
| | | String extension = getExtension(file); |
| | | fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension; |
| | | fileName = DateUtils.datePath() + "/" + IdUtil.fastUUID() + "." + extension; |
| | | return fileName; |
| | | } |
| | | |
| | |
| | | { |
| | | 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; |
| | | } |
| | | |
| | |
| | | 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); |