Преглед на файлове

Base64 上传 阿里云

weiximei преди 6 години
родител
ревизия
381169e569

+ 1
- 6
CODE/smart-community/property-api/src/main/java/com/community/huiju/controller/ImageController.java Целия файл

@@ -55,16 +55,11 @@ public class ImageController extends BaseController {
55 55
     public ResponseBean uploadImgBase64AndGetUrl(@ApiParam(value = "uploadFiles" ,required = true) String[] uploadFiles) throws Exception {
56 56
         ResponseBean responseBean = new ResponseBean();
57 57
         List<String> urls = new ArrayList<String>();
58
-//        for (MultipartFile uploadFile : uploadFiles){
59
-//            String url = imageService.getImageUrl(uploadFile);
60
-//            urls.add(url);
61
-//        }
62 58
         for (String base64Url : uploadFiles) {
63 59
             BASE64Decoder base64Decoder = new BASE64Decoder();
64 60
             byte[] bytes = base64Decoder.decodeBuffer(base64Url);
65 61
             ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
66
-            MultipartFile multipartFil = new MockMultipartFile(String.valueOf(System.currentTimeMillis()), byteArrayInputStream);
67
-            String url = imageService.getImageUrl(multipartFil);
62
+            String url = imageService.getImageUrl(byteArrayInputStream);
68 63
             urls.add(url);
69 64
         }
70 65
         responseBean.addSuccess(urls);

+ 9
- 0
CODE/smart-community/property-api/src/main/java/com/community/huiju/service/ImageServiceI.java Целия файл

@@ -3,6 +3,7 @@ package com.community.huiju.service;
3 3
 import org.springframework.web.multipart.MultipartFile;
4 4
 
5 5
 import java.io.IOException;
6
+import java.io.InputStream;
6 7
 
7 8
 /**
8 9
  * @author admin
@@ -12,4 +13,12 @@ import java.io.IOException;
12 13
  */
13 14
 public interface ImageServiceI {
14 15
     String getImageUrl(MultipartFile uploadFile) throws IOException;
16
+
17
+    /**
18
+     * 二进制方式 上传 阿里云
19
+     * @param uploadFile
20
+     * @return
21
+     * @throws IOException
22
+     */
23
+    String getImageUrl(InputStream uploadFile) throws IOException;
15 24
 }

+ 24
- 0
CODE/smart-community/property-api/src/main/java/com/community/huiju/service/impl/ImageServiceimpl.java Целия файл

@@ -7,6 +7,7 @@ import org.springframework.web.multipart.MultipartFile;
7 7
 
8 8
 import java.io.ByteArrayInputStream;
9 9
 import java.io.IOException;
10
+import java.io.InputStream;
10 11
 import java.util.Date;
11 12
 
12 13
 /**
@@ -43,4 +44,27 @@ public class ImageServiceimpl implements ImageServiceI {
43 44
         url = url.substring(0, url.lastIndexOf("&Signature"));
44 45
         return url;
45 46
     }
47
+
48
+    @Override
49
+    public String getImageUrl(InputStream uploadFile) throws IOException {
50
+
51
+        String imgName = System.currentTimeMillis() + ".png";
52
+        // 创建OSSClient实例
53
+        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
54
+        // 上传
55
+        long time = System.currentTimeMillis();
56
+
57
+        ossClient.putObject(bucketName, imgName, uploadFile);
58
+
59
+        // 关闭client
60
+        ossClient.shutdown();
61
+        Date expiration = new Date(time + 3600 * 1000 * 24 * 365 * 10);
62
+        String url = ossClient.generatePresignedUrl(bucketName, imgName, expiration).toString();
63
+
64
+        // 截取掉签名
65
+        url = url.substring(0, url.lastIndexOf("&Signature"));
66
+        return url;
67
+
68
+        return null;
69
+    }
46 70
 }