| | |
| | |
|
| | | 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.core.exception.file.FileException;
|
| | | import com.ruoyi.common.core.exception.file.FileNameLengthLimitExceededException;
|
| | | import com.ruoyi.common.core.exception.file.FileSizeLimitExceededException;
|
| | | import com.ruoyi.common.core.exception.file.InvalidExtensionException;
|
| | | import com.ruoyi.common.core.utils.DateUtils;
|
| | | import com.ruoyi.common.core.utils.StringUtils;
|
| | | import com.ruoyi.common.core.utils.file.FileTypeUtils;
|
| | | import com.ruoyi.common.core.utils.file.MimeTypeUtils;
|
| | | import com.ruoyi.common.core.utils.uuid.Seq;
|
| | |
|
| | |
| | | {
|
| | | return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
| | | }
|
| | | catch (FileException fe)
|
| | | {
|
| | | throw new IOException(fe.getDefaultMessage(), fe);
|
| | | }
|
| | | catch (Exception e)
|
| | | {
|
| | | throw new IOException(e.getMessage(), e);
|
| | |
| | |
|
| | | 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(fileName);
|
| | | }
|
| | |
|
| | |
| | | public static final String extractFilename(MultipartFile file)
|
| | | {
|
| | | return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
|
| | | FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
|
| | | FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), FileTypeUtils.getExtension(file));
|
| | | }
|
| | |
|
| | | private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
|
| | |
| | | }
|
| | |
|
| | | String fileName = file.getOriginalFilename();
|
| | | String extension = getExtension(file);
|
| | | String extension = FileTypeUtils.getExtension(file);
|
| | | if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
|
| | | {
|
| | | if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
|
| | |
| | | }
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取文件名的后缀
|
| | | * |
| | | * @param file 表单文件
|
| | | * @return 后缀名
|
| | | */
|
| | | public static final String getExtension(MultipartFile file)
|
| | | {
|
| | | String extension = FilenameUtils.getExtension(file.getOriginalFilename());
|
| | | if (StringUtils.isEmpty(extension))
|
| | | {
|
| | | extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
|
| | | }
|
| | | return extension;
|
| | | }
|
| | | } |