xiexiang пре 1 година
родитељ
комит
c15823eb7c

+ 25 - 14
src/main/java/com/example/fms/controller/FileMangerController.java

@@ -82,22 +82,33 @@ public class FileMangerController {
     @GetMapping("/downloadFile")
     @Operation(summary = "下载文件")
     public ResponseEntity<InputStreamResource> downloadFile(String fileId) {
-        //根据文件GUID获取文件信息
-        byte[] fileData = fileManagerService.downloadFile(fileId);
         LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(SystemFile::getGuid, fileId);
-        SystemFile systemFile = systemFileService.getOne(queryWrapper);
-        String fileName = systemFile.getOriginalName();
-        try {
-            //文件原始名中的中文字符可能会乱码,对文件名进行URL编码
-            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("文件名取出时发生错误!");
+        SystemFile systemFile = systemFileService.getOne(queryWrapper, false);
+
+        if (systemFile != null) {
+            //根据文件GUID获取文件信息
+            byte[] fileData = fileManagerService.downloadFile(fileId);
+            if (fileData.length > 0) {
+                String fileName = systemFile.getOriginalName();
+                try {
+                    //文件原始名中的中文字符可能会乱码,对文件名进行URL编码
+                    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("文件名取出时发生错误!");
+                }
+            } else {
+                // 文件字节数组为空
+                return ResponseEntity.noContent().build();
+            }
+        } else {
+            // 文件信息为空
+            return ResponseEntity.noContent().build();
         }
     }
 

+ 30 - 31
src/main/java/com/example/fms/service/FileMangerService.java

@@ -89,39 +89,38 @@ public class FileMangerService {
      * @throws Exception
      */
     public byte[] downloadFile(String fileId) {
-        if (fileId != null) {
-            //1.新建一个对象
-            DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
-            //2.根据传入文件id查询到文件信息
-            LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
-            queryWrapper.eq(SystemFile::getGuid, fileId);
-            SystemFile systemFileVO = systemFileService.getOne(queryWrapper);
-            //3.文件信息标记为已经删除的,返回null
-            if (systemFileVO.getIsDelete().equals(1)) {
-                return null;
-            } else {
-                //3.对象赋值给新的
-                BeanUtils.copyProperties(systemFileVO, 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;
+
+        //1.新建一个对象
+        DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
+        //2.根据传入文件id查询到文件信息
+        LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(SystemFile::getGuid, fileId);
+        SystemFile systemFile = systemFileService.getOne(queryWrapper, false);
+        //外层做过判断,一定可以查到
+        //3.文件信息标记为已经删除的,返回null
+        if (systemFile.getIsDelete().equals(1)) {
+            return new byte[0];
+        } else {
+            //3.对象赋值给新的
+            BeanUtils.copyProperties(systemFile, 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(systemFile.getSourceId());
+            //8.调用工厂类下载方法
+            String sourceName = configSettingVO.getSourceName();
+            //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
+            IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
+            byte[] fileData = fileFactoryObject.downloadFile(downloadSysFileDTO, configSettingVO);
+            return fileData;
         }
-        return null;
     }
 
     /**