weiximei 6 лет назад
Родитель
Сommit
70374b84ad

+ 5
- 0
whole-estate/pom.xml Просмотреть файл

@@ -48,6 +48,11 @@
48 48
             <!--<version>2.1.3.RELEASE</version>-->
49 49
         <!--</dependency>-->
50 50
 
51
+        <dependency>
52
+            <groupId>com.aliyun.oss</groupId>
53
+            <artifactId>aliyun-sdk-oss</artifactId>
54
+            <version>2.8.3</version>
55
+        </dependency>
51 56
 
52 57
         <!-- 测试 -->
53 58
         <dependency>

+ 75
- 0
whole-estate/src/main/java/com/example/wholeestate/controller/ImageController.java Просмотреть файл

@@ -0,0 +1,75 @@
1
+package com.example.wholeestate.controller;
2
+
3
+import com.example.wholeestate.common.base.BaseController;
4
+import com.example.wholeestate.common.resp.ResponseBean;
5
+import com.example.wholeestate.service.ImageService;
6
+import io.swagger.annotations.Api;
7
+import io.swagger.annotations.ApiOperation;
8
+import io.swagger.annotations.ApiParam;
9
+import org.springframework.beans.factory.annotation.Autowired;
10
+import org.springframework.web.bind.annotation.PostMapping;
11
+import org.springframework.web.bind.annotation.RequestMapping;
12
+import org.springframework.web.bind.annotation.RestController;
13
+import org.springframework.web.multipart.MultipartFile;
14
+import sun.misc.BASE64Decoder;
15
+
16
+import javax.sql.rowset.serial.SerialBlob;
17
+import java.util.ArrayList;
18
+import java.util.List;
19
+
20
+/**
21
+ * @author weichaochao
22
+ * @Title: ImageController
23
+ * @Description: 图片上传以及获取url
24
+ * @date 2018/10/31
25
+ */
26
+@RestController
27
+@RequestMapping("/")
28
+@Api(value = "图片操作API", description = "图片操作API")
29
+public class ImageController extends BaseController {
30
+
31
+    @Autowired
32
+    private ImageService imageService;
33
+
34
+    @ApiOperation(value = "图片上传以及获取url", notes = "图片上传以及获取url")
35
+    @PostMapping(value = "/uploadimage", consumes = "multipart/*", headers = "content-type=multipart/form-data")
36
+    public ResponseBean uploadImgAndGetUrl(@ApiParam(value = "uploadFiles" ,required = true) MultipartFile[] uploadFiles) throws Exception {
37
+        ResponseBean responseBean = new ResponseBean();
38
+        List<String> urls = new ArrayList<String>();
39
+        for (MultipartFile uploadFile : uploadFiles){
40
+            String url = imageService.getImageUrl(uploadFile);
41
+            urls.add(url);
42
+        }
43
+        responseBean.addSuccess(urls);
44
+        return responseBean;
45
+    }
46
+
47
+    @ApiOperation(value = "图片上传以及获取url", notes = "图片上传以及获取url")
48
+    @PostMapping(value = "/uploadimageBase64")
49
+    public ResponseBean uploadImgBase64AndGetUrl(@ApiParam(value = "uploadFiles" ,required = true) String uploadFiles) throws Exception {
50
+        ResponseBean responseBean = new ResponseBean();
51
+        List<String> urls = new ArrayList<String>();
52
+        // int index = uploadFiles.indexOf("data:image/jpeg;base64,");
53
+        //uploadFiles = uploadFiles.substring(23);
54
+        String[] split = uploadFiles.split("base64,");
55
+        SerialBlob serialBlob = null;
56
+        if (split.length > 1) {
57
+            serialBlob = decodeToImage(split[1]);
58
+        } else {
59
+            serialBlob = decodeToImage(split[0]);
60
+        }
61
+
62
+        String url = imageService.getImageUrl(serialBlob.getBinaryStream());
63
+        urls.add(url);
64
+        responseBean.addSuccess(urls);
65
+        return responseBean;
66
+    }
67
+
68
+    public static SerialBlob decodeToImage(String imageString) throws Exception {
69
+        BASE64Decoder decoder = new BASE64Decoder();
70
+        byte[] imageByte = decoder.decodeBuffer(imageString);
71
+        return new SerialBlob(imageByte);
72
+    }
73
+
74
+
75
+}

+ 24
- 0
whole-estate/src/main/java/com/example/wholeestate/service/ImageService.java Просмотреть файл

@@ -0,0 +1,24 @@
1
+package com.example.wholeestate.service;
2
+
3
+import org.springframework.web.multipart.MultipartFile;
4
+
5
+import java.io.IOException;
6
+import java.io.InputStream;
7
+
8
+/**
9
+ * @author admin
10
+ * @Title: ImageService
11
+ * @Description: TODO
12
+ * @date 2018/11/5
13
+ */
14
+public interface ImageService {
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;
24
+}

+ 68
- 0
whole-estate/src/main/java/com/example/wholeestate/service/impl/ImageServiceimpl.java Просмотреть файл

@@ -0,0 +1,68 @@
1
+package com.example.wholeestate.service.impl;
2
+
3
+import com.aliyun.oss.OSSClient;
4
+import com.example.wholeestate.service.ImageService;
5
+import org.springframework.stereotype.Service;
6
+import org.springframework.web.multipart.MultipartFile;
7
+
8
+import java.io.ByteArrayInputStream;
9
+import java.io.IOException;
10
+import java.io.InputStream;
11
+import java.util.Date;
12
+
13
+/**
14
+ * @author weichaochao
15
+ * @Title: ImageServiceimpl
16
+ * @Description: 获取图片地址实现类
17
+ * @date 2018/11/5
18
+ */
19
+@Service("ImageService")
20
+public class ImageServiceimpl implements ImageService {
21
+
22
+    private static String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
23
+    private static String accessKeyId = "LTAIkc75dpkJw8Lb";
24
+    private static String accessKeySecret = "v4bvXCaix6vSDTCFfwSAdqV53iFEQw";
25
+    private static String bucketName = "jingcheng-h5temp";
26
+
27
+    @Override
28
+    public String getImageUrl(MultipartFile uploadFile) throws IOException {
29
+
30
+        String imgName = System.currentTimeMillis() + ".png";
31
+        // 创建OSSClient实例
32
+        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
33
+        // 上传
34
+        long time = System.currentTimeMillis();
35
+
36
+        ossClient.putObject(bucketName, imgName, new ByteArrayInputStream(uploadFile.getBytes()));
37
+
38
+        // 关闭client
39
+        ossClient.shutdown();
40
+        Date expiration = new Date(time + 3600 * 1000 * 24 * 365 * 10);
41
+        String url = ossClient.generatePresignedUrl(bucketName, imgName, expiration).toString();
42
+
43
+        // 截取掉签名
44
+        url = url.substring(0, url.lastIndexOf("&Signature"));
45
+        return url;
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
+}