Explorar el Código

加上oss上传设置contentType

wuweihao hace 3 años
padre
commit
301f149f88

+ 24 - 4
720yun_fd_manage/gis_common/src/main/java/com/gis/common/util/AliyunOssUtil.java

@@ -1,10 +1,7 @@
 package com.gis.common.util;
 
 import com.aliyun.oss.OSSClient;
-import com.aliyun.oss.model.OSSObject;
-import com.aliyun.oss.model.OSSObjectSummary;
-import com.aliyun.oss.model.ObjectListing;
-import com.aliyun.oss.model.PutObjectResult;
+import com.aliyun.oss.model.*;
 import com.gis.common.constant.ConfigConstant;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -56,6 +53,29 @@ public class AliyunOssUtil {
         }
     }
 
+    /**
+     *
+     * @param data
+     * @param key
+     * @param contentType : 不设置,默认浏览器打开图片
+     * application/octet-stream 使用cdn后会自动下载
+     * @throws IOException
+     */
+    public void upload(byte[] data, String key, String contentType) throws IOException {
+        OSSClient ossClient = init();
+
+        // 创建上传文件的元信息,可以通过文件元信息设置HTTP header。
+        ObjectMetadata meta = new ObjectMetadata();
+        meta.setContentType(contentType);
+
+        try {
+            // 2019-2-28 启动aliyun oss 空间
+            ossClient.putObject(configConstant.ossBucket, key, new ByteArrayInputStream(data), meta);
+        } catch (Exception e) {
+            log.error(e.toString() + key);
+        }
+    }
+
 
     public void upload(String filePath, String key) {
         OSSClient ossClient = init();

+ 42 - 0
720yun_fd_manage/gis_common/src/main/java/com/gis/common/util/FileUtils.java

@@ -266,6 +266,48 @@ public class FileUtils {
     }
 
 
+    public Object renameUploadOssByeByType(MultipartFile file, String ossBasePath, String ossDomain, String type)  {
+
+        if (file == null) {
+            log.error("文件不能为空");
+            return null;
+        }
+
+        long size = file.getSize();
+        log.info("文件大小:" + size );
+        log.info("文件大小:" + (size/1000) + "kb");
+
+
+        String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
+
+        String fileName = file.getOriginalFilename();
+        log.info("上传的文件名:" + fileName);
+
+        String fileType = getFileType(fileName);
+        String dirType = "image/";
+        if (fileType.equals("doc")) {
+            dirType = "doc/";
+        }
+
+        String suffix = StringUtils.substringAfterLast(fileName, ".");
+        String newName = time  + "."  +suffix;
+
+        // 上传oss
+        String ossPath = ossBasePath + dirType + newName;
+        try {
+            aliyunOssUtil.upload(file.getBytes(), ossPath, type);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        String ossUrl = ossDomain + ossPath;
+        log.info("ossUrl: {}", ossUrl);
+
+        return ossUrl;
+
+    }
+
+
     /**
      * 文件流上传,不存本地服务器,直接上传oss
      * @param file

+ 10 - 0
720yun_fd_manage/gis_web/src/main/java/com/gis/web/controller/TestController.java

@@ -5,6 +5,7 @@ import com.gis.common.constant.CmdConstant;
 import com.gis.common.constant.ConfigConstant;
 import com.gis.common.config.RabbitConfig;
 import com.gis.common.exception.BaseRuntimeException;
+import com.gis.common.util.AliyunOssUtil;
 import com.gis.common.util.CmdUtils;
 import com.gis.common.util.FileUtils;
 import com.gis.common.util.Result;
@@ -63,6 +64,9 @@ public class TestController extends BaseController {
     @Autowired
     TestService testService;
 
+    @Autowired
+    AliyunOssUtil aliyunOssUtil;
+
 
 
 
@@ -206,6 +210,12 @@ public class TestController extends BaseController {
         return Result.success(fileUtils.renameUploadOssBye(file, configConstant.ossBasePath, configConstant.ossDomain));
     }
 
+    @PostMapping("uploadOssByeByType")
+    @ApiOperation(value = "文件上传oss-字节上传设置类型上传")
+    public Result uploadOssByeByType(MultipartFile file){
+        return Result.success(fileUtils.renameUploadOssByeByType(file, configConstant.ossBasePath, configConstant.ossDomain, "application/octet-stream"));
+    }
+
 
     @Test
     public void testReadFileToString(){