|
@@ -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
|
+}
|