소스 검색

增加用户信息跟进导入到 oss

ruqinhu 5 년 전
부모
커밋
953c014403

+ 6 - 0
pom.xml

@@ -241,6 +241,12 @@
             <artifactId>lombok</artifactId>
             <version>1.18.10</version>
         </dependency>
+
+        <dependency>
+            <groupId>com.aliyun.oss</groupId>
+            <artifactId>aliyun-sdk-oss</artifactId>
+            <version>3.10.2</version>
+        </dependency>
     </dependencies>
 
 

+ 6 - 0
src/main/java/com/ygj/yuemum/component/Constant.java

@@ -65,6 +65,8 @@ public class Constant {
     public static Integer AGE;
     public static Integer MAXSCHOLARSHIPS;
 
+    public static String FOLDER;
+
     //注入
     @Autowired(required = false)
     public void getQrcode(@Value("${file.qrcode}") String QRCODE) {
@@ -304,4 +306,8 @@ public class Constant {
     public void getMAXSCHOLARSHIPS(@Value("${college.MAXSCHOLARSHIPS}") Integer MAXSCHOLARSHIPS) {
         Constant.MAXSCHOLARSHIPS = MAXSCHOLARSHIPS;
     }
+    @Autowired(required = false)
+    public void getMAXSCHOLARSHIPS(@Value("${chuanyun.OssFolder}") String OssFolder) {
+        Constant.FOLDER = OssFolder;
+    }
 }

+ 22 - 1
src/main/java/com/ygj/yuemum/component/CyScheduled.java

@@ -9,6 +9,7 @@ import com.ygj.yuemum.service.customer.CustomerInfoBasicService;
 import com.ygj.yuemum.service.customer.CustomerInfoFeedbackService;
 import com.ygj.yuemum.service.customer.CustomerInfoFollowService;
 import com.ygj.yuemum.service.scheduled.CYScheduledService;
+import com.ygj.yuemum.utils.OssService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
@@ -225,7 +226,7 @@ public class CyScheduled {
             customerInfoFollowCyDto.setFollow_state(jsonObject.getString("F0000007"));
             customerInfoFollowCyDto.setAddress(jsonObject.getString("F0000008"));
             customerInfoFollowCyDto.setFollow_desc(jsonObject.getString("F0000009"));
-            customerInfoFollowCyDto.setPhotos("");
+            customerInfoFollowCyDto.setPhotos(getPhotoUrl(jsonObject.getJSONArray("F0000010")));
             customerInfoFollowCyDto.setNext_follow_date(jsonObject.getString("F0000011"));
             customerInfoFollowCyDto.setCreate_person(jsonObject.getString("CreatedBy"));
             customerInfoFollowCyDto.setFb_date(jsonObject.getString("F0000023"));
@@ -234,4 +235,24 @@ public class CyScheduled {
         return list;
     }
 
+    private String getPhotoUrl(JSONArray attachmentIdJsonArray) {
+        if (attachmentIdJsonArray != null) {
+            List<String> attachmentIds = attachmentIdJsonArray.toJavaList(String.class);
+            if (attachmentIds.size() > 0) {
+                StringBuilder photos = new StringBuilder();
+                for (String attachmentId:attachmentIds) {
+                    try {
+                        OssService.UploadDto uploadDto = cyScheduledService.getDownloadBizObject(attachmentId);
+                        photos.append(uploadDto.getUrl()).append(",");
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                }
+                photos.delete(photos.length()-1, photos.length());
+                return photos.toString();
+            }
+        }
+        return "";
+    }
+
 }

+ 9 - 0
src/main/java/com/ygj/yuemum/controller/admin/ImageUploadController.java

@@ -76,6 +76,15 @@ public class ImageUploadController {
         Date date = new Date();
         DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
         File dest = new File(path + "/" + getpathtype(type) + id + type + sdf.format(date) + newName);
+        if (!dest.exists()) {
+            dest.getParentFile().mkdir();
+            try {
+                dest.mkdir();
+                dest.createNewFile();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
 
         try {
             file.transferTo(dest); //保存文件

+ 25 - 2
src/main/java/com/ygj/yuemum/controller/customer/CustomerInfoFollowController.java

@@ -1,14 +1,22 @@
 package com.ygj.yuemum.controller.customer;
 
+import com.ygj.yuemum.component.Constant;
+import com.ygj.yuemum.domain.admin.Photo;
 import com.ygj.yuemum.domain.customer.CustomerInfoFollow;
 import com.ygj.yuemum.domain.customer.dto.CustomerInfoFollowDto;
 import com.ygj.yuemum.service.customer.CustomerInfoFollowService;
+import com.ygj.yuemum.utils.OssService;
 import com.ygj.yuemum.utils.ResponseUtil;
 import io.swagger.annotations.Api;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import java.io.File;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
@@ -25,6 +33,9 @@ public class CustomerInfoFollowController {
     @Resource
     private CustomerInfoFollowService customerInfoFollowService;
 
+    @Resource
+    private OssService ossService;
+
     /**
     * [新增]
     * @author zrz
@@ -76,4 +87,16 @@ public class CustomerInfoFollowController {
         return ResponseUtil.convertRetMap(list);
     }
 
+    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
+    @ResponseBody
+    public OssService.UploadDto uploadImg(@RequestParam("file") MultipartFile file) {
+        try {
+            OssService.UploadDto uploadDto = ossService.upload(file, Constant.FOLDER);
+            return uploadDto;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
 }

+ 30 - 0
src/main/java/com/ygj/yuemum/service/scheduled/CYScheduledService.java

@@ -2,15 +2,19 @@ package com.ygj.yuemum.service.scheduled;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.ygj.yuemum.component.Constant;
 import com.ygj.yuemum.utils.HttpUtils;
+import com.ygj.yuemum.utils.OssService;
 import org.apache.http.HttpResponse;
 import org.apache.http.util.EntityUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.UUID;
 
 @Service
 public class CYScheduledService {
@@ -21,6 +25,9 @@ public class CYScheduledService {
     @Value("${chuanyun.EngineSecret}")
     String EngineSecret;
 
+    @Autowired
+    OssService ossService;
+
     public JSONObject listFormData(String SchemaCode, int FromRowNum, int ToRowNum) throws Exception {
         return this.listFormData(EngineCode, EngineSecret, SchemaCode, FromRowNum, ToRowNum);
     }
@@ -68,4 +75,27 @@ public class CYScheduledService {
         return jsonObject;
     }
 
+    public OssService.UploadDto getDownloadBizObject(String attachmentId) throws Exception {
+        Map<String, String> paramMap = new HashMap();
+
+        paramMap.put("attachmentId", attachmentId);
+
+        paramMap.put("EngineCode", "ae483ce1607kh089");
+
+        //身份认证参数
+        Map<String, String> headers = new HashMap();
+
+        headers.put("EngineCode","ae483ce1607kh089");
+
+        headers.put("EngineSecret","aDKyV5OYQa7HMv4qWvqH8bAlXxmzD3bQK2/klBFoHlu6XHRxh7Q2lg==");
+
+        HttpResponse result = HttpUtils.doPost("https://www.h3yun.com", "/Api/DownloadBizObjectFile", headers, paramMap, "");
+
+        String fileName = UUID.randomUUID().toString() + ".jpg";
+
+        OssService.UploadDto uploadDto = ossService.upload(result.getEntity().getContent(), Constant.FOLDER, fileName, result.getEntity().getContentLength());
+
+        return uploadDto;
+    }
+
 }

+ 1 - 0
src/main/java/com/ygj/yuemum/shiro/ShiroConfig.java

@@ -145,6 +145,7 @@ public class ShiroConfig {
         filterChainDefinitionMap.put("/uploadPayImg", "anon");
         filterChainDefinitionMap.put("/uploadMiniImg", "anon");
         filterChainDefinitionMap.put("/uploadDianPingImg", "anon");
+        filterChainDefinitionMap.put("/CustomerInfoFollow/uploadImg", "anon");
         //简历分享问题
         filterChainDefinitionMap.put("/getWeChatInfo", "anon");
         //超人妈妈学院

+ 4 - 2
src/main/java/com/ygj/yuemum/utils/HttpUtils.java

@@ -198,8 +198,10 @@ public class HttpUtils {
 			throws Exception {
 		HttpClient httpClient = wrapClient(host, path);
 		HttpPost request = new HttpPost(buildUrl(host, path, querys));
-		for (Map.Entry<String, String> e : headers.entrySet()) {
-			request.addHeader(e.getKey(), e.getValue());
+		if (headers != null) {
+			for (Map.Entry<String, String> e : headers.entrySet()) {
+				request.addHeader(e.getKey(), e.getValue());
+			}
 		}
 		if (StringUtils.isNotBlank(body)) {
 			request.setEntity(new StringEntity(body, "utf-8"));

+ 125 - 0
src/main/java/com/ygj/yuemum/utils/OssService.java

@@ -0,0 +1,125 @@
+package com.ygj.yuemum.utils;
+
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClient;
+import com.aliyun.oss.model.ObjectMetadata;
+import lombok.Data;
+import lombok.experimental.Accessors;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.UUID;
+
+@Service
+public class OssService {
+    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
+    String accessKeyId = "02yUhyzfxsxzLCKA";
+    String accessKeySecret = "6Cw3uKQKKRedocMtfZVfCxHoeZh2h6";
+    String bucket = "yueguanjia-bucket";
+    String rootserver = "oss-cn-hangzhou.aliyuncs.com";
+
+
+    public UploadDto upload(File file, String folder) throws Exception {
+        OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
+        ObjectMetadata objectMetadata = new ObjectMetadata();
+        objectMetadata.setContentLength(file.length());
+        FileInputStream inputStream = new FileInputStream(file);
+
+        String extension = getExtension(file.getName());
+        String saveName = UUID.randomUUID().toString() + "." + extension;
+
+        String key = folder + "/" + saveName;
+        ossClient.putObject(bucket, key, inputStream, objectMetadata);
+        ossClient.shutdown();
+
+        UploadDto uploadDto = new UploadDto();
+        uploadDto.url = getUrl(key);
+        uploadDto.name = file.getName();
+        uploadDto.saveName = saveName;
+        uploadDto.size = file.length();
+        uploadDto.key = key;
+        return uploadDto;
+    }
+
+    public UploadDto upload(MultipartFile file, String folder) throws Exception {
+        OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
+        ObjectMetadata objectMetadata = new ObjectMetadata();
+        objectMetadata.setContentLength(file.getSize());
+
+
+        String extension = getExtension(file.getOriginalFilename());
+        String saveName = UUID.randomUUID().toString() + "." + extension;
+
+        String key = folder + "/" + saveName;
+        ossClient.putObject(bucket, key, file.getInputStream(), objectMetadata);
+        ossClient.shutdown();
+
+        UploadDto uploadDto = new UploadDto();
+        uploadDto.url = getUrl(key);
+        uploadDto.name = file.getOriginalFilename();
+        uploadDto.saveName = saveName;
+        uploadDto.size = file.getSize();
+        uploadDto.key = key;
+        return uploadDto;
+    }
+
+    public UploadDto upload(InputStream inputStream, String folder, String fileName, long lenth) throws UnsupportedEncodingException {
+        OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
+
+        String extension = getExtension(fileName);
+        String saveName = UUID.randomUUID().toString() + "." + extension;
+
+        String key = folder + "/" + saveName;
+        ossClient.putObject(bucket, key, inputStream);
+        ossClient.shutdown();
+
+        UploadDto uploadDto = new UploadDto();
+        uploadDto.url = getUrl(key);
+        uploadDto.name = fileName;
+        uploadDto.saveName = saveName;
+        uploadDto.size = lenth;
+        uploadDto.key = key;
+        return uploadDto;
+    }
+
+    private String getExtension(String fileName) {
+        int index = fileName.lastIndexOf(".");
+        if (index < 0) {
+            return "";
+        }
+        return fileName.substring(index + 1);
+    }
+
+
+    public String getUrl(String key) throws UnsupportedEncodingException {
+        return "https://" + bucket + "." + rootserver + "/" + URLEncoder.encode(key, "UTF-8");
+    }
+
+    @Data
+    @Accessors(chain = true)
+    public static class UploadDto {
+        /**
+         * oss存储地址
+         */
+        private String url;
+        /**
+         * osskey
+         */
+        private String key;
+        private String name;
+        /**
+         * 保存oss文件名称
+         */
+        private String saveName;
+        /**
+         * 文件大小
+         */
+        private Long size;
+    }
+
+}

+ 1 - 0
src/main/resources/application.yml

@@ -159,6 +159,7 @@ swagger:
 chuanyun:
   EngineCode: ae483ce1607kh089
   EngineSecret: aDKyV5OYQa7HMv4qWvqH8bAlXxmzD3bQK2/klBFoHlu6XHRxh7Q2lg==
+  OssFolder: yeusuo/follow
 
 
 

+ 2 - 2
src/test/java/com/ygj/yuemum/chuanyun/CyScheduledTest.java

@@ -33,8 +33,8 @@ public class CyScheduledTest {
 
     @Test
     public void testInitCustomerInfoFollow() {
-        for (int i = 0; i < 15; i++) {
-            cyScheduled.initCustomerInfoFollow(i * base, (i + 1) * base);
+        for (int i = 0; i < 1; i++) {
+            cyScheduled.initCustomerInfoFollow(i , (i + 1));
         }
     }
 

+ 50 - 0
src/test/java/com/ygj/yuemum/chuanyun/ScheduledSingleServiceTest.java

@@ -3,6 +3,7 @@ package com.ygj.yuemum.chuanyun;
 import com.alibaba.fastjson.JSONObject;
 import com.google.gson.Gson;
 import com.ygj.yuemum.utils.HttpUtils;
+import com.ygj.yuemum.utils.OssService;
 import org.apache.http.HttpResponse;
 import org.apache.http.util.EntityUtils;
 import org.junit.Test;
@@ -75,4 +76,53 @@ public class ScheduledSingleServiceTest {
         System.out.println(strResult);
     }
 
+    @Test
+    public void testPhotoObject() throws Exception {
+        OssService ossService = new OssService();
+
+        System.out.println("开始时间戳:  " + System.currentTimeMillis());
+        Map<String, String> paramMap = new HashMap();
+
+        paramMap.put("attachmentId",   "25274e9f-6a2b-4d1e-a181-af9d1d47fb87");
+
+        paramMap.put("EngineCode", "ae483ce1607kh089");
+
+        //身份认证参数
+        Map headers = new HashMap();
+
+        headers.put("EngineCode","ae483ce1607kh089");
+
+        headers.put("EngineSecret","aDKyV5OYQa7HMv4qWvqH8bAlXxmzD3bQK2/klBFoHlu6XHRxh7Q2lg==");
+
+        Gson gson = new Gson();
+
+        HttpResponse result = HttpUtils.doPost("https://www.h3yun.com", "/Api/DownloadBizObjectFile", headers, paramMap, "");
+
+//        String strResult = EntityUtils.toString();
+        OssService.UploadDto uploadDto = ossService.upload(result.getEntity().getContent(), "test", "1.jpg", result.getEntity().getContentLength());
+
+        System.out.println(uploadDto.toString());
+
+        System.out.println("结束时间戳:  " + System.currentTimeMillis());
+
+
+//        File dest = new File("D:\\" ,  "1.jpg");
+
+//        InputStream is = result.getEntity().getContent();
+//
+//        dest.getParentFile().mkdirs();
+//        FileOutputStream fileout = new FileOutputStream(dest);
+//        /**
+//         * 根据实际运行效果 设置缓冲区大小
+//         */
+//        byte[] buffer=new byte[1024];
+//        int ch = 0;
+//        while ((ch = is.read(buffer)) != -1) {
+//            fileout.write(buffer,0,ch);
+//        }
+//        is.close();
+//        fileout.flush();
+//        fileout.close();
+    }
+
 }