Browse Source

CDSS后台管理增加文件上传接口

gaodm 4 years ago
parent
commit
2580728bb6

+ 17 - 0
cdssman-service/pom.xml

@@ -185,6 +185,23 @@
             <scope>runtime</scope>
         </dependency>
 
+        <!-- 文件上传相关架包 -->
+        <dependency>
+            <groupId>commons-fileupload</groupId>
+            <artifactId>commons-fileupload</artifactId>
+            <version>1.3.1</version>
+        </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.4</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.squareup.okhttp3</groupId>
+            <artifactId>okhttp</artifactId>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 33 - 0
cdssman-service/src/main/java/com/diagbot/config/MultipartConfigurer.java

@@ -0,0 +1,33 @@
+package com.diagbot.config;
+
+import org.springframework.boot.web.servlet.MultipartConfigFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.util.unit.DataSize;
+
+import javax.servlet.MultipartConfigElement;
+
+/**
+ * @Description: 配置上传文件大小的配置
+ * @author: gaodm
+ * @time: 2019/5/9 11:18
+ */
+@Configuration
+public class MultipartConfigurer {
+    /**
+     * 配置上传文件大小的配置
+     *
+     * @return
+     */
+    @Bean
+    public MultipartConfigElement multipartConfigElement() {
+        MultipartConfigFactory factory = new MultipartConfigFactory();
+        //  上传文件临时文件夹
+        factory.setLocation(System.getProperty("/data/tmp"));
+        //  单个数据大小
+        factory.setMaxFileSize(DataSize.ofMegabytes(500));
+        /// 总上传数据大小
+        factory.setMaxRequestSize(DataSize.ofMegabytes(500));
+        return factory.createMultipartConfig();
+    }
+}

+ 28 - 0
cdssman-service/src/main/java/com/diagbot/dto/FileDTO.java

@@ -0,0 +1,28 @@
+package com.diagbot.dto;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @Description:
+ * @Author:zhaops
+ * @time: 2018/12/18 17:08
+ */
+@Getter
+@Setter
+public class FileDTO {
+    private String state;
+    private String original;
+    private String title;
+    private String url;
+    private String md5;
+    private String info;
+
+    public FileDTO(String state, String info) {
+        this.state = state;
+        this.info = info;
+    }
+    public FileDTO(){
+
+    }
+}

+ 17 - 0
cdssman-service/src/main/java/com/diagbot/dto/FileDeleteDTO.java

@@ -0,0 +1,17 @@
+package com.diagbot.dto;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2019/11/4 11:09
+ */
+@Getter
+@Setter
+public class FileDeleteDTO {
+    private Object data;
+    private String message;
+    private String status;
+}

+ 25 - 0
cdssman-service/src/main/java/com/diagbot/dto/FileUploadDTO.java

@@ -0,0 +1,25 @@
+package com.diagbot.dto;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2019/11/4 10:55
+ */
+@Getter
+@Setter
+public class FileUploadDTO {
+    private String url;
+    private String md5;
+    private String path;
+    private String domain;
+    private String scene;
+    private int size;
+    private int mtime;
+    private String scenes;
+    private String retmsg;
+    private int retcode;
+    private String src;
+}

+ 13 - 0
cdssman-service/src/main/java/com/diagbot/facade/UploadFacade.java

@@ -0,0 +1,13 @@
+package com.diagbot.facade;
+
+import com.diagbot.service.impl.UploadServiceImpl;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Description: 文件上传装饰层
+ * @author: gaodm
+ * @time: 2018/11/13 13:19
+ */
+@Component
+public class UploadFacade extends UploadServiceImpl {
+}

+ 13 - 0
cdssman-service/src/main/java/com/diagbot/service/UploadService.java

@@ -0,0 +1,13 @@
+package com.diagbot.service;
+
+import com.diagbot.dto.FileDTO;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * @Description: 文件上传服务接口
+ * @author: gaodm
+ * @time: 2018/11/13 13:50
+ */
+public interface UploadService {
+    FileDTO singleFileUpload(MultipartFile file);
+}

+ 129 - 0
cdssman-service/src/main/java/com/diagbot/service/impl/UploadServiceImpl.java

@@ -0,0 +1,129 @@
+package com.diagbot.service.impl;
+
+import com.diagbot.dto.FileDTO;
+import com.diagbot.dto.FileDeleteDTO;
+import com.diagbot.dto.FileUploadDTO;
+import com.diagbot.service.UploadService;
+import com.diagbot.util.GsonUtil;
+import com.diagbot.util.StringUtil;
+import lombok.extern.slf4j.Slf4j;
+import okhttp3.FormBody;
+import okhttp3.MediaType;
+import okhttp3.MultipartBody;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * @Description: 文件上传服务接口实现
+ * @author: gaodm
+ * @time: 2018/11/13 13:50
+ */
+@Slf4j
+@Service
+public class UploadServiceImpl implements UploadService {
+    @Value("${imageUrl.prefix}")
+    private String imagerUrl;
+
+    @Override
+    public FileDTO singleFileUpload(MultipartFile file) {
+        if (file.isEmpty()) {
+            return new FileDTO("FAILURE", "文件不能为空");
+        }
+        //文件大小上限1M
+        if (file.getSize() > 1024 * 1024) {
+            return new FileDTO("FAILURE", "文件上传失败,超出大小限制1MB");
+        }
+
+        String result = "";
+        try {
+            OkHttpClient httpClient = new OkHttpClient();
+            MultipartBody multipartBody = new MultipartBody.Builder().
+                    setType(MultipartBody.FORM)
+                    .addFormDataPart("file", file.getOriginalFilename(),
+                            RequestBody.create(MediaType.parse("multipart/form-data;charset=utf-8"),
+                                    file.getBytes()))
+                    .addFormDataPart("scene", "M05")
+                    .addFormDataPart("output", "json")
+                    .build();
+
+            Request request = new Request.Builder()
+                    .url(imagerUrl + "/group1/upload")
+                    .post(multipartBody)
+                    .build();
+
+            Response response = httpClient.newCall(request).execute();
+            if (response.isSuccessful()) {
+                ResponseBody body = response.body();
+                if (body != null) {
+                    result = body.string();
+                    //System.out.println(result);
+                }
+            }
+
+            if (StringUtil.isBlank(result)) {
+                return new FileDTO("FAILURE", "文件上传失败,请重新上传");
+            }
+        } catch (Exception e) {
+            log.error("文件上传失败", e);
+            return new FileDTO("FAILURE", "文件上传失败,请重新上传");
+        }
+
+        FileUploadDTO fileUploadDTO = GsonUtil.toObject(result, FileUploadDTO.class);
+        FileDTO fileDTO = new FileDTO("SUCCESS", "文件上传成功");
+        fileDTO.setUrl(fileUploadDTO.getPath());
+        fileDTO.setMd5(fileUploadDTO.getMd5());
+        fileDTO.setOriginal(file.getOriginalFilename());
+        fileDTO.setTitle(file.getOriginalFilename());
+        return fileDTO;
+    }
+
+    /**
+     * 删除服务端文件
+     *
+     * @param md5
+     * @return
+     */
+    public FileDTO deleteRemoteFile(String md5) {
+        String result = "";
+        try {
+            OkHttpClient httpClient = new OkHttpClient();
+            RequestBody formBody = new FormBody.Builder()
+                    .add("md5", md5)
+                    .build();
+
+            Request request = new Request.Builder()
+                    .url(imagerUrl + "/group1/delete")
+                    .post(formBody)
+                    .build();
+
+            Response response = httpClient.newCall(request).execute();
+            if (response.isSuccessful()) {
+                ResponseBody body = response.body();
+                if (body != null) {
+                    result = body.string();
+                    //System.out.println(result);
+                }
+            }
+
+            if (StringUtil.isBlank(result)) {
+                return new FileDTO("FAILURE", "文件删除失败");
+            }
+
+        } catch (Exception e) {
+            log.error("", e);
+            return new FileDTO("FAILURE", "文件删除失败");
+        }
+
+        FileDeleteDTO fileDeleteDTO = GsonUtil.toObject(result, FileDeleteDTO.class);
+        if (fileDeleteDTO.getStatus().equals("fail")) {
+            return new FileDTO("FAILURE", fileDeleteDTO.getMessage());
+        }
+        return new FileDTO("SUCCESS", "文件删除成功");
+    }
+}

+ 48 - 0
cdssman-service/src/main/java/com/diagbot/web/UploadController.java

@@ -0,0 +1,48 @@
+package com.diagbot.web;
+
+import com.diagbot.dto.FileDTO;
+import com.diagbot.dto.RespDTO;
+import com.diagbot.facade.UploadFacade;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.CrossOrigin;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+@RestController
+@Api(value = "文件上传API", tags = { "知识库标准化-文件上传API" })
+@RequestMapping(value = "/file")
+@SuppressWarnings("unchecked")
+public class UploadController {
+    @Autowired
+    private UploadFacade uploadFacade;
+
+    @ApiOperation(value = "知识库标准化-文件上传")
+    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = { RequestMethod.POST }, origins = "*")
+    @PostMapping(value = "/uploadImage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
+    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);
+        }
+    }
+
+    @PostMapping("/deleteRemoteFile")
+    @ApiOperation(value = "知识库标准化-文件删除")
+    public RespDTO<FileDTO> deleteRemoteFile(@RequestParam("md5") String md5) {
+        FileDTO data = uploadFacade.deleteRemoteFile(md5);
+        if (data.getState().equals("FAILURE")) {
+            return RespDTO.onError(data.getInfo());
+        } else {
+            return RespDTO.onSuc(data);
+        }
+    }
+}

+ 3 - 0
config-server/src/main/resources/shared/cdssman-service-dev.yml

@@ -80,3 +80,6 @@ mybatis-plus:
   configuration:
     map-underscore-to-camel-case: true
     cache-enabled: false
+
+imageUrl:
+  prefix: http://192.168.2.236:82

+ 3 - 0
config-server/src/main/resources/shared/cdssman-service-local.yml

@@ -80,3 +80,6 @@ mybatis-plus:
   configuration:
     map-underscore-to-camel-case: true
     cache-enabled: false
+
+imageUrl:
+  prefix: http://192.168.2.236:82

+ 3 - 0
config-server/src/main/resources/shared/cdssman-service-pre.yml

@@ -80,3 +80,6 @@ mybatis-plus:
   configuration:
     map-underscore-to-camel-case: true
     cache-enabled: false
+
+imageUrl:
+  prefix: http://192.168.2.121:82

+ 3 - 0
config-server/src/main/resources/shared/cdssman-service-pro.yml

@@ -80,3 +80,6 @@ mybatis-plus:
   configuration:
     map-underscore-to-camel-case: true
     cache-enabled: false
+
+imageUrl:
+  prefix: http://192.168.2.122:82

+ 3 - 0
config-server/src/main/resources/shared/cdssman-service-test.yml

@@ -80,3 +80,6 @@ mybatis-plus:
   configuration:
     map-underscore-to-camel-case: true
     cache-enabled: false
+
+imageUrl:
+  prefix: http://192.168.2.241:82