From f40a9f9c1b90d3efd52926b29f3612cb1c293afa Mon Sep 17 00:00:00 2001
From: LSH
Date: 星期一, 11 九月 2023 13:25:42 +0800
Subject: [PATCH] #跟踪项目负责人默认值

---
 src/main/java/com/zy/crm/common/service/OssService.java |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/zy/crm/common/service/OssService.java b/src/main/java/com/zy/crm/common/service/OssService.java
index fc3ce79..1472c11 100644
--- a/src/main/java/com/zy/crm/common/service/OssService.java
+++ b/src/main/java/com/zy/crm/common/service/OssService.java
@@ -3,6 +3,8 @@
 import com.aliyun.oss.OSSClient;
 import com.aliyun.oss.model.OSSObject;
 import com.aliyun.oss.model.ObjectMetadata;
+import com.aliyun.oss.model.PutObjectRequest;
+import com.aliyun.oss.model.PutObjectResult;
 import com.core.common.SnowflakeIdWorker;
 import com.core.exception.CoolException;
 import lombok.Data;
@@ -10,12 +12,13 @@
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
+import java.io.*;
 import java.net.URL;
 import java.nio.file.Path;
 import java.util.Date;
@@ -54,6 +57,7 @@
      * 涓婁紶OSS鏂囦欢
      * @return the url
      */
+    @Deprecated
     public String upload(InputStream inputStream, String suffix){
         if(suffix.trim().equals("file")){
             suffix="jpg";
@@ -82,6 +86,8 @@
         }
         throw new CoolException("500-涓婁紶澶辫触");
     }
+
+    @Deprecated
     public String upload(InputStream inputStream, String suffix,String contentType){
         if(suffix.trim().equals("file")){
             suffix="jpg";
@@ -112,8 +118,53 @@
         throw new CoolException("500-涓婁紶澶辫触");
     }
 
+    @Deprecated
     public String upload(InputStream inputStream) {
         return upload(inputStream, ".jpg");
+    }
+
+    /**
+     * 鏂囦欢涓婁紶
+     * @param filePath OSS璺緞
+     * @param file 鏂囦欢File
+     */
+    public boolean uploadFile(String filePath, File file) {
+        OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret);
+        // 鍒涘缓PutObjectRequest瀵硅薄銆�
+        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, filePath, file);
+        // 涓婁紶鏂囦欢銆�
+        PutObjectResult result = ossClient.putObject(putObjectRequest);
+        return true;
+    }
+
+    /**
+     * 涓嬭浇鏂囦欢
+     * @param filePath OSS璺緞
+     */
+    public ResponseEntity<InputStreamResource> downloadFile(String filePath) throws IOException {
+        OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret);
+        OSSObject ossObject = ossClient.getObject(bucket, filePath);
+        InputStream inputStream = ossObject.getObjectContent();
+
+        // 鍒涘缓鍝嶅簲瀹炰綋锛屽皢杈撳嚭娴佷綔涓烘枃浠跺唴瀹硅繑鍥�
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
+        headers.setContentDispositionFormData("attachment", "output.docx");
+        InputStreamResource resource = new InputStreamResource(convertToByteArrayInputStream(inputStream));
+        return ResponseEntity.ok()
+                .headers(headers)
+                .body(resource);
+    }
+
+    public static ByteArrayInputStream convertToByteArrayInputStream(InputStream inputStream) throws IOException {
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        byte[] buffer = new byte[4096];
+        int bytesRead;
+        while ((bytesRead = inputStream.read(buffer)) != -1) {
+            outputStream.write(buffer, 0, bytesRead);
+        }
+        byte[] data = outputStream.toByteArray();
+        return new ByteArrayInputStream(data);
     }
 
     /**
@@ -134,6 +185,7 @@
 //        }
 //    }
 
+    @Deprecated
     public String download(String name) {
         OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret);
         try {

--
Gitblit v1.9.1