Procházet zdrojové kódy

fix 修改文件传输到oss的方式

DYH2020 před 2 roky
rodič
revize
b9535eacdc

+ 40 - 1
src/main/java/com/ywt/mg/domain/models/OssSettring.java

@@ -10,7 +10,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.OutputStream;
 import java.util.Date;
 
 /**
@@ -33,7 +35,7 @@ public class OssSettring {
 
     public static final String fileBucketName = "ywt-files";
 
-    public String uploadFile(String localPath, String saveFileName){
+    public String uploadFile(String localPath, String saveFileName) {
         // 创建OSSClient实例。
         OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
 
@@ -74,4 +76,41 @@ public class OssSettring {
         return "";
     }
 
+    public String uploadFile(String saveFileName, String ossPath, ByteArrayInputStream bio) {
+        // 创建OSSClient实例。
+        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
+        // 接收返回的url
+        String url = "";
+        try {
+            saveFileName = ossPath + "/" + saveFileName;
+            logger.info("uploadFile:saveFileName:{}", fileBucketName);
+            // 创建PutObjectRequest对象。
+            PutObjectRequest putObjectRequest = new PutObjectRequest(fileBucketName, saveFileName, bio);
+            // 上传文件
+            ossClient.putObject(putObjectRequest);
+            // 获取文件url
+            // url过期时间
+            Date date = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7);
+            url = ossClient.generatePresignedUrl(fileBucketName, saveFileName, date).toString();
+            logger.info("uploadFile:url:{}", url);
+            return saveFileName;
+        } catch (OSSException oe) {
+            logger.info("Caught an OSSException, which means your request made it to OSS, "
+                    + "but was rejected with an error response for some reason.");
+            logger.info("Error Message:" + oe.getErrorMessage());
+            logger.info("Error Code:" + oe.getErrorCode());
+            logger.info("Request ID:" + oe.getRequestId());
+            logger.info("Host ID:" + oe.getHostId());
+        } catch (ClientException ce) {
+            logger.info("Caught an ClientException, which means the client encountered "
+                    + "a serious internal problem while trying to communicate with OSS, "
+                    + "such as not being able to access the network.");
+            logger.info("Error Message:" + ce.getMessage());
+        } finally {
+            if (ossClient != null) {
+                ossClient.shutdown();
+            }
+        }
+        return "";
+    }
 }

+ 7 - 1
src/main/java/com/ywt/mg/web/common/FileSrv.java

@@ -6,6 +6,7 @@ import com.ywt.gapi.user.DoctorInfo;
 import com.ywt.gapi.user.DoctorServiceGrpc;
 import com.ywt.gapi.user.GetDoctorAllRequest;
 import com.ywt.gapi.user.GetDoctorAllResponse;
+import com.ywt.mg.domain.models.OssSettring;
 import com.ywt.mg.web.DataResponse;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -33,6 +34,9 @@ public class FileSrv {
     @Autowired
     private FileServiceGrpc.FileServiceBlockingStub fileServiceBlockingStub;
 
+    @Autowired
+    private OssSettring ossSettring;
+
     public void deleteFile(String fileName) {
         try {
             //创建一个文件对象
@@ -70,7 +74,9 @@ public class FileSrv {
 //            byte[] bytes  =  ByteString.readFrom(bio);
 //            byte[] bytes = Files.readAllBytes(file.toPath());
 //            String fileUrl = uploadFile(filename, ossPath, bytes);
-            String fileUrl = uploadFile(filename, ossPath, bio);
+            // 因为文件太大,grpc没找到node设置传输大小的参数,目前采用java上传oss
+//            String fileUrl = uploadFile(filename, ossPath, bio);
+            String fileUrl = ossSettring.uploadFile(filename, ossPath, bio);
             response.setData(fileUrl);
             response.succeed();
         } catch (Exception e) {

+ 1 - 1
src/main/resources/application.properties

@@ -1,7 +1,7 @@
 server.port=7678
 
 #前缀
-server.contextPath=/downloadService
+#server.contextPath=/downloadService
 
 spring.jpa.hibernate.ddl-auto=none