| | |
| | | import java.nio.file.Paths; |
| | | import java.util.Objects; |
| | | |
| | | import com.ard.common.config.OssConfiguration; |
| | | import com.ard.common.config.UavConfig; |
| | | import com.ard.common.constant.Constants; |
| | | import com.ard.common.exception.file.FileNameLengthLimitExceededException; |
| | |
| | | /** |
| | | * 文件上传工具类 |
| | | * |
| | | * @author ruoyi |
| | | * @author ard |
| | | */ |
| | | public class FileUploadUtils { |
| | | /** |
| | |
| | | */ |
| | | private static String defaultBaseDir = UavConfig.getProfile(); |
| | | |
| | | /** |
| | | * Minio默认上传的地址 |
| | | */ |
| | | private static String bucketName = OssConfiguration.bucket; |
| | | |
| | | public static void setDefaultBaseDir(String defaultBaseDir) { |
| | | FileUploadUtils.defaultBaseDir = defaultBaseDir; |
| | |
| | | return defaultBaseDir; |
| | | } |
| | | |
| | | public static String getBucketName() { |
| | | return bucketName; |
| | | } |
| | | |
| | | /** |
| | | * 以默认配置进行文件上传 |
| | |
| | | return getPathFileName(baseDir, fileName); |
| | | } |
| | | |
| | | /** |
| | | * 以默认BucketName配置上传到Minio服务器 |
| | | * |
| | | * @param file 上传的文件 |
| | | * @return 文件名称 |
| | | * @throws Exception |
| | | */ |
| | | public static final String uploadMinio(MultipartFile file) throws IOException { |
| | | try { |
| | | return uploadMinino(getBucketName(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | } catch (Exception e) { |
| | | throw new IOException(e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 自定义bucketName配置上传到Minio服务器 |
| | | * |
| | | * @param file 上传的文件 |
| | | * @return 文件名称 |
| | | * @throws Exception |
| | | */ |
| | | public static final String uploadMinio(MultipartFile file, String bucketName) throws IOException { |
| | | try { |
| | | return uploadMinino(bucketName, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | } catch (Exception e) { |
| | | throw new IOException(e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | private static String uploadMinino(String bucketName, MultipartFile file, String[] allowedExtension) |
| | | throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, |
| | | InvalidExtensionException { |
| | | int fileNamelength = file.getOriginalFilename().length(); |
| | | if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) { |
| | | throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH); |
| | | } |
| | | assertAllowed(file, allowedExtension); |
| | | try { |
| | | String fileName = extractFilename(file); |
| | | String pathFileName = MinioUtil.uploadFile(bucketName, fileName, file); |
| | | return pathFileName; |
| | | } catch (Exception e) { |
| | | throw new IOException(e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 编码文件名 |