Jelajahi Sumber

文件删除

Zhaops 6 tahun lalu
induk
melakukan
1d565de449

+ 2 - 1
icssman-service/src/main/java/com/diagbot/exception/ServiceErrorCode.java

@@ -8,7 +8,8 @@ package com.diagbot.exception;
  * @time: 2018/9/10 11:11
  */
 public enum ServiceErrorCode implements ErrorCode {
-    FILE_UPLOAD_ERROE("12020001", "文件上传出错");
+    FILE_UPLOAD_ERROE("12020001", "文件上传出错"),
+    FILE_DELETE_ERROR("12020002", "远程删除文件出错");
 
     private String code;
     private String msg;

+ 21 - 0
icssman-service/src/main/java/com/diagbot/service/impl/UploadServiceImpl.java

@@ -68,4 +68,25 @@ public class UploadServiceImpl implements UploadService {
         return path;
 
     }
+
+    /**
+     * 删除服务端文件
+     *
+     * @param path
+     * @return
+     */
+    public Boolean deleteRemoteFile(String path) {
+        if (path.startsWith("/")) {
+            path = path.substring(1);
+        }
+        String fileName = path.substring(path.indexOf("/") + 1);
+        String groupName = path.substring(0, path.indexOf("/"));
+        try {
+            FastDFSClient.deleteFile(groupName, fileName);
+        } catch (Exception e) {
+            log.error("", e);
+            throw new CommonException(ServiceErrorCode.FILE_DELETE_ERROR, "delete file failed");
+        }
+        return true;
+    }
 }

+ 5 - 0
icssman-service/src/main/java/com/diagbot/web/UploadController.java

@@ -22,4 +22,9 @@ public class UploadController {
     public RespDTO<String> singleFileUpload(@RequestParam("file") MultipartFile file) {
         return RespDTO.onSuc(uploadFacade.singleFileUpload(file));
     }
+
+    @PostMapping("/deleteRemoteFile")
+    public RespDTO<Boolean> deleteRemoteFile(@RequestParam("path") String path) {
+        return RespDTO.onSuc(uploadFacade.deleteRemoteFile(path));
+    }
 }