FileFactoryService.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.example.fms.service;
  2. import com.example.fms.common.model.dto.DownloadSysFileDTO;
  3. import com.example.fms.common.model.dto.SystemFileDTO;
  4. import com.example.fms.common.model.vo.ConfigSettingVO;
  5. import com.example.fms.common.utils.ExcuteConfigUtils;
  6. import com.example.fms.common.utils.FileUtils;
  7. import com.example.fms.exception.XiaoShiException;
  8. import lombok.RequiredArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import java.util.*;
  13. /**
  14. * 文件工厂类
  15. *
  16. * @Author xiexiang
  17. * @Date 2023/6/2
  18. */
  19. @Slf4j
  20. @Service
  21. @RequiredArgsConstructor
  22. public class FileFactoryService {
  23. private final FileUtils fileUtils;
  24. /**
  25. * 上传文件
  26. *
  27. * @param files
  28. * @param sourceId
  29. * @return
  30. */
  31. public List<SystemFileDTO> uploadFiles(List<MultipartFile> files, Integer sourceId) {
  32. if (files != null && files.size() != 0) {
  33. //调用解析配置方法,获取配置信息
  34. List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
  35. ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null);
  36. if (configSettingVO == null) {
  37. return null;
  38. }
  39. String sourceName = configSettingVO.getSourceName();
  40. if (sourceName.equals("FSS")) {
  41. return this.uploadToFSS(files, configSettingVO);
  42. }
  43. return null;
  44. }
  45. throw new XiaoShiException("传入文件为空");
  46. }
  47. /**
  48. * 上传到服务器
  49. *
  50. * @param files
  51. * @param configSettingVO
  52. * @return
  53. */
  54. public List<SystemFileDTO> uploadToFSS(List<MultipartFile> files, ConfigSettingVO configSettingVO) {
  55. List<SystemFileDTO> systemFileDTOS = new ArrayList<>();
  56. for (MultipartFile file : files) {
  57. try {
  58. String directoryName = fileUtils.getDirectoryName();
  59. SystemFileDTO systemFileDTO = SftpService.upload(configSettingVO.getFilePath() + directoryName, file, configSettingVO);
  60. systemFileDTO.setPType(configSettingVO.getSourceId());
  61. systemFileDTO.setOriginalName(file.getOriginalFilename());
  62. systemFileDTO.setFilePath(configSettingVO.getFilePath() + directoryName + "/" + systemFileDTO.getFileName());
  63. systemFileDTOS.add(systemFileDTO);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. return systemFileDTOS;
  69. }
  70. /**
  71. * 下载文件
  72. *
  73. * @param downloadSysFileDTO
  74. * @throws Exception
  75. */
  76. public byte[] download(DownloadSysFileDTO downloadSysFileDTO) throws Exception {
  77. Integer sourceId = downloadSysFileDTO.getSourceId();
  78. //调用解析配置方法,获取配置信息
  79. List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
  80. //根据传入sourceId判断从哪里下载数据
  81. ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null);
  82. if (configSettingVO == null) {
  83. throw new XiaoShiException("解析错误");
  84. }
  85. String sourceName = configSettingVO.getSourceName();
  86. if (sourceName.equals("FSS")) {
  87. byte[] file = this.downloadFromFSS(downloadSysFileDTO, configSettingVO);
  88. return file;
  89. } else {
  90. return null;
  91. }
  92. }
  93. /**
  94. * @param //directory 下载目录 根据SFTP设置的根目录来进行传输
  95. * @param //downloadFile 下载的文件
  96. * @param //saveFile 存在本地的路径
  97. */
  98. public byte[] downloadFromFSS(DownloadSysFileDTO downloadSysFileDTO, ConfigSettingVO configSettingVO) throws Exception {
  99. //下载目录,也是存储路径
  100. String filePath = downloadSysFileDTO.getFilePath();
  101. String directory = filePath.substring(0, filePath.lastIndexOf("\\"));
  102. //下载的文件,也就是文件名
  103. String downloadFile = downloadSysFileDTO.getFileName();
  104. //存在本地的路径,随机生成
  105. // String saveFile = "C:\\Users\\Admin\\Desktop\\downloadFile";
  106. // byte[] fileData = SftpService.download(directory, downloadFile, saveFile, configSettingVO);
  107. byte[] fileData = SftpService.download(directory, downloadFile, configSettingVO);
  108. return fileData;
  109. }
  110. /**
  111. * 删除文件
  112. *
  113. * @param filePath
  114. * @param pType
  115. */
  116. public static void delete(String filePath, Integer pType) {
  117. //调用解析配置方法,获取配置信息
  118. List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
  119. //根据传入类型判断从哪里删除数据
  120. //如果为1,则为服务器
  121. if (pType.equals(1)) {
  122. int sourceId = 1;
  123. ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null);
  124. //拆分路径字符串
  125. int index = filePath.lastIndexOf('/');
  126. //要删除文件所在目录
  127. String directory = filePath.substring(0, index);
  128. //要删除的文件
  129. String deleteFile = filePath.substring(index + 1, filePath.length());
  130. try {
  131. SftpService.delete(directory, deleteFile, configSettingVO);
  132. } catch (Exception e) {
  133. throw new XiaoShiException("删除错误");
  134. }
  135. } else if (pType.equals(2)) {//类型为2,则为阿里云OSS
  136. int sourceId = 2;
  137. ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getSourceId().equals(sourceId)).findFirst().orElse(null);
  138. }
  139. }
  140. }