Explorar el Código

更新oss上传附件

ccjgmwz hace 4 años
padre
commit
8f6e67a2fb

+ 4 - 3
pom.xml

@@ -185,9 +185,6 @@
                 <env>local</env>
             </properties>
 
-            <activation>
-                <activeByDefault>true</activeByDefault>
-            </activation>
         </profile>
         <profile>
             <id>dev</id>
@@ -206,6 +203,10 @@
             <properties>
                 <env>prod</env>
             </properties>
+
+            <activation>
+                <activeByDefault>true</activeByDefault>
+            </activation>
         </profile>
     </profiles>
     <build>

+ 40 - 21
winsea-haixin-platform-backend/src/main/java/com/yh/saas/platform/controller/AppendixController.java

@@ -3,6 +3,9 @@ package com.yh.saas.platform.controller;
 import ch.qos.logback.classic.Logger;
 import com.alibaba.fastjson.JSONObject;
 import com.alipay.sofa.runtime.api.annotation.SofaReference;
+import com.aliyun.oss.OSSClient;
+import com.aliyun.oss.model.ObjectMetadata;
+import com.aliyun.oss.model.PutObjectRequest;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.mapper.Wrapper;
 import com.baomidou.mybatisplus.toolkit.CollectionUtils;
@@ -14,10 +17,13 @@ import com.winsea.svc.base.base.util.DateUtils;
 import com.winsea.svc.base.base.view.CommonAppendixView;
 import com.winsea.svc.base.security.util.AuthSecurityUtils;
 import com.yh.saas.platform.util.GtAppendixUtil;
+import com.yh.saas.plugin.yiliangyiyun.util.GeneratorUtil;
 import lombok.Getter;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.tomcat.util.http.fileupload.util.Streams;
+import org.apache.velocity.shaded.commons.io.FilenameUtils;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.InitializingBean;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
@@ -44,7 +50,7 @@ import java.util.zip.ZipOutputStream;
  */
 @RestController
 @RequestMapping("/appendix")
-public class AppendixController {
+public class AppendixController implements InitializingBean {
 
     private Logger log = (Logger) LoggerFactory.getLogger(this.getClass());
 
@@ -80,20 +86,27 @@ public class AppendixController {
 
     static final String OSS_PERFIX_URL = "https://{0}.{1}/";
 
+    private String host;
+
+    @Getter
+    @Value("${oss.endpoint.internal:}")
+    private String baseUrl;
+    @Autowired
+    private OSSClient ossClient;
+    @Override
+    public void afterPropertiesSet() throws Exception {
+        host = "http://" + bucketName + "." + endpoint;
+    }
     /**
      * 查询附件
-     *
-     * @param billId
-     * @param detailId
-     * @param appendixType
      * @return
      */
     @GetMapping("/query/getFileList")
     public List<CommonAppendix> getFileList(@RequestParam String appendixIds) {
         List<CommonAppendix> list = appendixService.selectBatchIds(Arrays.asList(appendixIds.split(",")));
-//        if (isOssUsed) {
-//            addPrefix(list);
-//        }
+        if (isOssUsed) {
+            addPrefix(list);
+        }
 
         return list;
     }
@@ -116,7 +129,6 @@ public class AppendixController {
     /**
      * 保存附件
      *
-     * @param ids
      * @return
      */
     @PostMapping("/api/saveFiles")
@@ -131,7 +143,6 @@ public class AppendixController {
     /**
      * 删除附件
      *
-     * @param ids
      * @return
      */
     @PostMapping("/api/deleteFiles")
@@ -233,25 +244,34 @@ public class AppendixController {
             filePathSb.append(DateUtils.formatDate(new Date(), DateUtils.DATE_FMT_DATETIME));
             // 添加后缀
             filePathSb.append(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")));
-            File dest = new File(fileRootPath + MessageFormat.format(filePathSb.toString(), fileName));
-            if (!dest.getParentFile().exists()) { // 判断文件父目录是否存在
-                dest.getParentFile().mkdirs();
-            }
+//            File dest = new File(fileRootPath + MessageFormat.format(filePathSb.toString(), fileName));
+//            if (!dest.getParentFile().exists()) { // 判断文件父目录是否存在
+//                dest.getParentFile().mkdirs();
+//            }
             try {
-                Streams.copy(file.getInputStream(), new FileOutputStream(dest), true);
+//                Streams.copy(file.getInputStream(), new FileOutputStream(dest), true);
+                ObjectMetadata objectMetadata = new ObjectMetadata();
+                objectMetadata.setContentLength(file.getSize());
+                objectMetadata.setContentType(file.getContentType());
+                String ext = FilenameUtils.getExtension(file.getOriginalFilename());
+                String uuid = GeneratorUtil.genFileName();
+                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, "eliangeyun/" + uuid+"."+ext, file.getInputStream(), objectMetadata);
+                ossClient.putObject(putObjectRequest);
                 CommonAppendix appendix = new CommonAppendix();
                 appendix.setAppendixName(
                         fileName + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")));
-                String path = MessageFormat.format(filePathSb.toString(),
-                        URLEncoder.encode(fileName, String.valueOf(StandardCharsets.UTF_8)));
+                String path = baseUrl + "eliangeyun/" + uuid +"."+ext;
                 appendix.setAppendixPath(path.replace("+", "%20"));
                 int size = (int) file.getSize();
                 appendix.setAppendixSize(GtAppendixUtil.getFileSize(size));
+                log.info("文件上传成功 地址:", path);
                 return appendix;
             } catch (Exception e) {
-                if (dest.delete()) {
-                    log.error("文件上传失败", e);
-                }
+                log.error("文件上传失败", e);
+            }
+            finally {
+
+//                dest.delete();
             }
         }
         return null;
@@ -315,7 +335,6 @@ public class AppendixController {
      * 查询要打包的附件
      *
      * @param billIds
-     * @param detailId
      * @return
      */
     private List<CommonAppendix> selectAppendixList(String billIds) {

+ 5 - 5
winsea-haixin-plugin-base/src/main/java/com/winsea/svc/common/config/CommonConfig.java

@@ -94,11 +94,11 @@ public class CommonConfig implements EnvironmentPostProcessor {
      */
     private Map<String, Object> getDefaultOssConfig(String active) {
         Map<String, Object> ossConfig = new HashMap<>();
-        ossConfig.put("oss.access.key.id", "Y8IZ2bDqwsFDLqM0");
-        ossConfig.put("oss.access.key.secret", "GX34icMi6crTpF3IW31hv2CMTnpEO4");
-        ossConfig.put("oss.endpoint.default", "oss-cn-shanghai.aliyuncs.com");
-        ossConfig.put("oss.endpoint.internal", "http://oss-cn-shanghai-internal.aliyuncs.com");
-        ossConfig.put("oss.bucket.name", "prod".equals(active) ? "winsea" : "winsea-product-test");
+        ossConfig.put("oss.access.key.id", "LTAI4G9c14PgKvM23WZ9zrpc");
+        ossConfig.put("oss.access.key.secret", "FpClTp4OVrRRtHEfi3lBOWUoLxKieW");
+        ossConfig.put("oss.endpoint.default", "oss-cn-beijing.aliyuncs.com");
+        ossConfig.put("oss.endpoint.internal", "https://taohaoliang.oss-cn-beijing.aliyuncs.com/");
+        ossConfig.put("oss.bucket.name", "prod".equals(active) ? "eliangeyun" : "eliangeyun-test");
         return ossConfig;
     }
 

+ 26 - 0
winsea-haixin-plugin-yiliangyiyun/src/main/java/com/yh/saas/plugin/yiliangyiyun/config/OSSClientConfig.java

@@ -0,0 +1,26 @@
+package com.yh.saas.plugin.yiliangyiyun.config;
+
+import com.aliyun.oss.OSSClient;
+import lombok.Getter;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class OSSClientConfig {
+    @Getter
+    @Value("${oss.access.key.id:}")
+    private String accessId;
+    @Getter
+    @Value("${oss.access.key.secret:}")
+    private String accessKey;
+    @Getter
+    @Value("${oss.endpoint.default:}")
+    private String endpoint;
+
+    @Bean
+    public OSSClient ossClient() {
+        return new OSSClient(endpoint, accessId, accessKey);
+    }
+
+}