Преглед на файлове

文件上传删除接口

gaodm преди 4 години
родител
ревизия
d55a397525

+ 17 - 0
pom.xml

@@ -188,6 +188,23 @@
             <artifactId>commons-pool2</artifactId>
         </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>
+
         <dependency>
             <groupId>io.github.lvyahui8</groupId>
             <artifactId>spring-boot-data-aggregator-starter</artifactId>

+ 2 - 0
src/main/java/com/diagbot/config/ResourceServerConfigurer.java

@@ -134,6 +134,8 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
                 .antMatchers("/sys/dictionaryInfo/getList").permitAll()
                 .antMatchers("/sys/dictionaryInfo/getListBack").permitAll()
                 .antMatchers("/sys/plan/getPlanInfoIds").permitAll()
+                .antMatchers("/sys/file/uploadImage").permitAll()
+                .antMatchers("/sys/file/deleteRemoteFile").permitAll()
                 .antMatchers("/**").authenticated();
         //                .antMatchers("/**").permitAll();
     }

+ 2 - 0
src/main/java/com/diagbot/config/security/UrlAccessDecisionManager.java

@@ -176,6 +176,8 @@ public class UrlAccessDecisionManager implements AccessDecisionManager {
                 || matchers("/sys/dictionaryInfo/getList", request)
                 || matchers("/sys/dictionaryInfo/getListBack", request)
                 || matchers("/sys/plan/getPlanInfoIds", request)
+                || matchers("/sys/file/uploadImage", request)
+                || matchers("/sys/file/deleteRemoteFile", request)
                 || matchers("/", request)) {
             return true;
         }

+ 28 - 0
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
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
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
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
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);
+}

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

@@ -0,0 +1,134 @@
+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;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @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", "文件不能为空");
+        }
+        //文件大小上限500M
+        if (file.getSize() > 1024 * 1024 * 500) {
+            return new FileDTO("FAILURE", "文件上传失败,超出大小限制500MB");
+        }
+
+        String result = "";
+        try {
+            OkHttpClient httpClient = new OkHttpClient.Builder()
+                    .connectTimeout(500, TimeUnit.SECONDS)
+                    .readTimeout(500, TimeUnit.SECONDS)
+                    .build();
+            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
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 = "/sys/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);
+        }
+    }
+}

+ 4 - 0
src/main/resources/application-dev.yml

@@ -169,6 +169,10 @@ swagger:
 tokenAuth:
   enable: false
 
+#图片服务器
+imageUrl:
+  prefix: http://192.168.2.236:82
+
 #病历质控地址
 mrqc:
   url: http://192.168.2.236:5858

+ 4 - 0
src/main/resources/application-local.yml

@@ -169,6 +169,10 @@ swagger:
 tokenAuth:
   enable: false
 
+#图片服务器
+imageUrl:
+  prefix: http://192.168.2.236:82
+
 #病历质控地址
 mrqc:
   url: http://192.168.2.236:5858

+ 4 - 0
src/main/resources/application-pre.yml

@@ -169,6 +169,10 @@ swagger:
 tokenAuth:
   enable: false
 
+#图片服务器
+imageUrl:
+  prefix: http://192.168.2.121:82
+
 #病历质控地址
 mrqc:
   url: http://192.168.2.121:5858

+ 4 - 0
src/main/resources/application-pro.yml

@@ -169,6 +169,10 @@ swagger:
 tokenAuth:
   enable: false
 
+#图片服务器
+imageUrl:
+  prefix: http://192.168.2.122:82
+
 #病历质控地址
 mrqc:
   url: http://192.168.2.122:5858

+ 4 - 0
src/main/resources/application-test.yml

@@ -169,6 +169,10 @@ swagger:
 tokenAuth:
   enable: false
 
+#图片服务器
+imageUrl:
+  prefix: http://192.168.2.241:82
+
 #病历质控地址
 mrqc:
   url: http://192.168.2.241:5858