Explorar o código

预问诊文件上传

zhoutg %!s(int64=5) %!d(string=hai) anos
pai
achega
8a150dbb12

+ 0 - 8
config-server/src/main/resources/shared/prec-service-dev.yml

@@ -95,14 +95,6 @@ mybatis-plus:
     map-underscore-to-camel-case: true
     cache-enabled: false
 
-fastdfs:
-  connect_timeout_in_seconds: 60
-  network_timeout_in_seconds: 60
-  charset: UTF-8
-  http_tracker_http_port: 8080
-  http_anti_steal_token: no
-  tracker_servers: 192.168.2.236:22122
-
 imageUrl:
   prefix: http://192.168.2.236:82
 

+ 0 - 8
config-server/src/main/resources/shared/prec-service-local.yml

@@ -95,14 +95,6 @@ mybatis-plus:
     map-underscore-to-camel-case: true
     cache-enabled: false
 
-fastdfs:
-  connect_timeout_in_seconds: 60
-  network_timeout_in_seconds: 60
-  charset: UTF-8
-  http_tracker_http_port: 8080
-  http_anti_steal_token: no
-  tracker_servers: 192.168.2.236:22122
-
 imageUrl:
   prefix: http://192.168.2.236:82
 

+ 0 - 8
config-server/src/main/resources/shared/prec-service-pro.yml

@@ -95,14 +95,6 @@ mybatis-plus:
     map-underscore-to-camel-case: true
     cache-enabled: false
 
-fastdfs:
-  connect_timeout_in_seconds: 60
-  network_timeout_in_seconds: 60
-  charset: UTF-8
-  http_tracker_http_port: 8080
-  http_anti_steal_token: no
-  tracker_servers: 192.168.2.236:22122
-
 imageUrl:
   prefix: http://192.168.2.236:82
 

+ 0 - 9
config-server/src/main/resources/shared/prec-service-test.yml

@@ -95,15 +95,6 @@ mybatis-plus:
     map-underscore-to-camel-case: true
     cache-enabled: false
 
-
-fastdfs:
-  connect_timeout_in_seconds: 60
-  network_timeout_in_seconds: 60
-  charset: UTF-8
-  http_tracker_http_port: 8080
-  http_anti_steal_token: no
-  tracker_servers: 192.168.2.241:22122
-
 imageUrl:
   prefix: http://192.168.2.241:82
 

+ 4 - 5
prec-service/pom.xml

@@ -154,11 +154,6 @@
             <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>net.oschina.zcx7878</groupId>
-            <artifactId>fastdfs-client-java</artifactId>
-            <version>1.27.0.0</version>
-        </dependency>
         <!-- 文件上传相关架包 -->
         <dependency>
             <groupId>commons-fileupload</groupId>
@@ -182,6 +177,10 @@
             <artifactId>spring-boot-data-aggregator-starter</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.squareup.okhttp3</groupId>
+            <artifactId>okhttp</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 29 - 28
prec-service/src/main/java/com/diagbot/aggregate/UploadAggregate.java

@@ -1,5 +1,6 @@
 package com.diagbot.aggregate;
 
+import com.diagbot.dto.FileDTO;
 import com.diagbot.dto.FileThumDTO;
 import com.diagbot.facade.UploadFacade;
 import com.diagbot.util.ListUtil;
@@ -23,80 +24,80 @@ public class UploadAggregate {
     private UploadFacade uploadFacade;
 
     @DataProvider("thumUploads")
-    public List<FileThumDTO> setAll(
-            @DataConsumer("thumUpload1") FileThumDTO fileThumDTO1,
-            @DataConsumer("thumUpload2") FileThumDTO fileThumDTO2,
-            @DataConsumer("thumUpload3") FileThumDTO fileThumDTO3,
-            @DataConsumer("thumUpload4") FileThumDTO fileThumDTO4,
-            @DataConsumer("thumUpload5") FileThumDTO fileThumDTO5,
-            @DataConsumer("thumUpload6") FileThumDTO fileThumDTO6) {
-        List<FileThumDTO> fileThumDTOList = ListUtil.newArrayList();
-        if (null != fileThumDTO1){
-            fileThumDTOList.add(fileThumDTO1);
+    public List<FileDTO> setAll(
+            @DataConsumer("thumUpload1") FileDTO fileDTO1,
+            @DataConsumer("thumUpload2") FileDTO fileDTO2,
+            @DataConsumer("thumUpload3") FileDTO fileDTO3,
+            @DataConsumer("thumUpload4") FileDTO fileDTO4,
+            @DataConsumer("thumUpload5") FileDTO fileDTO5,
+            @DataConsumer("thumUpload6") FileDTO fileDTO6) {
+        List<FileDTO> fileThumDTOList = ListUtil.newArrayList();
+        if (null != fileDTO1){
+            fileThumDTOList.add(fileDTO1);
         }
 
-        if (null != fileThumDTO2){
-            fileThumDTOList.add(fileThumDTO2);
+        if (null != fileDTO2){
+            fileThumDTOList.add(fileDTO2);
         }
 
-        if (null != fileThumDTO3){
-            fileThumDTOList.add(fileThumDTO3);
+        if (null != fileDTO3){
+            fileThumDTOList.add(fileDTO3);
         }
 
-        if (null != fileThumDTO4){
-            fileThumDTOList.add(fileThumDTO4);
+        if (null != fileDTO4){
+            fileThumDTOList.add(fileDTO4);
         }
 
-        if (null != fileThumDTO5){
-            fileThumDTOList.add(fileThumDTO5);
+        if (null != fileDTO5){
+            fileThumDTOList.add(fileDTO5);
         }
-        if (null != fileThumDTO6){
-            fileThumDTOList.add(fileThumDTO6);
+        if (null != fileDTO6){
+            fileThumDTOList.add(fileDTO6);
         }
 
         return fileThumDTOList;
     }
 
     @DataProvider("thumUpload1")
-    public FileThumDTO thumUpload1(@InvokeParameter("file1") MultipartFile file,
+    public FileDTO thumUpload1(@InvokeParameter("file1") MultipartFile file,
                                    @InvokeParameter("type1") Integer type) {
         return this.singleFileThumUpload(file, type);
     }
 
     @DataProvider("thumUpload2")
-    public FileThumDTO thumUpload2(@InvokeParameter("file2") MultipartFile file,
+    public FileDTO thumUpload2(@InvokeParameter("file2") MultipartFile file,
                                    @InvokeParameter("type2") Integer type) {
         return this.singleFileThumUpload(file, type);
     }
 
     @DataProvider("thumUpload3")
-    public FileThumDTO thumUpload3(@InvokeParameter("file3") MultipartFile file,
+    public FileDTO thumUpload3(@InvokeParameter("file3") MultipartFile file,
                                    @InvokeParameter("type3") Integer type) {
         return this.singleFileThumUpload(file, type);
     }
 
     @DataProvider("thumUpload4")
-    public FileThumDTO thumUpload4(@InvokeParameter("file4") MultipartFile file,
+    public FileDTO thumUpload4(@InvokeParameter("file4") MultipartFile file,
                                    @InvokeParameter("type4") Integer type) {
         return this.singleFileThumUpload(file, type);
     }
 
     @DataProvider("thumUpload5")
-    public FileThumDTO thumUpload5(@InvokeParameter("file5") MultipartFile file,
+    public FileDTO thumUpload5(@InvokeParameter("file5") MultipartFile file,
                                    @InvokeParameter("type5") Integer type) {
         return this.singleFileThumUpload(file, type);
     }
 
     @DataProvider("thumUpload6")
-    public FileThumDTO thumUpload6(@InvokeParameter("file6") MultipartFile file,
+    public FileDTO thumUpload6(@InvokeParameter("file6") MultipartFile file,
                                    @InvokeParameter("type6") Integer type) {
         return this.singleFileThumUpload(file, type);
     }
 
-    private FileThumDTO singleFileThumUpload(MultipartFile file, Integer type) {
+    private FileDTO singleFileThumUpload(MultipartFile file, Integer type) {
         if (file == null) {
             return null;
         }
-        return uploadFacade.singleFileThumUpload2(file, type);
+        return uploadFacade.singleFileUpload(file, type);
     }
 }

+ 0 - 161
prec-service/src/main/java/com/diagbot/client/fastdfs/FastDFSClient.java

@@ -1,161 +0,0 @@
-package com.diagbot.client.fastdfs;
-
-import org.csource.common.NameValuePair;
-import org.csource.fastdfs.ClientGlobal;
-import org.csource.fastdfs.FileInfo;
-import org.csource.fastdfs.ServerInfo;
-import org.csource.fastdfs.StorageClient;
-import org.csource.fastdfs.StorageServer;
-import org.csource.fastdfs.TrackerClient;
-import org.csource.fastdfs.TrackerServer;
-import org.slf4j.LoggerFactory;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- * @Description: FastDFS 客户端
- * @author: gaodm
- * @time: 2018/11/13 13:55
- */
-public class FastDFSClient {
-    private static org.slf4j.Logger logger = LoggerFactory.getLogger(FastDFSClient.class);
-
-    public static String[] upload(FastDFSFile file) {
-        logger.info("File Name: " + file.getName() + "File Length:" + file.getContent().length);
-
-        // 设置元信息
-        NameValuePair[] meta_list = new NameValuePair[2];
-        meta_list[0] = new NameValuePair("name", file.getName());
-        meta_list[1] = new NameValuePair("ext", file.getExt());
-
-        long startTime = System.currentTimeMillis();
-        String[] uploadResults = null;
-        StorageClient storageClient = null;
-        try {
-            storageClient = getTrackerClient();
-            uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
-        } catch (IOException e) {
-            logger.error("IO Exception when uploadind the file:" + file.getName(), e);
-        } catch (Exception e) {
-            logger.error("Non IO Exception when uploadind the file:" + file.getName(), e);
-        }
-        logger.info("upload_file time used:" + (System.currentTimeMillis() - startTime) + " ms");
-
-        if (uploadResults == null && storageClient != null) {
-            logger.error("upload file fail, error code:" + storageClient.getErrorCode());
-        }
-        String groupName = uploadResults[0];
-        String remoteFileName = uploadResults[1];
-
-        logger.info("upload file successfully!!!" + "group_name:" + groupName + ", remoteFileName:" + " " + remoteFileName);
-        return uploadResults;
-    }
-
-    public static FileInfo getFile(String groupName, String remoteFileName) {
-        try {
-            StorageClient storageClient = getTrackerClient();
-            return storageClient.get_file_info(groupName, remoteFileName);
-        } catch (IOException e) {
-            logger.error("IO Exception: Get File from Fast DFS failed", e);
-        } catch (Exception e) {
-            logger.error("Non IO Exception: Get File from Fast DFS failed", e);
-        }
-        return null;
-    }
-
-    public static InputStream downFile(String groupName, String remoteFileName) {
-        try {
-            StorageClient storageClient = getTrackerClient();
-            byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
-            InputStream ins = new ByteArrayInputStream(fileByte);
-            return ins;
-        } catch (IOException e) {
-            logger.error("IO Exception: Get File from Fast DFS failed", e);
-        } catch (Exception e) {
-            logger.error("Non IO Exception: Get File from Fast DFS failed", e);
-        }
-        return null;
-    }
-
-    public static void deleteFile(String groupName, String remoteFileName)
-            throws Exception {
-        StorageClient storageClient = getTrackerClient();
-        int i = storageClient.delete_file(groupName, remoteFileName);
-        logger.info("delete file successfully!!!" + i);
-    }
-
-    public static StorageServer[] getStoreStorages(String groupName)
-            throws IOException {
-        TrackerClient trackerClient = new TrackerClient();
-        TrackerServer trackerServer = trackerClient.getConnection();
-        return trackerClient.getStoreStorages(trackerServer, groupName);
-    }
-
-    public static ServerInfo[] getFetchStorages(String groupName,
-                                                String remoteFileName) throws IOException {
-        TrackerClient trackerClient = new TrackerClient();
-        TrackerServer trackerServer = trackerClient.getConnection();
-        return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
-    }
-
-    public static String getTrackerUrl() throws IOException {
-        return "http://" + getTrackerServer().getInetSocketAddress().getHostString() + ":" + ClientGlobal.getG_tracker_http_port() + "/";
-    }
-
-    private static StorageClient getTrackerClient() throws IOException {
-        TrackerClient trackerClient = new TrackerClient();
-        TrackerServer trackerServer = trackerClient.getConnection();
-        StorageServer storageServer = getStorageServer(getStorageServerIp(trackerClient, trackerServer));
-        StorageClient storageClient
-                = new StorageClient(trackerServer, storageServer);
-        return storageClient;
-    }
-
-    private static TrackerServer getTrackerServer() throws IOException {
-        TrackerClient trackerClient = new TrackerClient();
-        TrackerServer trackerServer = trackerClient.getConnection();
-        return trackerServer;
-    }
-
-    /**
-     * 获得可用的storage IP
-     *
-     * @param trackerClient
-     * @param trackerServer
-     * @return 返回storage IP
-     */
-    private static String getStorageServerIp(TrackerClient trackerClient, TrackerServer trackerServer) {
-        String storageIp = null;
-        if (trackerClient != null && trackerServer != null) {
-            try {
-                StorageServer storageServer = trackerClient.getStoreStorage(trackerServer, "group1");
-                storageIp = storageServer.getSocket().getInetAddress().getHostAddress();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
-        logger.info("获取组中可用的storage IP:" + storageIp);
-        return storageIp;
-    }
-
-    /**
-     * 得到Storage服务
-     *
-     * @param storageIp
-     * @return 返回Storage服务
-     */
-    private static StorageServer getStorageServer(String storageIp) {
-        StorageServer storageServer = null;
-        if (storageIp != null && !("").equals(storageIp)) {
-            try {
-                storageServer = new StorageServer(storageIp, Integer.parseInt("23000"), Integer.parseInt("1"));
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
-        logger.info("storage server生成");
-        return storageServer;
-    }
-}

+ 0 - 70
prec-service/src/main/java/com/diagbot/client/fastdfs/FastDFSFile.java

@@ -1,70 +0,0 @@
-package com.diagbot.client.fastdfs;
-
-public class FastDFSFile {
-    private String name;
-
-    private byte[] content;
-
-    private String ext;
-
-    private String md5;
-
-    private String author;
-
-    public FastDFSFile(String name, byte[] content, String ext, String height,
-                       String width, String author) {
-        super();
-        this.name = name;
-        this.content = content;
-        this.ext = ext;
-        this.author = author;
-    }
-
-    public FastDFSFile(String name, byte[] content, String ext) {
-        super();
-        this.name = name;
-        this.content = content;
-        this.ext = ext;
-
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-
-    public byte[] getContent() {
-        return content;
-    }
-
-    public void setContent(byte[] content) {
-        this.content = content;
-    }
-
-    public String getExt() {
-        return ext;
-    }
-
-    public void setExt(String ext) {
-        this.ext = ext;
-    }
-
-    public String getMd5() {
-        return md5;
-    }
-
-    public void setMd5(String md5) {
-        this.md5 = md5;
-    }
-
-    public String getAuthor() {
-        return author;
-    }
-
-    public void setAuthor(String author) {
-        this.author = author;
-    }
-}

+ 0 - 49
prec-service/src/main/java/com/diagbot/config/FastDFSConfigurer.java

@@ -1,49 +0,0 @@
-package com.diagbot.config;
-
-import lombok.extern.slf4j.Slf4j;
-import org.csource.fastdfs.ClientGlobal;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import java.util.Properties;
-
-/**
- * @Description:
- * @author: gaodm
- * @time: 2019/3/1 14:15
- */
-@Configuration
-@Slf4j
-public class FastDFSConfigurer {
-    @Value("${fastdfs.connect_timeout_in_seconds}")
-    private String connectTimeout;
-    @Value("${fastdfs.network_timeout_in_seconds}")
-    private String networkTimeout;
-    @Value("${fastdfs.charset}")
-    private String charset;
-    @Value("${fastdfs.http_tracker_http_port}")
-    private String httpTrackerHttpPort;
-    @Value("${fastdfs.http_anti_steal_token}")
-    private String httpAntiStealToken;
-    @Value("${fastdfs.tracker_servers}")
-    private String trackerServers;
-
-    @Bean
-    public Integer fastDFSInit(){
-        try {
-            Properties props = new Properties();
-            props.put(ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS, connectTimeout);
-            props.put(ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS, networkTimeout);
-            props.put(ClientGlobal.PROP_KEY_CHARSET, charset);
-            props.put(ClientGlobal.PROP_KEY_HTTP_TRACKER_HTTP_PORT, httpTrackerHttpPort);
-            props.put(ClientGlobal.PROP_KEY_HTTP_ANTI_STEAL_TOKEN, httpAntiStealToken);
-            props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, trackerServers);
-            ClientGlobal.initByProperties(props);
-
-        } catch (Exception e) {
-            log.error("FastDFS Client Init Fail!", e);
-        }
-        return 1;
-    }
-}

+ 3 - 9
prec-service/src/main/java/com/diagbot/dto/FileDTO.java

@@ -15,21 +15,15 @@ public class FileDTO {
     private String original;
     private String title;
     private String url;
+    private String md5;
     private String info;
     private Integer type;
 
-    public FileDTO(String state, String info, Integer type) {
-        this.state = state;
-        this.info = info;
-        this.type = type;
-    }
-
     public FileDTO(String state, String info) {
         this.state = state;
         this.info = info;
     }
-
-    public FileDTO() {
+    public FileDTO(){
 
     }
-}
+}

+ 17 - 0
prec-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
prec-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;
+}

+ 5 - 14
prec-service/src/main/java/com/diagbot/service/UploadService.java

@@ -12,21 +12,12 @@ import java.util.List;
  * @time: 2018/11/13 13:50
  */
 public interface UploadService {
-    //单文件上传
-    FileDTO singleFileUpload(MultipartFile file);
-
-    //单文件上传同时生成缩略图
-    FileThumDTO singleFileThumUpload(MultipartFile file);
 
-    //多文件上传
-    List<FileDTO> multiFileUpload(MultipartFile[] mpfs);
+    FileDTO singleFileUpload(MultipartFile file);
 
-    //多文件上传同时生成缩略图
-    List<FileThumDTO> multiFileThumUpload(MultipartFile[] mpfs, Integer[] type);
+    //多文件上传(多线程)
+    List<FileDTO> multiFileThumUpload(MultipartFile[] mpfs, Integer[] type);
 
-    //多文件上传同时生成缩略图(多线程)
-    List<FileThumDTO> multiFileThumUpload2(MultipartFile[] mpfs, Integer[] type);
+    FileDTO singleFileUpload(MultipartFile file, Integer type);
 
-    //单文件上传同时生成缩略图
-    FileThumDTO singleFileThumUpload2(MultipartFile file, Integer type);
-}
+}

+ 96 - 217
prec-service/src/main/java/com/diagbot/service/impl/UploadServiceImpl.java

@@ -1,23 +1,29 @@
 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.dto.FileDeleteDTO;
 import com.diagbot.dto.FileThumDTO;
+import com.diagbot.dto.FileUploadDTO;
 import com.diagbot.exception.CommonErrorCode;
 import com.diagbot.exception.CommonException;
 import com.diagbot.service.UploadService;
+import com.diagbot.util.GsonUtil;
+import com.diagbot.util.StringUtil;
 import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade;
 import lombok.extern.slf4j.Slf4j;
-import net.coobird.thumbnailator.Thumbnails;
+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.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -34,121 +40,111 @@ public class UploadServiceImpl implements UploadService {
     @Autowired
     private DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade;
 
+    @Value("${imageUrl.prefix}")
+    private String imagerUrl;
+
     @Override
     public FileDTO singleFileUpload(MultipartFile file) {
         if (file.isEmpty()) {
             return new FileDTO("FAILURE", "文件不能为空");
         }
-        //文件大小上限4M
-        if (file.getSize() > 4 * 1024 * 1024) {
+        //文件大小上限1M
+        if (file.getSize() > 1024 * 1024 * 4) {
             return new FileDTO("FAILURE", "文件上传失败,超出大小限制4MB");
         }
+
+        String result = "";
         try {
-            FileDTO fileDTO = saveFile(file, false);
-            return fileDTO;
+            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", "M01")
+                    .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", "文件上传失败,请重新上传");
         }
-    }
 
-    @Override
-    public FileThumDTO singleFileThumUpload(MultipartFile file) {
-        FileThumDTO fileThumDTO = new FileThumDTO();
-        if (file.isEmpty()) {
-            fileThumDTO.setSource(new FileDTO("FAILURE", "文件不能为空"));
-            return fileThumDTO;
-        }
-        //文件大小上限4M
-        if (file.getSize() > 4 * 1024 * 1024) {
-            fileThumDTO.setSource(new FileDTO("FAILURE", "文件上传失败,超出大小限制4MB"));
-            return fileThumDTO;
-        }
-        try {
-            fileThumDTO = saveFileWithThum(file);
-            return fileThumDTO;
-        } catch (Exception e) {
-            log.error("文件上传失败", e);
-            fileThumDTO.setSource(new FileDTO("FAILURE", "文件上传失败,请重新上传"));
-            return fileThumDTO;
-        }
+        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;
     }
 
-    @Override
-    public List<FileDTO> multiFileUpload(MultipartFile[] mpfs) {
-        // 上传文件返回的路径集合
-        List<FileDTO> fileDTOS = new ArrayList<>();
-        if (null == mpfs) {
-            throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "文件不能为空");
-        }
-
-        for (MultipartFile file : mpfs) {
-            if (file.isEmpty()) {
-                fileDTOS.add(new FileDTO("FAILURE", "文件不能为空"));
-                continue;
-            }
-            //文件大小上限4M
-            if (file.getSize() > 4 * 1024 * 1024) {
-                fileDTOS.add(new FileDTO("FAILURE", "文件上传失败,超出大小限制4MB"));
-                continue;
+    /**
+     * 删除服务端文件
+     *
+     * @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);
+                }
             }
 
-            try {
-                FileDTO fileDTO = saveFile(file, false);
-                fileDTOS.add(fileDTO);
-            } catch (Exception e) {
-                log.error("文件上传失败", e);
-                fileDTOS.add(new FileDTO("FAILURE", "文件上传失败,请重新上传"));
+            if (StringUtil.isBlank(result)) {
+                return new FileDTO("FAILURE", "文件删除失败");
             }
-        }
-        return fileDTOS;
-    }
 
-
-    @Override
-    public List<FileThumDTO> multiFileThumUpload(MultipartFile[] mpfs, Integer[] type) {
-        // 上传文件返回的路径集合
-        List<FileThumDTO> fileDTOS = new ArrayList<>();
-        if (null == mpfs) {
-            throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "文件不能为空");
+        } catch (Exception e) {
+            log.error("", e);
+            return new FileDTO("FAILURE", "文件删除失败");
         }
 
-        FileThumDTO fileThumDTO = null;
-        for (int i = 0; i < mpfs.length; i++) {
-            MultipartFile file = mpfs[i];
-            if (file.isEmpty()) {
-                fileThumDTO = new FileThumDTO();
-                fileThumDTO.setSource(new FileDTO("FAILURE", "文件不能为空", type[i]));
-                fileDTOS.add(fileThumDTO);
-                continue;
-            }
-            //文件大小上限4M
-            if (file.getSize() > 4 * 1024 * 1024) {
-                fileThumDTO = new FileThumDTO();
-                fileThumDTO.setSource(new FileDTO("FAILURE", "文件上传失败,超出大小限制4MB", type[i]));
-                fileDTOS.add(fileThumDTO);
-                continue;
-            }
-
-            try {
-                fileThumDTO = saveFileWithThum(file);
-                fileThumDTO.getSource().setType(type[i]);
-                fileThumDTO.getThumbnail().setType(type[i]);
-                fileDTOS.add(fileThumDTO);
-            } catch (Exception e) {
-                log.error("文件上传失败", e);
-                fileThumDTO = new FileThumDTO();
-                fileThumDTO.setSource(new FileDTO("FAILURE", "文件上传失败,请重新上传", type[i]));
-                fileDTOS.add(fileThumDTO);
-            }
+        FileDeleteDTO fileDeleteDTO = GsonUtil.toObject(result, FileDeleteDTO.class);
+        if (fileDeleteDTO.getStatus().equals("fail")) {
+            return new FileDTO("FAILURE", fileDeleteDTO.getMessage());
         }
-        return fileDTOS;
+        return new FileDTO("SUCCESS", "文件删除成功");
     }
 
+
     @Override
-    public List<FileThumDTO> multiFileThumUpload2(MultipartFile[] mpfs, Integer[] type) {
+    public List<FileDTO> multiFileThumUpload(MultipartFile[] mpfs, Integer[] type) {
         // 上传文件返回的路径集合
-        List<FileThumDTO> fileDTOS = new ArrayList<>();
+        List<FileDTO> fileDTOS = new ArrayList<>();
         try {
             Map<String, Object> invokeParams = new HashMap<>();
             for (int i = 0; i < 6; i++) {
@@ -171,128 +167,11 @@ public class UploadServiceImpl implements UploadService {
         return fileDTOS;
     }
 
-    @Override
-    public FileThumDTO singleFileThumUpload2(MultipartFile file, Integer type) {
-        FileThumDTO fileThumDTO = new FileThumDTO();
-        if (file.isEmpty()) {
-            fileThumDTO.setSource(new FileDTO("FAILURE", "文件不能为空", type));
-            return fileThumDTO;
-        }
-        //文件大小上限4M
-        if (file.getSize() > 4 * 1024 * 1024) {
-            fileThumDTO.setSource(new FileDTO("FAILURE", "文件上传失败,超出大小限制4MB", type));
-            return fileThumDTO;
-        }
-        try {
-            fileThumDTO = saveFileWithThum(file);
-            fileThumDTO.getSource().setType(type);
-            fileThumDTO.getThumbnail().setType(type);
-            return fileThumDTO;
-        } catch (Exception e) {
-            log.error("文件上传失败", e);
-            fileThumDTO.setSource(new FileDTO("FAILURE", "文件上传失败,请重新上传", type));
-            return fileThumDTO;
-        }
-    }
-
-    /**
-     * @param multipartFile
-     * @return
-     * @throws IOException
-     */
-    public FileThumDTO saveFileWithThum(MultipartFile multipartFile) throws IOException {
-        FileThumDTO fileThumDTO = new FileThumDTO();
-        //原图上传到服务器
-        FileDTO source = saveFile(multipartFile);
-        fileThumDTO.setSource(source);
-        //压缩图片
-        FileDTO thumbnail = saveFile(multipartFile, true);
-        fileThumDTO.setThumbnail(thumbnail);
-        return fileThumDTO;
-    }
-
-    /**
-     * @param multipartFile
-     * @return
-     * @throws IOException
-     */
-    public FileDTO saveFile(MultipartFile multipartFile) throws IOException {
-        return saveFile(multipartFile, false);
-    }
-
-    /**
-     * @param multipartFile
-     * @return
-     * @throws IOException
-     */
-    public FileDTO saveFile(MultipartFile multipartFile, Boolean isThum) throws IOException {
-        String[] fileAbsolutePath = {};
-        String fileName = multipartFile.getOriginalFilename();
-        String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
-        byte[] file_buff = null;
-        InputStream inputStream = multipartFile.getInputStream();
-        //压缩图片
-        if (isThum) {
-            Integer width = 280;
-            Integer hight = 430;
-            //文件名重新命名
-            fileName = width + "_" + hight + "_" + fileName;
-            inputStream = compress(inputStream, width, hight);
-        }
 
-        if (inputStream != null) {
-            int len1 = inputStream.available();
-            file_buff = new byte[len1];
-            inputStream.read(file_buff);
-        }
-        inputStream.close();
-        FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
-        try {
-            fileAbsolutePath = FastDFSClient.upload(file);  //upload to fastdfs
-        } catch (Exception e) {
-            log.error("文件上传异常", e);
-            return new FileDTO("FAILURE", "文件上传异常");
-        }
-        if (fileAbsolutePath == null) {
-            log.error("文件上传失败,请重新上传");
-            return new FileDTO("FAILURE", "文件上传失败,请重新上传");
-        }
-        String path = "/" + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
-        FileDTO fileDTO = new FileDTO("SUCCESS", "文件上传成功");
-        fileDTO.setUrl(path);
-        fileDTO.setOriginal(fileName);
-        fileDTO.setTitle(fileName);
+    @Override
+    public FileDTO singleFileUpload(MultipartFile file, Integer type) {
+        FileDTO fileDTO = singleFileUpload(file);
+        fileDTO.setType(type);
         return fileDTO;
-
-    }
-
-    public ByteArrayInputStream compress(InputStream inputStream, Integer width, Integer hight) throws IOException {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        Thumbnails.of(inputStream)
-                .size(width, hight)
-                .toOutputStream(baos);
-        final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
-        return swapStream;
-    }
-
-    /**
-     * 删除服务端文件
-     *
-     * @param path
-     * @return
-     */
-    public FileDTO 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);
-            return new FileDTO("FAILURE", "文件删除失败");
-        }
-        return new FileDTO("SUCCESS", "文件删除成功");
     }
 }

+ 11 - 65
prec-service/src/main/java/com/diagbot/web/UploadController.java

@@ -43,76 +43,29 @@ public class UploadController {
         }
     }
 
-    @ApiOperation(value = "智能预问诊-单个文件上传同时生成缩略图")
-    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = { RequestMethod.POST }, origins = "*")
-    @PostMapping(value = "/uploadImageThum", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
-    public RespDTO<FileThumDTO> singleFileThumUpload(@RequestParam("upfile") MultipartFile file) {
-        FileThumDTO data = uploadFacade.singleFileThumUpload(file);
-        if (null != data.getSource() &&
-                data.getSource().getState().equals("FAILURE")) {
-            return RespDTO.onError(data.getSource().getInfo());
-        } else {
-            return RespDTO.onSuc(data);
-        }
-    }
 
-    @ApiOperation(value = "智能预问诊-多个文件上传")
-    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = { RequestMethod.POST }, origins = "*")
-    @PostMapping(value = "/uploadImages", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
-    public RespDTO<List<FileDTO>> multiFileUpload(@RequestParam("upfiles") MultipartFile[] file) {
-        List<FileDTO> data = uploadFacade.multiFileUpload(file);
-        String msg = "";
-        if (ListUtil.isNotEmpty(data)) {
-            for (int i = 0; i < data.size(); i++) {
-                if (data.get(i).getState().equals("FAILURE")) {
-                    msg += "第【" + (i + 1) + "】张图片上传失败," + data.get(i).getInfo() + ";";
-                }
-            }
-        }
-        if (StringUtil.isNotBlank(msg)) {
-            return RespDTO.onError(msg);
+    @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);
         }
     }
 
-//    @ApiOperation(value = "智能预问诊-多个文件上传同时生成缩略图")
-//    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = { RequestMethod.POST }, origins = "*")
-//    @PostMapping(value = "/uploadImageThums", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
-//    public RespDTO<List<FileThumDTO>> multiFileThumUpload(@RequestParam("upfiles") MultipartFile[] file, @RequestParam("type") Integer[] type) {
-//        List<FileThumDTO> data = uploadFacade.multiFileThumUpload(file, type);
-//        String msg = "";
-//        if (ListUtil.isNotEmpty(data)) {
-//            for (int i = 0; i < data.size(); i++) {
-//                if (data.get(i).getSource() != null && data.get(i).getSource().getState().equals("FAILURE")) {
-//                    msg += "第【" + (i + 1) + "】张图片上传失败," + data.get(i).getSource().getInfo() + ";";
-//                }
-//                if (data.get(i).getThumbnail() != null && data.get(i).getThumbnail().getState().equals("FAILURE")) {
-//                    msg += "第【" + (i + 1) + "】张图片缩略图上传失败," + data.get(i).getThumbnail().getInfo() + ";";
-//                }
-//            }
-//        }
-//        if (StringUtil.isNotBlank(msg)) {
-//            return RespDTO.onError(msg);
-//        } else {
-//            return RespDTO.onSuc(data);
-//        }
-//    }
-
 
-    @ApiOperation(value = "智能预问诊-多个文件上传同时生成缩略图")
+    @ApiOperation(value = "智能预问诊-多个文件上传")
     @CrossOrigin(allowCredentials = "true", allowedHeaders = "*", methods = { RequestMethod.POST }, origins = "*")
     @PostMapping(value = "/uploadImageThums", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
-    public RespDTO<List<FileThumDTO>> multiFileThumUpload(@RequestParam("upfiles") MultipartFile[] file, @RequestParam("type") Integer[] type) {
-        List<FileThumDTO> data = uploadFacade.multiFileThumUpload2(file, type);
+    public RespDTO<List<FileDTO>> multiFileThumUpload(@RequestParam("upfiles") MultipartFile[] file, @RequestParam("type") Integer[] type) {
+        List<FileDTO> data = uploadFacade.multiFileThumUpload(file, type);
         String msg = "";
         if (ListUtil.isNotEmpty(data)) {
             for (int i = 0; i < data.size(); i++) {
-                if (data.get(i).getSource() != null && data.get(i).getSource().getState().equals("FAILURE")) {
-                    msg += "第【" + (i + 1) + "】张图片上传失败," + data.get(i).getSource().getInfo() + ";";
-                }
-                if (data.get(i).getThumbnail() != null && data.get(i).getThumbnail().getState().equals("FAILURE")) {
-                    msg += "第【" + (i + 1) + "】张图片缩略图上传失败," + data.get(i).getThumbnail().getInfo() + ";";
+                if ("FAILURE".equals(data.get(i).getState())) {
+                    msg += "第【" + (i + 1) + "】张图片上传失败," + data.get(i).getInfo() + ";";
                 }
             }
         }
@@ -122,11 +75,4 @@ public class UploadController {
             return RespDTO.onSuc(data);
         }
     }
-
-    @PostMapping("/deleteRemoteFile")
-    @ApiOperation(value = "智能预问诊-文件删除")
-    public String deleteRemoteFile(@RequestParam("path") String path) {
-        String data = FastJsonUtils.getBeanToJson(uploadFacade.deleteRemoteFile(path));
-        return data;
-    }
 }