sgj
2025-12-05 e34442d2f1fee9bebc3490cafdc0f01280eb8b1e
fzzy-igdss-web/src/main/java/com/fzzy/sys/manager/file/FileManager.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,133 @@
package com.fzzy.sys.manager.file;
import com.fzzy.igds.constant.RespCodeEnum;
import com.fzzy.igds.data.PageResponse;
import com.fzzy.igds.domain.Dept;
import com.fzzy.igds.service.CoreDeptService;
import com.fzzy.igds.service.FileService;
import com.fzzy.igds.utils.ContextUtil;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
/**
 * @Description å‡ºå…¥åº“设备相关功能
 * @Author CZT
 * @Date 2025/12/4 9:27
 */
@Slf4j
@Component("file.fileManager")
public class FileManager {
   @Resource
   private CoreDeptService coreDeptService;
   @Resource
   private FileService fileService;
   /**
    * æ ¹æ®å•位ID获取单位信息
    *
    * @param deptId
    * @return
    */
   public String getDeptFile(String deptId) {
      if (StringUtils.isEmpty(deptId)) {
         return null;
      }
      String imgBathPath = "未上传";
      Dept dept = coreDeptService.getDeptById(deptId);
      if (StringUtils.isNotEmpty(dept.getImgName())) {
         File imgFile = new File(dept.getImgName());
         if (imgFile.exists()) {
            imgBathPath = dept.getImgName();
         }
      }
      return imgBathPath;
   }
   /**
    * å‡ºå…¥åº“的附件上次,参数是车牌号
    *
    * @param file
    * @return
    * @throws IOException
    */
   public PageResponse<String> upLoadInoutHandleImg(MultipartFile file, String plateNum)
         throws IOException {
      log.debug("上传出入库的附件……");
      // èŽ·å–é»˜è®¤ç¼–ç çš„å­—èŠ‚æ•°ç»„
      byte[] fileByte = file.getBytes();
      // èŽ·å–æ–‡ä»¶çš„æºæ–‡ä»¶åç§°
      String oldFileName = file.getOriginalFilename();
      // èŽ·å–æ–‡ä»¶ä¿å­˜è·¯å¾„
      String filePath = fileService.getFileSavePath("INOUT");
      // èŽ·å–æ–°çš„ID
      String newFileName = ContextUtil.generateId();
      // æ–‡ä»¶åŽç¼€å
      String suffixName = oldFileName.substring(oldFileName.lastIndexOf("."));
      // åˆæˆæ–°çš„æ–‡ä»¶å
      if (StringUtils.isEmpty(plateNum)) {
         newFileName = newFileName + suffixName;
      } else {
         newFileName = plateNum + "_" + newFileName + suffixName;
      }
      // æ–‡ä»¶ä¸Šä¼ 
      boolean flag = uploadFile(fileByte, filePath, newFileName);
      // åˆ¤æ–­æ–‡ä»¶ä¸Šä¼ æ˜¯å¦æˆåŠŸ,成功返回文件名
      if (flag) {
         return new PageResponse<String>(RespCodeEnum.CODE_0000.getCode(),
               newFileName);
      } else {
         return new PageResponse<String>(RespCodeEnum.CODE_1111.getCode(),
               "上传失败");
      }
   }
   /**
    * æ–‡ä»¶æµä¸Šä¼ æ–‡ä»¶
    *
    * @param file
    * @param filePath
    * @param fileName
    * @return
    */
   private static boolean uploadFile(byte[] file, String filePath, String fileName) throws IOException {
      // é»˜è®¤æ–‡ä»¶ä¸Šä¼ æˆåŠŸ
      boolean flag = true;
      // new一个文件对象实例
      File targetFile = new File(filePath);
      // å¦‚果当前文件目录不存在就自动创建该文件或者目录
      if (!targetFile.exists()) {
         targetFile.mkdirs();
      }
      // åˆ›å»ºæ–‡ä»¶æµ
      FileOutputStream fileOutputStream = new FileOutputStream(filePath + fileName);
      try {
         fileOutputStream.write(file);
      } catch (FileNotFoundException e) {
         flag = false;
      } catch (IOException ioException) {
         flag = false;
      } finally {
         fileOutputStream.flush();
         fileOutputStream.close();
      }
      return flag;
   }
}