|
|
@@ -1,70 +1,114 @@
|
|
|
-package com.xjrsoft.common.utils;
|
|
|
-
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
-import com.xjrsoft.common.factory.OssFactory;
|
|
|
-import okhttp3.OkHttpClient;
|
|
|
-import okhttp3.Request;
|
|
|
-import okhttp3.ResponseBody;
|
|
|
-import org.apache.commons.collections.CollectionUtils;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-
|
|
|
-import java.io.InputStream;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
-
|
|
|
-public class UploadUtil {
|
|
|
-
|
|
|
- //单文件上传
|
|
|
- public static String uploadFile(MultipartFile file) throws Exception {
|
|
|
- if (file.isEmpty()) {
|
|
|
- throw new RuntimeException("上传文件不能为空");
|
|
|
- }
|
|
|
- //上传文件
|
|
|
- String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(StringPool.DOT));
|
|
|
- return Objects.requireNonNull(OssFactory.build()).uploadSuffix(file.getBytes(), suffix);
|
|
|
- }
|
|
|
-
|
|
|
- //多文件上传
|
|
|
- public static List<String> uploadFiles(List<MultipartFile> files) throws Exception {
|
|
|
- if (CollectionUtils.isNotEmpty(files)) {
|
|
|
- throw new RuntimeException("上传文件不能为空");
|
|
|
- }
|
|
|
- List<String> urls = new ArrayList<>();
|
|
|
- //上传文件
|
|
|
- for (MultipartFile file : files) {
|
|
|
- urls.add(uploadFile(file));
|
|
|
- }
|
|
|
- return urls;
|
|
|
- }
|
|
|
-
|
|
|
- public static InputStream download(String path) {
|
|
|
- try {
|
|
|
- OkHttpClient client = new OkHttpClient();
|
|
|
- Request req = new Request.Builder().url(path).build();
|
|
|
- InputStream inputStream = null;
|
|
|
- okhttp3.Response resp = client.newCall(req).execute();
|
|
|
- if (resp.isSuccessful()) {
|
|
|
- ResponseBody body = resp.body();
|
|
|
- inputStream = body.byteStream();
|
|
|
- }
|
|
|
- return inputStream;
|
|
|
- } catch (Exception e){
|
|
|
- throw new RuntimeException("获取文件失败,请检查配置信息", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static boolean delete(String path) {
|
|
|
- return Objects.requireNonNull(OssFactory.build()).delete(path);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static String uploadFileByte(String fileName, byte[] file) throws Exception {
|
|
|
- if (file.length == 0) {
|
|
|
- throw new RuntimeException("上传文件不能为空");
|
|
|
- }
|
|
|
- //上传文件
|
|
|
- String suffix = Objects.requireNonNull(fileName).substring(fileName.lastIndexOf(StringPool.DOT));
|
|
|
- return Objects.requireNonNull(OssFactory.build()).uploadSuffix(file, suffix);
|
|
|
- }
|
|
|
-}
|
|
|
+package com.xjrsoft.common.utils;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
+import com.xjrsoft.common.constant.GlobalConstant;
|
|
|
+import com.xjrsoft.common.exception.MyException;
|
|
|
+import com.xjrsoft.common.factory.OssFactory;
|
|
|
+import com.xjrsoft.config.FileCheckRuleConfig;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.ResponseBody;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+public class UploadUtil {
|
|
|
+
|
|
|
+ //单文件上传
|
|
|
+ public static String uploadFile(MultipartFile file) throws Exception {
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ throw new RuntimeException("上传文件不能为空");
|
|
|
+ }
|
|
|
+ //上传文件
|
|
|
+ String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(StringPool.DOT));
|
|
|
+ return Objects.requireNonNull(OssFactory.build()).uploadSuffix(file.getBytes(), suffix);
|
|
|
+ }
|
|
|
+
|
|
|
+ //多文件上传
|
|
|
+ public static List<String> uploadFiles(List<MultipartFile> files) throws Exception {
|
|
|
+ if (CollectionUtils.isNotEmpty(files)) {
|
|
|
+ throw new RuntimeException("上传文件不能为空");
|
|
|
+ }
|
|
|
+ List<String> urls = new ArrayList<>();
|
|
|
+ //上传文件
|
|
|
+ for (MultipartFile file : files) {
|
|
|
+ urls.add(uploadFile(file));
|
|
|
+ }
|
|
|
+ return urls;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static InputStream download(String path) {
|
|
|
+ try {
|
|
|
+ OkHttpClient client = new OkHttpClient();
|
|
|
+ Request req = new Request.Builder().url(path).build();
|
|
|
+ InputStream inputStream = null;
|
|
|
+ okhttp3.Response resp = client.newCall(req).execute();
|
|
|
+ if (resp.isSuccessful()) {
|
|
|
+ ResponseBody body = resp.body();
|
|
|
+ inputStream = body.byteStream();
|
|
|
+ }
|
|
|
+ return inputStream;
|
|
|
+ } catch (Exception e){
|
|
|
+ throw new RuntimeException("获取文件失败,请检查配置信息", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean delete(String path) {
|
|
|
+ return Objects.requireNonNull(OssFactory.build()).delete(path);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String uploadFileByte(String fileName, byte[] file) throws Exception {
|
|
|
+ if (file.length == 0) {
|
|
|
+ throw new RuntimeException("上传文件不能为空");
|
|
|
+ }
|
|
|
+ //上传文件
|
|
|
+ String suffix = Objects.requireNonNull(fileName).substring(fileName.lastIndexOf(StringPool.DOT));
|
|
|
+ return Objects.requireNonNull(OssFactory.build()).uploadSuffix(file, suffix);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验文件
|
|
|
+ * @param file 上传的文件
|
|
|
+ * @param rule 校验规则
|
|
|
+ * @throws MyException 校验失败抛出异常
|
|
|
+ */
|
|
|
+ public static void fileTypeValidate(MultipartFile file, FileCheckRuleConfig rule) {
|
|
|
+ // 2. 文件名校验
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ if (StringUtils.isBlank(originalFilename)) {
|
|
|
+ throw new MyException("文件名不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 文件扩展名校验
|
|
|
+ String fileExtension = getFileExtension(originalFilename);
|
|
|
+ if (!rule.getAllowedExtensions().isEmpty() &&
|
|
|
+ !rule.getAllowedExtensions().contains(fileExtension.toLowerCase())) {
|
|
|
+ throw new MyException("不支持的文件类型,仅支持: " +
|
|
|
+ String.join(", ", rule.getAllowedExtensions()));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. MIME类型校验
|
|
|
+ String contentType = file.getContentType();
|
|
|
+ if (!rule.getAllowedMimeTypes().isEmpty() &&
|
|
|
+ !rule.getAllowedMimeTypes().contains(contentType)) {
|
|
|
+ throw new MyException("文件类型不匹配,仅支持: " +
|
|
|
+ String.join(", ", rule.getAllowedMimeTypes()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 安全的获取扩展名方法
|
|
|
+ private static String getFileExtension(String filename) {
|
|
|
+ // 处理没有扩展名的情况
|
|
|
+ int dotIndex = filename.lastIndexOf('.');
|
|
|
+ if (dotIndex < 0) {
|
|
|
+ throw new MyException("文件缺少扩展名");
|
|
|
+ }
|
|
|
+ return filename.substring(dotIndex + 1).toLowerCase();
|
|
|
+ }
|
|
|
+}
|