Browse Source

图片上传返回参数修改

Zhaops 6 years ago
parent
commit
cce01e1796

+ 9 - 0
icssman-service/src/main/java/com/diagbot/dto/FileDTO.java

@@ -15,4 +15,13 @@ public class FileDTO {
     private String original;
     private String title;
     private String url;
+    private String info;
+
+    public FileDTO(String state, String info) {
+        this.state = state;
+        this.info = info;
+    }
+    public FileDTO(){
+
+    }
 }

+ 12 - 17
icssman-service/src/main/java/com/diagbot/service/impl/UploadServiceImpl.java

@@ -3,8 +3,6 @@ package com.diagbot.service.impl;
 import com.diagbot.client.fastdfs.FastDFSClient;
 import com.diagbot.client.fastdfs.FastDFSFile;
 import com.diagbot.dto.FileDTO;
-import com.diagbot.exception.CommonException;
-import com.diagbot.exception.ServiceErrorCode;
 import com.diagbot.service.UploadService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
@@ -23,22 +21,20 @@ import java.io.InputStream;
 public class UploadServiceImpl implements UploadService {
     @Override
     public FileDTO singleFileUpload(MultipartFile file) {
-        FileDTO fileDTO = new FileDTO();
-
         if (file.isEmpty()) {
-            throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件不能为空");
+            return new FileDTO("FAILURE", "文件不能为空");
         }
         //文件大小上限1M
         if (file.getSize() > 1024 * 1024) {
-            throw new CommonException(ServiceErrorCode.FILE_MAX_SIZE_LIMIT, "文件上传失败,超出大小限制1MB");
+            return new FileDTO("FAILURE", "文件上传失败,超出大小限制1MB");
         }
         try {
-            fileDTO = saveFile(file);
+            FileDTO fileDTO = saveFile(file);
+            return fileDTO;
         } catch (Exception e) {
             log.error("文件上传失败", e);
-            throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传失败,请重新上传");
+            return new FileDTO("FAILURE", "文件上传失败,请重新上传");
         }
-        return fileDTO;
     }
 
 
@@ -48,7 +44,7 @@ public class UploadServiceImpl implements UploadService {
      * @throws IOException
      */
     public FileDTO saveFile(MultipartFile multipartFile) throws IOException {
-        FileDTO fileDTO = new FileDTO();
+
         String[] fileAbsolutePath = {};
         String fileName = multipartFile.getOriginalFilename();
         String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
@@ -65,19 +61,18 @@ public class UploadServiceImpl implements UploadService {
             fileAbsolutePath = FastDFSClient.upload(file);  //upload to fastdfs
         } catch (Exception e) {
             log.error("文件上传异常", e);
-            throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传异常");
+            return new FileDTO("FAILURE", "文件上传异常");
         }
         if (fileAbsolutePath == null) {
             log.error("文件上传失败,请重新上传");
-            throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传失败,请重新上传");
+            return new FileDTO("FAILURE", "文件上传失败,请重新上传");
         }
         String path = "/" + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
-        fileDTO.setState("SUCCESS");
+        FileDTO fileDTO = new FileDTO("SUCCESS", "文件上传成功");
         fileDTO.setUrl(path);
         fileDTO.setOriginal(multipartFile.getOriginalFilename());
         fileDTO.setTitle(multipartFile.getOriginalFilename());
         return fileDTO;
-
     }
 
     /**
@@ -86,7 +81,7 @@ public class UploadServiceImpl implements UploadService {
      * @param path
      * @return
      */
-    public Boolean deleteRemoteFile(String path) {
+    public FileDTO deleteRemoteFile(String path) {
         if (path.startsWith("/")) {
             path = path.substring(1);
         }
@@ -96,8 +91,8 @@ public class UploadServiceImpl implements UploadService {
             FastDFSClient.deleteFile(groupName, fileName);
         } catch (Exception e) {
             log.error("", e);
-            throw new CommonException(ServiceErrorCode.FILE_DELETE_ERROR, "文件删除失败");
+            return new FileDTO("FAILURE", "文件删除失败");
         }
-        return true;
+        return new FileDTO("SUCCESS", "文件删除成功");
     }
 }

+ 7 - 6
icssman-service/src/main/java/com/diagbot/web/UploadController.java

@@ -1,8 +1,7 @@
 package com.diagbot.web;
 
-import com.diagbot.dto.FileDTO;
-import com.diagbot.dto.RespDTO;
 import com.diagbot.facade.UploadFacade;
+import com.diagbot.util.FastJsonUtils;
 import io.swagger.annotations.Api;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.CrossOrigin;
@@ -23,12 +22,14 @@ public class UploadController {
 
     @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = { RequestMethod.POST }, origins = "*")
     @PostMapping(value = "/uploadImage", produces = "application/json;charset=UTF-8")
-    public RespDTO<FileDTO> singleFileUpload(@RequestParam("upfile") MultipartFile file) {
-        return RespDTO.onSuc(uploadFacade.singleFileUpload(file));
+    public String singleFileUpload(@RequestParam("upfile") MultipartFile file) {
+        String data = FastJsonUtils.getBeanToJson(uploadFacade.singleFileUpload(file));
+        return data;
     }
 
     @PostMapping("/deleteRemoteFile")
-    public RespDTO<Boolean> deleteRemoteFile(@RequestParam("path") String path) {
-        return RespDTO.onSuc(uploadFacade.deleteRemoteFile(path));
+    public String deleteRemoteFile(@RequestParam("path") String path) {
+        String data = FastJsonUtils.getBeanToJson(uploadFacade.deleteRemoteFile(path));
+        return data;
     }
 }