Procházet zdrojové kódy

2023/10/23 xiexiang

xiexiang před 1 rokem
rodič
revize
1029ff13ac

+ 7 - 0
src/main/java/com/example/fms/common/model/dto/DownloadSysFileDTO.java

@@ -16,7 +16,11 @@ public class DownloadSysFileDTO {
      */
     private String filePath;
 
+    /**
+     * 服务器存储目录位置(1.本地project/pas/prod/file 2.本地project/rms/prod/file 3.生产project/pas/prod/file 4.生产project/rms/prod/file)
+     */
     private Integer pType;
+
     /**
      * 文件名称
      */
@@ -27,6 +31,9 @@ public class DownloadSysFileDTO {
      */
     private String originalName;
 
+    /**
+     * 判断存储在哪个服务器
+     */
     private Integer sourceId;
 
 }

+ 1 - 1
src/main/java/com/example/fms/common/model/dto/FMSDeleteFileDTO.java

@@ -17,7 +17,7 @@ public class FMSDeleteFileDTO {
     private List<Integer> ids;
 
     /**
-     * 删除类型
+     * 删除类型()
      */
     private Integer type;
 }

+ 7 - 1
src/main/java/com/example/fms/common/model/vo/SystemFileVO.java

@@ -25,10 +25,16 @@ public class SystemFileVO {
     private String GUID;
 
     /**
-     * 存储位置
+     * 服务器存储目录位置(1.本地project/pas/prod/file 2.本地project/rms/prod/file 3.生产project/pas/prod/file 4.生产project/rms/prod/file)
+     * 等于configSettingVO里面的id
+     * 判断存储在服务器的哪个路径下
      */
     private Integer pType;
 
+    /**
+     * 配置类型id
+     * 判断存储在哪个服务器下
+     */
     private Integer sourceId;
 
     /**

+ 46 - 23
src/main/java/com/example/fms/controller/FileMangerController.java

@@ -4,15 +4,15 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.example.fms.common.model.dto.FMSDeleteFileDTO;
 import com.example.fms.common.utils.Response;
 import com.example.fms.domain.SystemFile;
+import com.example.fms.exception.XiaoShiException;
 import com.example.fms.service.FileMangerService;
-import com.example.fms.service.ReadPDF2Service;
-import com.example.fms.service.ReadPDFService;
 import com.example.fms.service.SystemFileService;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.core.io.InputStreamResource;
+import org.springframework.http.ContentDisposition;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
@@ -21,6 +21,10 @@ import org.springframework.web.multipart.MultipartFile;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
 import java.util.List;
 
 /**
@@ -35,8 +39,6 @@ import java.util.List;
 public class FileMangerController {
     private final FileMangerService fileManagerService;
     private final SystemFileService systemFileService;
-    private final ReadPDFService readPDFService;
-    private final ReadPDF2Service readPDF2Service;
 
 //    @PostMapping("/uploadSystemFile")
 //    @Operation(summary = "上传文件")
@@ -62,16 +64,36 @@ public class FileMangerController {
 
     }
 
-    @GetMapping("/downloadSystemFile")
-    @Operation(summary = "取出文件")
-    public ResponseEntity<InputStreamResource> download(Integer fileId) throws Exception {
-        byte[] fileData = fileManagerService.download(fileId);
+//    @GetMapping("/downloadSystemFile")
+//    @Operation(summary = "取出文件")
+//    public ResponseEntity<InputStreamResource> download(Integer fileId) throws Exception {
+//        byte[] fileData = fileManagerService.download(fileId);
+//        SystemFile systemFile = systemFileService.getById(fileId);
+//        String fileName = systemFile.getFileName();
+//        return ResponseEntity.ok().contentLength(fileData.length)
+//                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
+//                .contentType(MediaType.parseMediaType("application/octet-stream"))
+//                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
+//    }
+
+    @GetMapping("/downloadFile")
+    @Operation(summary = "下载文件")
+    public ResponseEntity<InputStreamResource> downloadFile(Integer fileId) {
+        byte[] fileData = fileManagerService.downloadFile(fileId);
         SystemFile systemFile = systemFileService.getById(fileId);
-        String fileName = systemFile.getFileName();
-        return ResponseEntity.ok().contentLength(fileData.length)
-                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
-                .contentType(MediaType.parseMediaType("application/octet-stream"))
-                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
+        String fileName = systemFile.getOriginalName();
+        try {
+            String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
+            return ResponseEntity.ok()
+                    .contentLength(fileData.length)
+                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedFileName + "\"")
+                    .contentType(MediaType.parseMediaType("application/octet-stream"))
+                    .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
+        } catch (UnsupportedEncodingException e){
+            throw new XiaoShiException("文件名取出时发生错误!");
+        }
+
+
     }
 
     @PostMapping("/getFileData")
@@ -83,17 +105,18 @@ public class FileMangerController {
         return systemFiles;
     }
 
-    @PostMapping("/deleteSystemFile")
+//    @PostMapping("/deleteSystemFile")
+//    @Operation(summary = "删除文件")
+//    public String delete(@RequestBody FMSDeleteFileDTO fMSDeleteFileDTO) {
+//        fileManagerService.delete(fMSDeleteFileDTO.getIds(), fMSDeleteFileDTO.getType());
+//        return Response.success("删除系统文件成功");
+//    }
+
+    @PostMapping("/deleteFile")
     @Operation(summary = "删除文件")
-    public String delete(@RequestBody FMSDeleteFileDTO fMSDeleteFileDTO) {
-        fileManagerService.delete(fMSDeleteFileDTO.getIds(), fMSDeleteFileDTO.getType());
-        return Response.success("删除系统文件成功");
+    public String deleteFile(@RequestBody FMSDeleteFileDTO fMSDeleteFileDTO) {
+        String mes = fileManagerService.deleteFile(fMSDeleteFileDTO.getIds(), fMSDeleteFileDTO.getType());
+        return Response.success(mes);
     }
 
-    @GetMapping("/PDF")
-    @Operation(summary = "pdf")
-    public String pdf(String fileName) {
-        readPDF2Service.read(fileName);
-        return Response.success("导出图片");
-    }
 }

+ 98 - 0
src/main/java/com/example/fms/service/File2SftpService.java

@@ -0,0 +1,98 @@
+package com.example.fms.service;
+
+import com.example.fms.common.model.dto.DownloadSysFileDTO;
+import com.example.fms.common.model.dto.SystemFileDTO;
+import com.example.fms.common.model.vo.ConfigSettingVO;
+import com.example.fms.common.utils.FileUtils;
+import com.example.fms.exception.XiaoShiException;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 文件管理接口
+ * @Author xiexiang
+ * @Date 2023/10/20
+ */
+@Slf4j
+@Service
+@RequiredArgsConstructor
+public class File2SftpService implements IFileFactory {
+    private final FileUtils fileUtils;
+
+    /**
+     * 上传文件
+     * @param files
+     * @param configSettingVO 根据传入的id,选择的配置类,选择的上传路径以及上传方法
+     * @return
+     * @throws IOException
+     */
+    public List<SystemFileDTO> uploadFile(List<MultipartFile> files, ConfigSettingVO configSettingVO) {
+        List<SystemFileDTO> systemFileDTOS = new ArrayList<>();
+        for (MultipartFile file : files) {
+            try {
+                String directoryName = fileUtils.getDirectoryName();
+                SystemFileDTO systemFileDTO = SftpService.upload(configSettingVO.getFilePath() + directoryName, file, configSettingVO);
+                //服务器存储目录位置(1.本地project/pas/prod/file 2.本地project/rms/prod/file 3.生产project/pas/prod/file 4.生产project/rms/prod/file)
+                systemFileDTO.setPType(configSettingVO.getId());
+                //FSS
+                systemFileDTO.setSourceId(configSettingVO.getSourceId());
+                systemFileDTO.setOriginalName(file.getOriginalFilename());
+                systemFileDTO.setFilePath(configSettingVO.getFilePath() + directoryName + "/" + systemFileDTO.getFileName());
+                systemFileDTOS.add(systemFileDTO);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        return systemFileDTOS;
+    }
+
+    /**
+     * 下载文件
+     * @param downloadSysFileDTO
+     * @param configSettingVO 根据传入的id,选择的配置类,选择下载路径以及下载方法
+     * @return
+     * @throws Exception
+     */
+    public byte[] downloadFile(DownloadSysFileDTO downloadSysFileDTO, ConfigSettingVO configSettingVO)  {
+        //下载目录,也是存储路径
+        String filePath = downloadSysFileDTO.getFilePath();
+        int index = filePath.lastIndexOf("/");
+        String directory = filePath.substring(0, index);
+        //下载的文件,也就是文件名
+        String downloadFile = downloadSysFileDTO.getFileName();
+        //存在本地的路径,随机生成
+//        String saveFile = "C:\\Users\\Admin\\Desktop\\downloadFile";
+//        byte[] fileData = SftpService.download(directory, downloadFile, saveFile, configSettingVO);
+        try {
+            byte[] fileData = SftpService.download(directory, downloadFile, configSettingVO);
+            return fileData;
+        } catch (Exception e) {
+            throw new XiaoShiException("下载错误");
+        }
+    }
+
+    /**
+     * 删除文件
+     * @param filePath
+     * @param configSettingVO
+     */
+    public void deleteFile(String filePath, ConfigSettingVO configSettingVO) {
+        //拆分路径字符串
+        int index = filePath.lastIndexOf('/');
+        //要删除文件所在目录
+        String directory = filePath.substring(0, index);
+        //要删除的文件
+        String deleteFile = filePath.substring(index + 1, filePath.length());
+        try {
+            SftpService.delete(directory, deleteFile, configSettingVO);
+        } catch (Exception e) {
+            throw new XiaoShiException("删除错误");
+        }
+    }
+}

+ 11 - 5
src/main/java/com/example/fms/service/FileFactory.java

@@ -13,11 +13,17 @@ import org.springframework.stereotype.Service;
 @Service
 @RequiredArgsConstructor
 public class FileFactory {
-    public static IFileFactory createObject(Integer sourceId){
-        IFileFactory factoryObject = null;
-        switch(sourceId){
-            case 1:
-                return factoryObject;
+    private final File2SftpService file2SftpService;
+
+    /**
+     * 判断
+     * @param sourceName
+     * @return
+     */
+    public IFileFactory createObject(String sourceName){
+        switch(sourceName){
+            case "FSS":
+                return file2SftpService;
             default:
                 return null;
         }

+ 0 - 153
src/main/java/com/example/fms/service/FileFactoryService.java

@@ -1,153 +0,0 @@
-package com.example.fms.service;
-
-
-import com.example.fms.common.model.dto.DownloadSysFileDTO;
-import com.example.fms.common.model.dto.SystemFileDTO;
-import com.example.fms.common.model.vo.ConfigSettingVO;
-import com.example.fms.common.utils.ExcuteConfigUtils;
-import com.example.fms.common.utils.FileUtils;
-import com.example.fms.exception.XiaoShiException;
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.util.*;
-
-/**
- * 文件工厂类
- *
- * @Author xiexiang
- * @Date 2023/6/2
- */
-@Slf4j
-@Service
-@RequiredArgsConstructor
-public class FileFactoryService {
-    private final FileUtils fileUtils;
-
-    /**
-     * 上传文件
-     *
-     * @param files
-     * @param sourceId
-     * @return
-     */
-    public List<SystemFileDTO> uploadFiles(List<MultipartFile> files, Integer sourceId) {
-        if (files != null && files.size() != 0) {
-            //调用解析配置方法,获取配置信息
-            List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
-            //根据配置id获得配置
-            ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(sourceId)).findFirst().orElse(null);
-            if (configSettingVO == null) {
-                return null;
-            }
-            String sourceName = configSettingVO.getSourceName();
-            if (sourceName.equals("FSS")) {
-                return this.uploadToFSS(files, configSettingVO);
-            }
-            return null;
-        }
-        throw new XiaoShiException("传入文件为空");
-    }
-
-
-    /**
-     * 上传到服务器
-     *
-     * @param files
-     * @param configSettingVO
-     * @return
-     */
-    public List<SystemFileDTO> uploadToFSS(List<MultipartFile> files, ConfigSettingVO configSettingVO) {
-        List<SystemFileDTO> systemFileDTOS = new ArrayList<>();
-        for (MultipartFile file : files) {
-            try {
-                String directoryName = fileUtils.getDirectoryName();
-                SystemFileDTO systemFileDTO = SftpService.upload(configSettingVO.getFilePath() + directoryName, file, configSettingVO);
-                systemFileDTO.setPType(configSettingVO.getId());
-                systemFileDTO.setSourceId(configSettingVO.getSourceId());
-                systemFileDTO.setOriginalName(file.getOriginalFilename());
-                systemFileDTO.setFilePath(configSettingVO.getFilePath() + directoryName + "/" + systemFileDTO.getFileName());
-                systemFileDTOS.add(systemFileDTO);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-        return systemFileDTOS;
-    }
-
-
-    /**
-     * 下载文件
-     *
-     * @param downloadSysFileDTO
-     * @throws Exception
-     */
-    public byte[] download(DownloadSysFileDTO downloadSysFileDTO) throws Exception {
-        Integer id = downloadSysFileDTO.getPType();
-        //调用解析配置方法,获取配置信息
-        List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
-        //根据传入sourceId判断从哪里下载数据
-        ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(id)).findFirst().orElse(null);
-        if (configSettingVO == null) {
-            throw new XiaoShiException("解析错误");
-        }
-        String sourceName = configSettingVO.getSourceName();
-        if (sourceName.equals("FSS")) {
-            byte[] file = this.downloadFromFSS(downloadSysFileDTO, configSettingVO);
-            return file;
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     * @param //directory    下载目录 根据SFTP设置的根目录来进行传输
-     * @param //downloadFile 下载的文件
-     * @param //saveFile     存在本地的路径
-     */
-    public byte[] downloadFromFSS(DownloadSysFileDTO downloadSysFileDTO, ConfigSettingVO configSettingVO) throws Exception {
-        //下载目录,也是存储路径
-        String filePath = downloadSysFileDTO.getFilePath();
-        int index = filePath.lastIndexOf("/");
-        String directory = filePath.substring(0, index);
-        //下载的文件,也就是文件名
-        String downloadFile = downloadSysFileDTO.getFileName();
-        //存在本地的路径,随机生成
-//        String saveFile = "C:\\Users\\Admin\\Desktop\\downloadFile";
-//        byte[] fileData = SftpService.download(directory, downloadFile, saveFile, configSettingVO);
-        byte[] fileData = SftpService.download(directory, downloadFile, configSettingVO);
-        return fileData;
-    }
-
-    /**
-     * 删除文件
-     *
-     * @param filePath
-     * @param pType
-     */
-    public static void delete(String filePath, Integer pType) {
-        //调用解析配置方法,获取配置信息
-        List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
-        ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(pType)).findFirst().orElse(null);
-        //根据传入类型判断从哪里删除数据
-        //如果为1,则为服务器
-        //if(sourceId.equals(1)){
-        //拆分路径字符串
-        int index = filePath.lastIndexOf('/');
-        //要删除文件所在目录
-        String directory = filePath.substring(0, index);
-        //要删除的文件
-        String deleteFile = filePath.substring(index + 1, filePath.length());
-        try {
-            SftpService.delete(directory, deleteFile, configSettingVO);
-        } catch (Exception e) {
-            throw new XiaoShiException("删除错误");
-        }
-        //} else if(sourceId.equals(2)){//类型为2,则为阿里云OSS
-        //}
-    }
-
-
-}

+ 72 - 55
src/main/java/com/example/fms/service/FileMangerService.java

@@ -8,7 +8,6 @@ import com.example.fms.common.utils.ExcuteConfigUtils;
 import com.example.fms.domain.SystemFile;
 import com.example.fms.exception.XiaoShiException;
 import com.example.fms.mapper.SystemFileMapper;
-import io.swagger.v3.oas.models.security.SecurityScheme;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
@@ -20,6 +19,7 @@ import java.io.IOException;
 import java.util.List;
 
 /**
+ * 文件管理外部调用接口Service层
  * @Author xiexiang
  * @Date 2023/8/9
  */
@@ -27,7 +27,7 @@ import java.util.List;
 @Slf4j
 @Service
 public class FileMangerService {
-    private final FileFactoryService fileFactoryService;
+    private final OldFileFactoryService oldFileFactoryService;
     private final SystemFileService systemFileService;
     private final SystemFileMapper systemFileMapper;
     private final FileFactory fileFactory;
@@ -35,19 +35,21 @@ public class FileMangerService {
 
     /**
      * 上传文件的最外部接口
-     * @param files
-     * @param sourceId
-     * @return
+     * @param files 需要上传的文件
+     * @param sourceId configSettingVO的主键id,判断存储在哪个服务器路径之下
+     * @return insertIds 文件在数据库中的id集合
      * @throws IOException
      */
     public List<Integer> uploadFile(List<MultipartFile> files, Integer sourceId) throws IOException {
         if (files != null && files.size() != 0) {
             //1.调用解析配置方法,获取配置信息
             List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
-            //2.根据配置id获得配置
-            ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(sourceId)).findFirst().orElse(null);
+            //2.根据传入id,获得配置类,根据配置类选择使用的上传方法
+           ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(sourceId)).findFirst().orElse(null);
             //3.获取文件上传到服务器,调用工厂类
-            IFileFactory fileFactoryObject = fileFactory.createObject(sourceId);
+            String sourceName = configSettingVO.getSourceName();
+            //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
+            IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
             List<SystemFileDTO> systemFileDTOS = fileFactoryObject.uploadFile(files, configSettingVO);
             //4.根据上传后的返回实体类信息,将实体类信息入库,调用systemFileService add
             if (systemFileDTOS != null && systemFileDTOS.size() != 0) {
@@ -61,40 +63,42 @@ public class FileMangerService {
         }
     }
 
+
     /**
-     * 上传文件
-     *
-     * @param files
-     * @param sourceId
+     * 下载文件
+     * @param fileId 文件id
+     * @return fileData 文件流
+     * @throws Exception
      */
-    public List<Integer> add(List<MultipartFile> files, Integer sourceId) {
-        if (files != null && files.size() != 0) {
-            //1.获取文件上传到服务器,调用FileFactory的uploadFile
-
-            List<SystemFileDTO> systemFileDTOS = fileFactoryService.uploadFiles(files, sourceId);
-            //2.根据上传后的返回实体类信息,将实体类信息入库,调用systemFileService add
-            if (systemFileDTOS != null && systemFileDTOS.size() != 0) {
-                List<Integer> insertIds = systemFileService.add(systemFileDTOS, sourceId);
-                return insertIds;
-            } else {
-                throw new XiaoShiException("入表信息为空");
-            }
-        } else {
-            throw new XiaoShiException("上传文件为空");
-        }
-    }
-
-    //取系统文件
-    public byte[] download(Integer fileId) throws Exception {
-        DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
+    public byte[] downloadFile(Integer fileId) {
         if (fileId != null) {
+            //1.新建一个对象
+            DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
+            //2.根据传入文件id查询到文件信息
             SystemFileVO systemFileVO = systemFileService.query(fileId);
+            //3.文件信息标记为已经删除的,返回null
             if (systemFileVO.getIsDelete().equals(1)) {
                 return null;
             } else {
+                //3.对象赋值给新的
                 BeanUtils.copyProperties(systemFileVO, downloadSysFileDTO);
-                downloadSysFileDTO.setSourceId(systemFileVO.getPType());
-                return fileFactoryService.download(downloadSysFileDTO);
+                //4.设置pType(存储在哪条路径下)
+                Integer id = downloadSysFileDTO.getPType();
+                //5.调用解析配置方法,获取配置信息
+                List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
+                if(configSettingVOS == null && configSettingVOS.size() == 0){
+                    throw new XiaoShiException("缺失配置文件");
+                }
+                //6.根据id获取需要的配置类
+                ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(id)).findFirst().orElse(null);
+                //7.设置sourceId为数据库中的sourceId字段
+                downloadSysFileDTO.setSourceId(systemFileVO.getSourceId());
+                //8.调用工厂类下载方法
+                String sourceName = configSettingVO.getSourceName();
+                //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
+                IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
+                byte[] fileData = fileFactoryObject.downloadFile(downloadSysFileDTO, configSettingVO);
+                return fileData;
             }
         }
         return null;
@@ -102,54 +106,67 @@ public class FileMangerService {
 
     /**
      * 删除系统文件
-     *
-     * @param ids
-     * @param type
+     * 分为物理删除和逻辑删除,逻辑删除只更新数据库中isDelete字段,物理删除需要调用工厂类方法
+     * @param ids 需要删除的id集合
+     * @param type 删除方式判断字段,1为逻辑删除,2为物理删除
      */
-    public void delete(List<Integer> ids, Integer type) {
-        //首先判断传入的需要删除的id集合不能为空
+    public String deleteFile(List<Integer> ids, Integer type) {
+        //1.首先判断传入的需要删除的id集合不能为空
         if (CollectionUtils.isEmpty(ids)) {
             throw new XiaoShiException("需要删除的id集合不能为空");
         }
+        //2.其次判断传入的删除类型不能为空
         if (type == null) {
             throw new XiaoShiException("删除类型不能为空");
         }
-        //有逻辑删除和物理删除两种,需要进行判定,传入类型为2时,物理删除;传入类型为1时,逻辑删除
-        //逻辑删除(更新isDelete字段为1)
+        //3.1 判断如果是逻辑删除(更新isDelete字段为1)
         if (type.equals(1)) {
-            //遍历传入的id集合,获取需要更新的对象,赋值给实体类
+            //3.1.1 遍历传入的id集合
             for (int i = 0; i < ids.size(); i++) {
+                //3.1.2 获取需要更新的对象
                 SystemFileVO systemFileVO = systemFileMapper.query(ids.get(i));
+                //3.1.3 将对象的是否删除字段置为1
                 systemFileVO.setIsDelete(1);
-                //将查询出来的vo赋值给实体类
+                //3.1.4 将查询出来的vo赋值给实体类
                 SystemFile systemFile = new SystemFile();
                 BeanUtils.copyProperties(systemFileVO, systemFile);
-                //更新
+                //3.1.5 数据入
                 int row = systemFileMapper.update(systemFile);
+                //3.1.6 判断是否成功
                 if (row != 1) {
                     String mes = "逻辑删除系统文件第" + i + "条失败";
-                    log.info("逻辑删除失败,{}", mes);
                     throw new XiaoShiException(mes);
                 }
             }
-        }
-        //物理删除(删除服务器文件,删除数据库中记录)
-        //sftp删除文件
-        if (type.equals(2)) {
+            String mes = "逻辑删除成功";
+            return mes;
+        } else if (type.equals(2)) { //3.2 物理删除(先删除服务器文件,删除数据库中记录)
+            //3.2.1 遍历传入的id集合
             for (int i = 0; i < ids.size(); i++) {
-                //根据id到表中查询该文件记录的数据
+                //3.2.2 根据id到表中查询该文件记录的数据
                 SystemFileVO systemFileVO = systemFileService.query(ids.get(i));
-                //pType判断是服务器还是阿里云OSS
+                //3.2.3 根据pType判断存储在服务器的哪个地址下
                 int pType = systemFileVO.getPType();
+                //3.2.4 文件路径
                 String filePath = systemFileVO.getFilePath();
-                fileFactoryService.delete(filePath, pType);
+                //3.2.5 调用解析配置方法,获取指定配置信息
+                List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
+                ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(pType)).findFirst().orElse(null);
+                //3.2.6 配置类的删除方法
+                String sourceName = configSettingVO.getSourceName();
+                //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
+                IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
+                fileFactoryObject.deleteFile(filePath, configSettingVO);
                 int row = systemFileMapper.deleteById(ids.get(i));
                 if (row != 1) {
-                    throw new XiaoShiException("删除异常");
-                } else {
-                    log.info("删除成功");
+                    throw new XiaoShiException("第" + i + "条删除异常");
                 }
             }
+            String mes = "物理删除成功";
+            return mes;
+        } else {
+            String mes = "删除类型错误";
+            return mes;
         }
     }
 

+ 16 - 1
src/main/java/com/example/fms/service/IFileFactory.java

@@ -1,5 +1,6 @@
 package com.example.fms.service;
 
+import com.example.fms.common.model.dto.DownloadSysFileDTO;
 import com.example.fms.common.model.dto.SystemFileDTO;
 import com.example.fms.common.model.vo.ConfigSettingVO;
 import org.springframework.web.multipart.MultipartFile;
@@ -14,8 +15,22 @@ import java.util.List;
  */
 public interface IFileFactory {
     /**
-     *
+     * 上传文件
      * @throws IOException
      */
     List<SystemFileDTO> uploadFile(List<MultipartFile> files, ConfigSettingVO configSettingVO) throws IOException;
+
+    /**
+     * 下载文件
+     * @param downloadSysFileDTO
+     * @param configSettingVO
+     */
+    byte[] downloadFile(DownloadSysFileDTO downloadSysFileDTO, ConfigSettingVO configSettingVO);
+
+    /**
+     * 删除文件
+     * @param filePath
+     * @param configSettingVO
+     */
+    void deleteFile(String filePath, ConfigSettingVO configSettingVO);
 }

+ 153 - 0
src/main/java/com/example/fms/service/OldFileFactoryService.java

@@ -0,0 +1,153 @@
+package com.example.fms.service;
+
+
+import com.example.fms.common.model.dto.DownloadSysFileDTO;
+import com.example.fms.common.model.dto.SystemFileDTO;
+import com.example.fms.common.model.vo.ConfigSettingVO;
+import com.example.fms.common.utils.ExcuteConfigUtils;
+import com.example.fms.common.utils.FileUtils;
+import com.example.fms.exception.XiaoShiException;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.*;
+
+/**
+ * 文件工厂类
+ *
+ * @Author xiexiang
+ * @Date 2023/6/2
+ */
+@Slf4j
+@Service
+@RequiredArgsConstructor
+public class OldFileFactoryService {
+    private final FileUtils fileUtils;
+
+//    /**
+//     * 上传文件
+//     *
+//     * @param files
+//     * @param sourceId
+//     * @return
+//     */
+//    public List<SystemFileDTO> uploadFiles(List<MultipartFile> files, Integer sourceId) {
+//        if (files != null && files.size() != 0) {
+//            //调用解析配置方法,获取配置信息
+//            List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
+//            //根据配置id获得配置
+//            ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(sourceId)).findFirst().orElse(null);
+//            if (configSettingVO == null) {
+//                return null;
+//            }
+//            String sourceName = configSettingVO.getSourceName();
+//            if (sourceName.equals("FSS")) {
+//                return this.uploadToFSS(files, configSettingVO);
+//            }
+//            return null;
+//        }
+//        throw new XiaoShiException("传入文件为空");
+//    }
+
+//
+//    /**
+//     * 上传到服务器
+//     *
+//     * @param files
+//     * @param configSettingVO
+//     * @return
+//     */
+//    public List<SystemFileDTO> uploadToFSS(List<MultipartFile> files, ConfigSettingVO configSettingVO) {
+//        List<SystemFileDTO> systemFileDTOS = new ArrayList<>();
+//        for (MultipartFile file : files) {
+//            try {
+//                String directoryName = fileUtils.getDirectoryName();
+//                SystemFileDTO systemFileDTO = SftpService.upload(configSettingVO.getFilePath() + directoryName, file, configSettingVO);
+//                systemFileDTO.setPType(configSettingVO.getId());
+//                systemFileDTO.setSourceId(configSettingVO.getSourceId());
+//                systemFileDTO.setOriginalName(file.getOriginalFilename());
+//                systemFileDTO.setFilePath(configSettingVO.getFilePath() + directoryName + "/" + systemFileDTO.getFileName());
+//                systemFileDTOS.add(systemFileDTO);
+//            } catch (Exception e) {
+//                e.printStackTrace();
+//            }
+//        }
+//        return systemFileDTOS;
+//    }
+
+
+//    /**
+//     * 下载文件
+//     *
+//     * @param downloadSysFileDTO
+//     * @throws Exception
+//     */
+//    public byte[] download(DownloadSysFileDTO downloadSysFileDTO) throws Exception {
+//        Integer id = downloadSysFileDTO.getPType();
+//        //调用解析配置方法,获取配置信息
+//        List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
+//        //根据传入sourceId判断从哪里下载数据
+//        ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(id)).findFirst().orElse(null);
+//        if (configSettingVO == null) {
+//            throw new XiaoShiException("解析错误");
+//        }
+//        String sourceName = configSettingVO.getSourceName();
+//        if (sourceName.equals("FSS")) {
+//            byte[] file = this.downloadFromFSS(downloadSysFileDTO, configSettingVO);
+//            return file;
+//        } else {
+//            return null;
+//        }
+//    }
+//
+//    /**
+//     * @param //directory    下载目录 根据SFTP设置的根目录来进行传输
+//     * @param //downloadFile 下载的文件
+//     * @param //saveFile     存在本地的路径
+//     */
+//    public byte[] downloadFromFSS(DownloadSysFileDTO downloadSysFileDTO, ConfigSettingVO configSettingVO) throws Exception {
+//        //下载目录,也是存储路径
+//        String filePath = downloadSysFileDTO.getFilePath();
+//        int index = filePath.lastIndexOf("/");
+//        String directory = filePath.substring(0, index);
+//        //下载的文件,也就是文件名
+//        String downloadFile = downloadSysFileDTO.getFileName();
+//        //存在本地的路径,随机生成
+////        String saveFile = "C:\\Users\\Admin\\Desktop\\downloadFile";
+////        byte[] fileData = SftpService.download(directory, downloadFile, saveFile, configSettingVO);
+//        byte[] fileData = SftpService.download(directory, downloadFile, configSettingVO);
+//        return fileData;
+//    }
+
+//    /**
+//     * 删除文件
+//     *
+//     * @param filePath
+//     * @param pType
+//     */
+//    public static void delete(String filePath, Integer pType) {
+//        //调用解析配置方法,获取配置信息
+//        List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
+//        ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(pType)).findFirst().orElse(null);
+//        //根据传入类型判断从哪里删除数据
+//        //如果为1,则为服务器
+//        //if(sourceId.equals(1)){
+//        //拆分路径字符串
+//        int index = filePath.lastIndexOf('/');
+//        //要删除文件所在目录
+//        String directory = filePath.substring(0, index);
+//        //要删除的文件
+//        String deleteFile = filePath.substring(index + 1, filePath.length());
+//        try {
+//            SftpService.delete(directory, deleteFile, configSettingVO);
+//        } catch (Exception e) {
+//            throw new XiaoShiException("删除错误");
+//        }
+//        //} else if(sourceId.equals(2)){//类型为2,则为阿里云OSS
+//        //}
+//    }
+
+
+}

+ 0 - 63
src/main/java/com/example/fms/service/ReadPDF2Service.java

@@ -1,63 +0,0 @@
-package com.example.fms.service;
-
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.pdfbox.cos.COSName;
-
-import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
-import org.springframework.stereotype.Service;
-import org.apache.pdfbox.pdmodel.*;
-
-
-import javax.imageio.ImageIO;
-import java.awt.image.BufferedImage;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-/**
- * @Author xiexiang
- * @Date 2023/9/6
- */
-@Slf4j
-@Service
-@RequiredArgsConstructor
-public class ReadPDF2Service {
-
-    public void read(String fileName) {
-        try (PDDocument document = PDDocument.load(new File(fileName))) {
-            PDPageTree pages = document.getPages();
-            int imageNumber = 1;
-
-            for (PDPage page : pages) {
-                PDResources resources = page.getResources();
-                Iterable<COSName> objectNames = resources.getXObjectNames();
-
-                for (COSName objectName : objectNames) {
-                    if (resources.isImageXObject(objectName)) {
-                        PDImageXObject image = (PDImageXObject) resources.getXObject(objectName);
-                        //处理图像,例如保存到本地文件
-                        String imageName = "C:/Users/Admin/Desktop/" + objectName.getName() + "." + image.getSuffix();
-                        //使用getImage()方法获取BufferedImage对象
-                        BufferedImage bufferedImage = image.getImage();
-                        bufferedImage.getGraphics().dispose();
-                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                        //将BufferedImage对象保存为字节数组
-                        ImageIO.write(bufferedImage, "png", baos);
-                        // 获取图像的字节数组
-                        byte[] imageData = baos.toByteArray();
-
-                        FileOutputStream outputStream = new FileOutputStream(imageName);
-                        outputStream.write(imageData);
-                        outputStream.close();
-                        System.out.println("Iamge saved: " + imageName);
-                        imageNumber++;
-                    }
-                }
-            }
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-    }
-}

+ 0 - 73
src/main/java/com/example/fms/service/ReadPDFService.java

@@ -1,73 +0,0 @@
-package com.example.fms.service;
-
-import com.itextpdf.text.exceptions.UnsupportedPdfException;
-import com.itextpdf.text.pdf.*;
-import com.itextpdf.text.pdf.PdfReader;
-
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Service;
-
-import java.util.Iterator;
-
-/**
- * @Author xiexiang
- * @Date 2023/9/5
- */
-@Slf4j
-@Service
-@RequiredArgsConstructor
-public class ReadPDFService {
-
-    public void extractImage(String filename) {
-        PdfReader reader = null;
-        try {
-            //读取pdf文件
-            reader = new PdfReader(filename);
-            //获得pdf文件的页数
-            int sumPage = reader.getNumberOfPages();
-            //读取pdf文件中的每一页
-            for (int i = 1; i <= sumPage; i++) {
-                //得到pdf每一页的字典对象
-                PdfDictionary dictionary = reader.getPageN(i);
-                //通过RESOURCES得到对应的字典对象
-                PdfDictionary res = (PdfDictionary) PdfReader.getPdfObject(dictionary.get(PdfName.RESOURCES));
-                //得到XOBJECT图片对象
-                PdfDictionary xobj = (PdfDictionary) PdfReader.getPdfObject(res.get(PdfName.XOBJECT));
-                if (xobj != null) {
-                    for (Iterator it = xobj.getKeys().iterator(); it.hasNext(); ) {
-                        PdfObject obj = xobj.get((PdfName) it.next());
-                        if (obj.isIndirect()) {
-                            PdfDictionary tg = (PdfDictionary) PdfReader.getPdfObject(obj);
-                            PdfName type = (PdfName) PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE));
-                            if (PdfName.IMAGE.equals(type)) {
-                                PdfObject object = reader.getPdfObject(obj);
-                                if (object.isStream()) {
-                                    PRStream prstream = (PRStream) object;
-                                    byte[] b;
-                                    try {
-                                        b = reader.getStreamBytes(prstream);
-                                    } catch (UnsupportedPdfException e) {
-                                        b = reader.getStreamBytesRaw(prstream);
-                                    }
-                                    FileOutputStream output = new FileOutputStream(String.format("C:/Users/Admin/Desktop/output%d.jpg", i));
-                                    output.write(b);
-                                    output.flush();
-                                    output.close();
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-    }
-
-
-}

+ 12 - 1
src/main/java/com/example/fms/service/SystemFileService.java

@@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.UUID;
 
 /**
  * @Author xiexiang
@@ -48,6 +49,8 @@ public class SystemFileService extends ServiceImpl<SystemFileMapper, SystemFile>
 //                PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
 //                //给实体类赋值登陆人id
 //                systemFile.setCreateId(personnelVO.getId());
+                String guid = this.giveFileGUID(systemFile.getOriginalName());
+                systemFile.setGUID(guid);
                 systemFile.setCreateId(1);
                 systemFile.setPType(sourceId);
                 //插入数据
@@ -101,5 +104,13 @@ public class SystemFileService extends ServiceImpl<SystemFileMapper, SystemFile>
         }
     }
 
-
+    /**
+     * 根据文件名生成guid
+     * @param fileName
+     * @return
+     */
+    public String giveFileGUID(String fileName){
+        String guid = UUID.randomUUID().toString().replaceAll("-","");
+        return guid;
+    }
 }

+ 0 - 43
src/main/java/com/example/fms/service/fileFactory1Service.java

@@ -1,43 +0,0 @@
-package com.example.fms.service;
-
-import com.example.fms.common.model.dto.SystemFileDTO;
-import com.example.fms.common.model.vo.ConfigSettingVO;
-import com.example.fms.common.utils.FileUtils;
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * @Author xiexiang
- * @Date 2023/10/20
- */
-@Slf4j
-@Service
-@RequiredArgsConstructor
-public class fileFactory1Service implements IFileFactory {
-    private final FileUtils fileUtils;
-
-    public List<SystemFileDTO> uploadFile(List<MultipartFile> files, ConfigSettingVO configSettingVO) throws IOException {
-        List<SystemFileDTO> systemFileDTOS = new ArrayList<>();
-        for (MultipartFile file : files) {
-            try {
-                String directoryName = fileUtils.getDirectoryName();
-                SystemFileDTO systemFileDTO = SftpService.upload(configSettingVO.getFilePath() + directoryName, file, configSettingVO);
-                systemFileDTO.setPType(configSettingVO.getId());
-                systemFileDTO.setSourceId(configSettingVO.getSourceId());
-                systemFileDTO.setOriginalName(file.getOriginalFilename());
-                systemFileDTO.setFilePath(configSettingVO.getFilePath() + directoryName + "/" + systemFileDTO.getFileName());
-                systemFileDTOS.add(systemFileDTO);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-        return systemFileDTOS;
-    }
-
-}