Forráskód Böngészése

修改华泰财险影像上传

wks 1 éve
szülő
commit
2f0721b029

+ 63 - 16
src/main/java/com/ydtech/modules/order/components/huatai/HuiTaiUploadImageComponents.java

@@ -10,19 +10,19 @@ import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.ByteArrayResource;
 import org.springframework.http.*;
 import org.springframework.stereotype.Component;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
 
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
+import java.io.*;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.security.MessageDigest;
 import java.util.*;
 
 
@@ -85,7 +85,8 @@ public class HuiTaiUploadImageComponents {
                         HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO1 =
                                 HuaTaiImageInitParamResponse.filteringTypeCode(childrenDTO, fileTypeEnum.getCode());
                         FileuploadResult fileupload = fileupload(domain, token, childrenDTO1, insUploadImagesDto.getFilePath(), file, initParamResponse.getStorageType());
-                        saveFileIdx(domain, token, initParamResponse, fileupload, fileName, childrenDTO1);
+                        String imgId = insUploadImagesDto.getId();
+                        saveFileIdx(domain, token, initParamResponse, fileupload, fileName, childrenDTO1, imgId);
                         map.put(insUploadImagesDto.getImageType(), insUploadImagesDto.getImageId());
                     } catch (Exception e) {
                         insImagesUploadCacheComponents.setUploadImageCache(companyId, map);
@@ -126,29 +127,47 @@ public class HuiTaiUploadImageComponents {
      * @return 文件路劲
      */
     private FileuploadResult fileupload(String domain, String token, HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO, String path,
-                                        String fileName, String storageType) {
+                                        String fileName, String storageType) throws Exception {
         File file = new File(path);
+        String md5Hash = getMD5Hash(path);
         byte[] bytes = file2byte(file);
         double v = (double) bytes.length / 1024;
-        HttpHeaders httpHeaders = new HttpHeaders();
-        httpHeaders.setAccept(Collections.singletonList(MediaType.MULTIPART_FORM_DATA));
-        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
-
+        String uuidImagesName = UUID.nameUUIDFromBytes(bytes).toString();
+        // 请求参数
         MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
-        body.set("file", new FileSystemResource(file));
+        ByteArrayResource byteArrayResource = new ByteArrayResource(bytes) {
+            @Override
+            public String getFilename() {
+                return fileName;
+            }
+        };
+        HttpHeaders httpHeadersFile = new HttpHeaders();
+        httpHeadersFile.setContentType(MediaType.IMAGE_JPEG);
+        body.set("file", new HttpEntity<>(byteArrayResource, httpHeadersFile));
         body.set("token", token);
         body.set("bussModule", childrenDTO.getBussType());
         body.set("bussNo", childrenDTO.getBussNo());
         body.set("fileName", fileName);
-        body.set("saveFileName", fileName);
+        body.set("saveFileName", null);
         body.set("bucket", childrenDTO.getBucket());
         body.set("storageType", storageType);
+
+        // 请求头
+        HttpHeaders httpHeaders = new HttpHeaders();
+        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
+        httpHeaders.set("Bucket", storageType);
+        httpHeaders.set("Bussmodule", childrenDTO.getBussType());
+        httpHeaders.set("Bussno", childrenDTO.getBussNo());
         // token
+        httpHeaders.set("Token", token);
+        // 文件名称
+        httpHeaders.set("Filename", fileName);
         // 文件名称
         HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(body, httpHeaders);
         String fileuploadUrl = domain + FILE_UPLOAD;
         ResponseEntity<String> response = restTemplate.postForEntity(fileuploadUrl, httpEntity, String.class);
-        return new FileuploadResult(response.getBody(), v);
+        String imageUrl = response.getBody().replace("\"", "");
+        return new FileuploadResult(imageUrl, v, uuidImagesName, md5Hash);
     }
 
 
@@ -162,9 +181,11 @@ public class HuiTaiUploadImageComponents {
      */
     private void saveFileIdx(String domain, String token, HuaTaiImageInitParamResponse response, FileuploadResult fileupload,
                              String fileName,
-                             HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO) {
+                             HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO,
+                             String imgId) {
+
         HuaTaiSaveFileIdxRequest huaTaiSaveFileIdxRequest = new HuaTaiSaveFileIdxRequest(token, response, fileupload, fileName,
-                childrenDTO);
+                childrenDTO, imgId);
         huaTaiSaveFileIdxRequest.getFileIdxVo().setImgId(System.currentTimeMillis());
         HttpHeaders httpHeaders = new HttpHeaders();
         httpHeaders.setContentType(MediaType.APPLICATION_JSON);
@@ -182,6 +203,9 @@ public class HuiTaiUploadImageComponents {
 
         private double fileSize;
 
+        private String uuidImagesName;
+
+        private String md5Hash;
     }
 
 
@@ -224,6 +248,29 @@ public class HuiTaiUploadImageComponents {
         }
     }
 
+    public static String getMD5Hash(String imageFilePath) throws Exception {
+        InputStream fis = Files.newInputStream(Paths.get(imageFilePath));
+        byte[] buffer = new byte[1024];
+        MessageDigest complete = MessageDigest.getInstance("MD5");
+        int numRead;
+
+        do {
+            numRead = fis.read(buffer);
+            if (numRead > 0) {
+                complete.update(buffer, 0, numRead);
+            }
+        } while (numRead != -1);
+
+        fis.close();
+        byte[] b = complete.digest();
+
+        StringBuilder result = new StringBuilder();
+        for (int i = 0; i < b.length; i++) {
+            result.append(Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1));
+        }
+        return result.toString();
+    }
+
 }
 
 

+ 16 - 14
src/main/java/com/ydtech/modules/order/entity/api/huatai/request/HuaTaiSaveFileIdxRequest.java

@@ -3,9 +3,14 @@ package com.ydtech.modules.order.entity.api.huatai.request;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.ydtech.modules.order.components.huatai.HuiTaiUploadImageComponents;
 import com.ydtech.modules.order.entity.api.huatai.response.HuaTaiImageInitParamResponse;
+import com.ydtech.utils.SnowFlakeUtil;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.security.MessageDigest;
+
 @NoArgsConstructor
 @Data
 public class HuaTaiSaveFileIdxRequest {
@@ -18,9 +23,9 @@ public class HuaTaiSaveFileIdxRequest {
     private Integer validFlag;
 
     public HuaTaiSaveFileIdxRequest(String token, HuaTaiImageInitParamResponse response, HuiTaiUploadImageComponents.FileuploadResult fileupload, String fileName,
-                                    HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO) {
+                                    HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO, String imgId) {
         this.token = token;
-        this.fileIdxVo = new FileIdxVoDTO(response, fileupload, fileName, childrenDTO);
+        this.fileIdxVo = new FileIdxVoDTO(response, fileupload, fileName, childrenDTO, imgId);
         this.validFlag = 1;
     }
 
@@ -87,12 +92,6 @@ public class HuaTaiSaveFileIdxRequest {
         private String saveType;
         @JsonProperty("fileHashCode")
         private String fileHashCode;
-        @JsonProperty("shootModel")
-        private String shootModel;
-        @JsonProperty("shootTime")
-        private String shootTime;
-        @JsonProperty("Orientation")
-        private Integer orientation;
         @JsonProperty("typePath")
         private String typePath;
         @JsonProperty("typePathName")
@@ -117,7 +116,7 @@ public class HuaTaiSaveFileIdxRequest {
         private String storageType;
 
         public FileIdxVoDTO(HuaTaiImageInitParamResponse response, HuiTaiUploadImageComponents.FileuploadResult fileupload, String fileName,
-                            HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO) {
+                            HuaTaiImageInitParamResponse.TypeTreeNodeVoDTO.ChildrenDTO childrenDTO, String imgId) {
             this.rotate = 0;
             this.rotateStart = 0;
             this.optionFlag = 2;
@@ -145,25 +144,28 @@ public class HuaTaiSaveFileIdxRequest {
             this.fileSize = fileupload.getFileSize();
             this.fileSizeFormat = String.format("%.2f", fileupload.getFileSize()) + "KB";
 
-            String uuidLower = HuiTaiUploadImageComponents.getUUIDLower();
+            String uuidLower = fileupload.getUuidImagesName();
             String substring = fileName.substring(fileName.lastIndexOf("."));
-
-            this.fileTitle = uuidLower;
+            this.fileTitle = fileupload.getUuidImagesName();
             this.fileOrgName = uuidLower + substring;
-
+            this.imgId = SnowFlakeUtil.getSnowFlakeId();
+            this.fileHashCode = fileupload.getMd5Hash();
             this.fileName = fileupload.getFilePath().substring(fileupload.getFilePath().lastIndexOf("/")+1);;
             this.typePath = childrenDTO.getTypePath();
             this.typePathName = childrenDTO.getTypePathName();
             this.typeName = childrenDTO.getLabel();
             this.picMaxSize = childrenDTO.getPicMaxSize();
             this.picQuality = childrenDTO.getPicQuality();
-
             this.error = 0;
             this.uploadStatus = 3;
             this.showIdx = 30;
             this.uploadTime = 154;
             this.storageType = response.getStorageType();
+            this.region = response.getRegion();
+            this.saveType = "U";
+            this.dstKey = "UW_CAR";
         }
 
     }
+
 }