Browse Source

Base64 上传 阿里云

weiximei 6 years ago
parent
commit
381169e569

+ 1
- 6
CODE/smart-community/property-api/src/main/java/com/community/huiju/controller/ImageController.java View File

55
     public ResponseBean uploadImgBase64AndGetUrl(@ApiParam(value = "uploadFiles" ,required = true) String[] uploadFiles) throws Exception {
55
     public ResponseBean uploadImgBase64AndGetUrl(@ApiParam(value = "uploadFiles" ,required = true) String[] uploadFiles) throws Exception {
56
         ResponseBean responseBean = new ResponseBean();
56
         ResponseBean responseBean = new ResponseBean();
57
         List<String> urls = new ArrayList<String>();
57
         List<String> urls = new ArrayList<String>();
58
-//        for (MultipartFile uploadFile : uploadFiles){
59
-//            String url = imageService.getImageUrl(uploadFile);
60
-//            urls.add(url);
61
-//        }
62
         for (String base64Url : uploadFiles) {
58
         for (String base64Url : uploadFiles) {
63
             BASE64Decoder base64Decoder = new BASE64Decoder();
59
             BASE64Decoder base64Decoder = new BASE64Decoder();
64
             byte[] bytes = base64Decoder.decodeBuffer(base64Url);
60
             byte[] bytes = base64Decoder.decodeBuffer(base64Url);
65
             ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
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
             urls.add(url);
63
             urls.add(url);
69
         }
64
         }
70
         responseBean.addSuccess(urls);
65
         responseBean.addSuccess(urls);

+ 9
- 0
CODE/smart-community/property-api/src/main/java/com/community/huiju/service/ImageServiceI.java View File

3
 import org.springframework.web.multipart.MultipartFile;
3
 import org.springframework.web.multipart.MultipartFile;
4
 
4
 
5
 import java.io.IOException;
5
 import java.io.IOException;
6
+import java.io.InputStream;
6
 
7
 
7
 /**
8
 /**
8
  * @author admin
9
  * @author admin
12
  */
13
  */
13
 public interface ImageServiceI {
14
 public interface ImageServiceI {
14
     String getImageUrl(MultipartFile uploadFile) throws IOException;
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 View File

7
 
7
 
8
 import java.io.ByteArrayInputStream;
8
 import java.io.ByteArrayInputStream;
9
 import java.io.IOException;
9
 import java.io.IOException;
10
+import java.io.InputStream;
10
 import java.util.Date;
11
 import java.util.Date;
11
 
12
 
12
 /**
13
 /**
43
         url = url.substring(0, url.lastIndexOf("&Signature"));
44
         url = url.substring(0, url.lastIndexOf("&Signature"));
44
         return url;
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
 }