| ¶Ô±ÈÐÂÎļþ |
| | |
| | | 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; |
| | | } |
| | | } |