Browse Source

图片上传返回结构标准化

Zhaops 5 years ago
parent
commit
23104beccd

+ 15 - 12
knowledgeman-service/src/main/java/com/diagbot/web/UploadController.java

@@ -1,6 +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 com.diagbot.util.StringUtil;
@@ -18,6 +19,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.File;
 
 @RestController
 @Api(value = "文件上传API", tags = { "知识库标准化-文件上传API" })
@@ -30,22 +32,23 @@ public class UploadController {
     @ApiOperation(value = "知识库标准化-文件上传")
     @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = { RequestMethod.POST }, origins = "*")
     @PostMapping(value = "/uploadImage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
-    public String singleFileUpload(@RequestParam("upfile") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
-        FileDTO fileDTO = uploadFacade.singleFileUpload(file);
-        String data = FastJsonUtils.getBeanToJson(fileDTO);
-        response.setContentType("text/plain;charset=UTF-8");
-
-        String callback = request.getParameter("callback");//客户端请求参数
-        if (callback != null && StringUtil.isNotBlank(callback)) {
-            data = callback + "(" + data + ")";
+    public RespDTO<FileDTO> singleFileUpload(@RequestParam("upfile") MultipartFile file) {
+        FileDTO data = uploadFacade.singleFileUpload(file);
+        if (data.getState().equals("FAILURE")) {
+            return RespDTO.onError(data.getInfo());
+        } else {
+            return RespDTO.onSuc(data);
         }
-        return data;
     }
 
     @PostMapping("/deleteRemoteFile")
     @ApiOperation(value = "知识库标准化-文件删除")
-    public String deleteRemoteFile(@RequestParam("path") String path) {
-        String data = FastJsonUtils.getBeanToJson(uploadFacade.deleteRemoteFile(path));
-        return data;
+    public RespDTO<FileDTO> deleteRemoteFile(@RequestParam("path") String path) {
+        FileDTO data = uploadFacade.deleteRemoteFile(path);
+        if (data.getState().equals("FAILURE")) {
+            return RespDTO.onError(data.getInfo());
+        } else {
+            return RespDTO.onSuc(data);
+        }
     }
 }

+ 14 - 17
prec-service/src/main/java/com/diagbot/web/UploadController.java

@@ -1,9 +1,8 @@
 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 com.diagbot.util.StringUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -16,9 +15,6 @@ import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
 @RestController
 @Api(value = "文件上传API", tags = { "文件上传API" })
 @RequestMapping(value = "/file")
@@ -30,22 +26,23 @@ public class UploadController {
     @ApiOperation(value = "文件上传")
     @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = { RequestMethod.POST }, origins = "*")
     @PostMapping(value = "/uploadImage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
-    public String singleFileUpload(@RequestParam("upfile") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
-        FileDTO fileDTO = uploadFacade.singleFileUpload(file);
-        String data = FastJsonUtils.getBeanToJson(fileDTO);
-        response.setContentType("text/plain;charset=UTF-8");
-
-        String callback = request.getParameter("callback");//客户端请求参数
-        if (callback != null && StringUtil.isNotBlank(callback)) {
-            data = callback + "(" + data + ")";
+    public RespDTO<FileDTO> singleFileUpload(@RequestParam("upfile") MultipartFile file) {
+        FileDTO data = uploadFacade.singleFileUpload(file);
+        if (data.getState().equals("FAILURE")) {
+            return RespDTO.onError(data.getInfo());
+        } else {
+            return RespDTO.onSuc(data);
         }
-        return data;
     }
 
     @PostMapping("/deleteRemoteFile")
     @ApiOperation(value = "知识库标准化-文件删除")
-    public String deleteRemoteFile(@RequestParam("path") String path) {
-        String data = FastJsonUtils.getBeanToJson(uploadFacade.deleteRemoteFile(path));
-        return data;
+    public RespDTO<FileDTO> deleteRemoteFile(@RequestParam("path") String path) {
+        FileDTO data = uploadFacade.deleteRemoteFile(path);
+        if (data.getState().equals("FAILURE")) {
+            return RespDTO.onError(data.getInfo());
+        } else {
+            return RespDTO.onSuc(data);
+        }
     }
 }