|
|
@@ -0,0 +1,158 @@
|
|
|
+package com.sckw.file.service;
|
|
|
+
|
|
|
+
|
|
|
+import com.sckw.core.utils.IdWorker;
|
|
|
+import com.sckw.core.utils.StringUtils;
|
|
|
+import com.sckw.core.web.constant.HttpStatus;
|
|
|
+import com.sckw.core.web.response.HttpResult;
|
|
|
+import com.sckw.excel.common.NumberConstant;
|
|
|
+import com.sckw.file.common.enums.FileEnum;
|
|
|
+import com.sckw.file.dao.KwsFileInfoDao;
|
|
|
+import com.sckw.file.model.FileInfo;
|
|
|
+import com.sckw.file.utils.FileUtils;
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author lfdc
|
|
|
+ * @version 1.0
|
|
|
+ * @className FileService
|
|
|
+ * @description 文件上传下载service
|
|
|
+ * @company sckw
|
|
|
+ * @date 2023-06-02 16:06:46
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class FileService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ KwsFileInfoDao fileInfoDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件至OSS
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public HttpResult uploadFile(MultipartFile file) {
|
|
|
+ HttpResult result = new HttpResult();
|
|
|
+ List<Map<String, Object>> resultList = new ArrayList<>();
|
|
|
+ result.setCode(HttpStatus.SUCCESS_CODE);
|
|
|
+ boolean isEmpty = file.isEmpty();
|
|
|
+ if (isEmpty) {
|
|
|
+ result.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
|
|
|
+ result.setMsg("上传请选择文件");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ //获取文件类型
|
|
|
+ String contentType = file.getContentType();
|
|
|
+ //获取上传文件的原始文件名
|
|
|
+ String oFileName = file.getOriginalFilename();
|
|
|
+ //文件大小
|
|
|
+ BigDecimal fileSize = FileUtils.getFileSize(file, "KB");
|
|
|
+ FileInfo fileInfo = new FileInfo();
|
|
|
+ fileInfo.setOriginalName(oFileName);
|
|
|
+ //获取文件后缀
|
|
|
+ fileInfo.setFileSuffix(FilenameUtils.getExtension(oFileName));
|
|
|
+ fileInfo.setFileSize(StringUtils.isBlank(fileSize) ? new BigDecimal("0") : fileSize);
|
|
|
+ Map<String, String> infoMap = FileUtils.uploadFileByInfo(file, FileEnum.DOCUMENT_ADDRESS);
|
|
|
+ FileInfo infoDo = new FileInfo();
|
|
|
+ infoDo.setId(new IdWorker(1).nextId());
|
|
|
+ infoDo.setType(FileEnum.FILE_STORE_TYPE_OSS.getFileType());
|
|
|
+ infoDo.setFileName(infoMap.get("fileName"));
|
|
|
+ infoDo.setMd5(infoMap.get("fileMd5"));
|
|
|
+ infoDo.setFilePath(infoMap.get("filePath"));
|
|
|
+ infoDo.setCreateTime(LocalDateTime.now());
|
|
|
+ infoDo.setUpdateTime(LocalDateTime.now());
|
|
|
+// int insert = fileInfoDao.insert(infoDo);
|
|
|
+ //上传至oss文件地址
|
|
|
+ if (StringUtils.isNotBlank(infoMap.get("filePath"))) {
|
|
|
+ String oosUrl = infoMap.get("filePath");
|
|
|
+ result.setCode(HttpStatus.SUCCESS_CODE);
|
|
|
+ result.setMsg("上传成功");
|
|
|
+ Map<String, Object> map = new HashMap<>(NumberConstant.SIXTEEN);
|
|
|
+ map.put("fileKey", infoMap.get("filePath"));
|
|
|
+ map.put("fileName", infoMap.get("fileName"));
|
|
|
+ resultList.add(map);
|
|
|
+ result.setData(resultList);
|
|
|
+ } else {
|
|
|
+ result.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
|
|
|
+ result.setMsg("上传请选择文件");
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量上传文件至OSS
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Async
|
|
|
+ public HttpResult uploadFileList(MultipartFile[] file) {
|
|
|
+ HttpResult result = new HttpResult();
|
|
|
+ List<Map<String, Object>> resultList = new ArrayList<>();
|
|
|
+ if (!ObjectUtils.isEmpty(file) && file.length > 0) {
|
|
|
+ List<MultipartFile> multipartFiles = Arrays.asList(file);
|
|
|
+ for (MultipartFile multipartFile : multipartFiles) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ //文件大小
|
|
|
+ String fileSize = FileUtils.getFileSize(multipartFile);
|
|
|
+ //文件名称
|
|
|
+ String originalFilename = multipartFile.getOriginalFilename();
|
|
|
+ map.put("code", HttpStatus.SUCCESS_CODE);
|
|
|
+ map.put("fileName", originalFilename);
|
|
|
+ map.put("message", HttpStatus.SUCCESS_MESSAGE);
|
|
|
+ String url = null;
|
|
|
+ //上传文件是否成功
|
|
|
+ try {
|
|
|
+ url = FileUtils.uploadFile(multipartFile, FileEnum.DOCUMENT_ADDRESS);
|
|
|
+ } catch (Exception e) {
|
|
|
+ map.put("code", HttpStatus.GLOBAL_EXCEPTION_CODE);
|
|
|
+ map.put("fileName", originalFilename);
|
|
|
+ map.put("message", e);
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(url)) {
|
|
|
+ map.put("code", HttpStatus.GLOBAL_EXCEPTION_CODE);
|
|
|
+ map.put("fileName", originalFilename);
|
|
|
+ map.put("message", HttpStatus.SUCCESS_MESSAGE);
|
|
|
+ }
|
|
|
+ //上传至oss文件地址
|
|
|
+ String oosUrl = url;
|
|
|
+ resultList.add(map);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ result.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
|
|
|
+ result.setMsg("上传选择文件为空");
|
|
|
+ }
|
|
|
+ result.setData(resultList);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * OSS下载文件/获取文件地址
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void fileDownload(String fileName) {
|
|
|
+ HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
|
|
|
+ FileUtils.downloadByUrl(response,fileName);
|
|
|
+ FileUtils.downOSSFile(fileName, response);
|
|
|
+// FileUtils.downloadByFileName(fileName);
|
|
|
+ }
|
|
|
+}
|