| | |
| | |
|
| | | import java.io.File;
|
| | | import java.io.IOException;
|
| | | import java.nio.file.Paths;
|
| | | import java.util.Objects;
|
| | | import org.apache.commons.io.FilenameUtils;
|
| | | import org.springframework.web.multipart.MultipartFile;
|
| | |
| | | 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.IdUtils;
|
| | | import com.ruoyi.common.utils.StringUtils;
|
| | | import com.ruoyi.common.utils.uuid.Seq;
|
| | | import com.ruoyi.framework.config.RuoYiConfig;
|
| | |
|
| | | /**
|
| | |
| | | throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
| | | InvalidExtensionException
|
| | | {
|
| | | int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
|
| | | if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
|
| | | int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length();
|
| | | if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
|
| | | {
|
| | | throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
|
| | | }
|
| | |
| | |
|
| | | String fileName = extractFilename(file);
|
| | |
|
| | | File desc = getAbsoluteFile(baseDir, fileName);
|
| | | file.transferTo(desc);
|
| | | String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
|
| | | file.transferTo(Paths.get(absPath));
|
| | | return getPathFileName(baseDir, fileName);
|
| | | }
|
| | |
|
| | |
| | | */
|
| | | public static final String extractFilename(MultipartFile file)
|
| | | {
|
| | | String fileName = file.getOriginalFilename();
|
| | | String extension = getExtension(file);
|
| | | fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
|
| | | return fileName;
|
| | | return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
|
| | | FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
|
| | | }
|
| | |
|
| | | public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
|
| | |
| | | throw new InvalidExtensionException(allowedExtension, extension, fileName);
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | /**
|