Bläddra i källkod

Merge branch 'release/V1.0.0/test' of http://192.168.4.240:3000/LINK/LINK-SERVER into release/V2.0.0/test

 Conflicts:
	src/main/java/com/lqkj/link/module/zone/service/ZoneInfoService.java
liaoyitao 1 månad sedan
förälder
incheckning
2b9f722d1c

+ 22 - 0
pom.xml

@@ -164,6 +164,28 @@
164 164
             <version>5.8.25</version>
165 165
         </dependency>
166 166
 
167
+<!--        阿里云oss依赖-->
168
+        <dependency>
169
+            <groupId>com.aliyun.oss</groupId>
170
+            <artifactId>aliyun-sdk-oss</artifactId>
171
+            <version>3.17.4</version>
172
+        </dependency>
173
+        <dependency>
174
+            <groupId>javax.xml.bind</groupId>
175
+            <artifactId>jaxb-api</artifactId>
176
+            <version>2.3.1</version>
177
+        </dependency>
178
+        <dependency>
179
+            <groupId>javax.activation</groupId>
180
+            <artifactId>activation</artifactId>
181
+            <version>1.1.1</version>
182
+        </dependency>
183
+        <!-- no more than 2.3.3-->
184
+        <dependency>
185
+            <groupId>org.glassfish.jaxb</groupId>
186
+            <artifactId>jaxb-runtime</artifactId>
187
+            <version>2.3.3</version>
188
+        </dependency>
167 189
     </dependencies>
168 190
 
169 191
     <build>

+ 2 - 1
src/main/java/com/lqkj/link/config/OpenApiConfig.java

@@ -106,7 +106,8 @@ public class OpenApiConfig {
106 106
                         "/audit/v1/**",
107 107
                         "/notice/v1/**",
108 108
                         "/layer/v1/**",
109
-                        "/resource/v1/**")
109
+                        "/resource/v1/**",
110
+                        "/resource/ossUpload")
110 111
                 .build();
111 112
     }
112 113
 

+ 22 - 0
src/main/java/com/lqkj/link/config/ThreadPoolConfig.java

@@ -0,0 +1,22 @@
1
+package com.lqkj.link.config;
2
+
3
+import org.springframework.context.annotation.Bean;
4
+import org.springframework.context.annotation.Configuration;
5
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
6
+
7
+import java.util.concurrent.Executor;
8
+
9
+@Configuration
10
+public class ThreadPoolConfig {
11
+
12
+    @Bean(name = "taskExecutor")
13
+    public Executor taskExecutor() {
14
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
15
+        executor.setCorePoolSize(10); // 核心线程池大小
16
+        executor.setMaxPoolSize(20); // 最大线程池大小
17
+        executor.setQueueCapacity(50); // 队列容量
18
+        executor.setThreadNamePrefix("Async-"); // 线程名称前缀
19
+        executor.initialize();
20
+        return executor;
21
+    }
22
+}

+ 13 - 0
src/main/java/com/lqkj/link/module/base/service/BaseService.java

@@ -1,15 +1,20 @@
1 1
 package com.lqkj.link.module.base.service;
2 2
 
3 3
 import com.lqkj.link.message.MessageBean;
4
+import com.lqkj.link.util.AliOSSUtils;
4 5
 import com.lqkj.link.util.FileUtils;
6
+import org.springframework.beans.factory.annotation.Autowired;
5 7
 import org.springframework.stereotype.Service;
6 8
 import org.springframework.web.multipart.MultipartFile;
7 9
 
10
+import java.io.IOException;
8 11
 import java.util.UUID;
9 12
 
10 13
 @Service
11 14
 public class BaseService {
12 15
 
16
+    @Autowired
17
+    private AliOSSUtils aliOSSUtils;
13 18
     public MessageBean<String> uploadImg(MultipartFile file, String path) {
14 19
         String fileName = file.getOriginalFilename();
15 20
         String suffix = fileName == null ? "" : fileName.substring(fileName.lastIndexOf(".") + 1);
@@ -75,4 +80,12 @@ public class BaseService {
75 80
         FileUtils.saveFile(file, filePath, newFileName);
76 81
         return MessageBean.ok(filePath.substring(1) + newFileName, "上传模型文件");
77 82
     }
83
+
84
+    public String ossUpload(MultipartFile file) {
85
+        try {
86
+            return aliOSSUtils.upload(file);
87
+        } catch (IOException e) {
88
+            throw new RuntimeException(e);
89
+        }
90
+    }
78 91
 }

+ 13 - 0
src/main/java/com/lqkj/link/module/zone/controller/ResourceController.java

@@ -237,4 +237,17 @@ public class ResourceController {
237 237
         String userCode = jwtService.decryptUsernameWithHeader(authHeader);
238 238
         return MessageBean.ok(resourceService.needRefreshResource(userCode), "资源更新检查");
239 239
     }
240
+
241
+
242
+    @Operation(
243
+            summary = "5.1.3.7 阿里云上传接口",
244
+            description = "5.1.3.7 阿里云上传接口",
245
+            parameters = {
246
+                    @Parameter(name = "file", description = "文件", required = true)
247
+            }
248
+    )
249
+    @PostMapping("/ossUpload")
250
+    public MessageBean<String> ossUpload(MultipartFile file) {
251
+        return MessageBean.ok(baseService.ossUpload(file), "oss上传");
252
+    }
240 253
 }

+ 12 - 0
src/main/java/com/lqkj/link/module/zone/domain/GeomInfo.java

@@ -54,6 +54,18 @@ public class GeomInfo {
54 54
     private JSONObject trans;
55 55
     @Transient
56 56
     private String modelPath;
57
+    @Column(name = "pics")
58
+    private String pics;
59
+    @Column(name = "video_url")
60
+    private String videoUrl;
61
+    @Column(name = "audio_url")
62
+    private String audioUrl;
63
+    @Column(name = "brief")
64
+    private String brief;
65
+    @Column(name = "navigation")
66
+    private Boolean navigation;
67
+    @Column(name = "navigation_end")
68
+    private String navigationEnd;
57 69
 
58 70
     /**
59 71
      * 材质ID

+ 16 - 25
src/main/java/com/lqkj/link/module/zone/service/ResourceService.java

@@ -6,8 +6,10 @@ import com.lqkj.link.module.zone.domain.ModelCategory;
6 6
 import com.lqkj.link.module.zone.domain.ModelInfo;
7 7
 import com.lqkj.link.module.zone.repository.ModelCategoryRepository;
8 8
 import com.lqkj.link.module.zone.repository.ModelInfoRepository;
9
+import com.lqkj.link.util.AliOSSUtils;
9 10
 import com.lqkj.link.util.Unzipper;
10 11
 import org.apache.commons.compress.archivers.ArchiveException;
12
+import org.springframework.beans.factory.annotation.Autowired;
11 13
 import org.springframework.data.domain.Page;
12 14
 import org.springframework.data.domain.PageRequest;
13 15
 import org.springframework.data.domain.Pageable;
@@ -25,6 +27,9 @@ public class ResourceService {
25 27
     private final ModelInfoRepository infoRepository;
26 28
     private final UserInfoRepository userInfoRepository;
27 29
 
30
+    @Autowired
31
+    private AliOSSUtils aliOSSUtils;
32
+
28 33
     public ResourceService(ModelCategoryRepository categoryRepository, ModelInfoRepository infoRepository, UserInfoRepository userInfoRepository) {
29 34
         this.categoryRepository = categoryRepository;
30 35
         this.infoRepository = infoRepository;
@@ -72,39 +77,25 @@ public class ResourceService {
72 77
                 for (File model : models) {
73 78
                     if (model.isFile() && model.getName().endsWith(".fbx")) {
74 79
                         String modelFileName = model.getName();
75
-                        String modelPath = "/upload/resource/" + category.getCategoryId() + "/model/";
76
-                        if (modelFileName.matches(".*[一-龥]+.*")) {
77
-                            // 如果包含中文,重命名
78
-                            String newFileName = UUID.randomUUID() + ".fbx";
79
-                            modelPath += newFileName;
80
-                            boolean rename = model.renameTo(new File("." + modelPath));
80
+                        String modelPath = aliOSSUtils.upload(model);
81
+                        // 如果包含中文,重命名
82
+                        String newFileName = UUID.randomUUID() + ".png";
83
+                        String modelIconPath = "." + "/upload/resource/" + category.getCategoryId() + "/icon/" + newFileName;
84
+                        // 模型图标也许要重命名
85
+                        File oldIconFile = new File("." + "/upload/resource/" + category.getCategoryId() + "/icon/" + modelFileName.replace(".fbx", ".png"));
86
+                        if (oldIconFile.exists()) {
87
+                            File newIconFile = new File(modelIconPath);
88
+                            Boolean rename = oldIconFile.renameTo(newIconFile);
81 89
                             System.out.println(rename);
82
-                            // 材质文件也需要重命名
83
-                            File oldMaterialFile = new File("." + "/upload/resource/" + category.getCategoryId() + "/model/" + modelFileName.replace(".fbx", ".json"));
84
-                            if (oldMaterialFile.exists()) {
85
-                                File newMaterialFile = new File("." + "/upload/resource/" + category.getCategoryId() + "/model/" + newFileName.replace(".fbx", ".json"));
86
-                                rename = oldMaterialFile.renameTo(newMaterialFile);
87
-                                System.out.println(rename);
88
-                            }
89
-
90
-                            // 模型图标也许要重命名
91
-                            File oldIconFile = new File("." + "/upload/resource/" + category.getCategoryId() + "/icon/" + modelFileName.replace(".fbx", ".png"));
92
-                            if (oldIconFile.exists()) {
93
-                                File newIconFile = new File("." + "/upload/resource/" + category.getCategoryId() + "/icon/" + newFileName.replace(".fbx", ".png"));
94
-                                rename = oldIconFile.renameTo(newIconFile);
95
-                                System.out.println(rename);
96
-                            }
97
-                        } else {
98
-                            modelPath += modelFileName;
99 90
                         }
100 91
                         if (!modelInfoMap.containsKey(modelFileName.substring(0, modelFileName.lastIndexOf(".")))) {
101 92
                             list.add(new ModelInfo(null, category.getCategoryId(), modelFileName.substring(0, modelFileName.lastIndexOf(".")),
102 93
                                     null, null, null, modelPath,
103
-                                    modelPath.replace("model", "icon").replace("fbx", "png"), null, null));
94
+                                    modelIconPath.substring(1), null, null));
104 95
                         } else {
105 96
                             ModelInfo modelInfo = modelInfoMap.get(modelFileName.substring(0, modelFileName.lastIndexOf(".")));
106 97
                             modelInfo.setOriginalPath(modelPath);
107
-                            modelInfo.setModelIcon(modelPath.replace("model", "icon").replace("fbx", "png"));
98
+                            modelInfo.setModelIcon(modelIconPath.substring(1));
108 99
                             modelInfo.setJsonPath(null);
109 100
                             modelInfo.setTexturePath(null);
110 101
                             list.add(modelInfo);

+ 126 - 135
src/main/java/com/lqkj/link/module/zone/service/ZoneInfoService.java

@@ -26,6 +26,7 @@ import org.springframework.stereotype.Service;
26 26
 import org.springframework.transaction.annotation.Transactional;
27 27
 
28 28
 import java.io.File;
29
+import java.io.IOException;
29 30
 import java.nio.file.Files;
30 31
 import java.nio.file.Paths;
31 32
 import java.sql.Timestamp;
@@ -40,6 +41,9 @@ public class ZoneInfoService {
40 41
     private final GeomInfoRepository geomInfoRepository;
41 42
     private final ModelInfoRepository modelInfoRepository;
42 43
 
44
+    @Autowired
45
+    private AliOSSUtils aliOSSUtils;
46
+
43 47
     private static final Long ONEDAY = 24 * 60 * 60 * 1000L;
44 48
 
45 49
     @Autowired
@@ -79,150 +83,137 @@ public class ZoneInfoService {
79 83
             zoneInfo.setTemplateUse(0);
80 84
         }
81 85
         ZoneInfo zoneInfo1 = zoneInfoRepository.save(zoneInfo);
82
-        try {
83
-            if (StringUtils.isNotBlank(zoneInfo.getTemplateFilePath())) {
84
-                // 清除元素与模型
85
-                geomInfoRepository.deleteAllByZoneId(zoneInfo1.getZoneId());
86
-                modelInfoRepository.deleteAllByTemplateIds(Collections.singletonList(zoneInfo1.getZoneId()));
87 86
 
88
-                // 解压压缩文件
89
-                // 文件目录格式:
90
-                // --geom.json
91
-                // --models
92
-                // ----model1.fbx 模型1文件
93
-                // ----model2.fbx 模型2文件
94
-                // ----model1.json 模型1材质
95
-                // ----model2.json 模型2材质
96
-                //
97
-                // geom.json文件总json格式:
98
-                // 	{
99
-                //		"init": {
100
-                //			"rotation": {
101
-                //				"x": 0,
102
-                //				"y": 0,
103
-                //				"z": 0,
104
-                //				"w": 0
105
-                //			},
106
-                //			"translation": {
107
-                //				"x": 0,
108
-                //				"y": 0,
109
-                //				"z": 0
110
-                //			},
111
-                //			"scale3D": {
112
-                //				"x": 0,
113
-                //				"y": 0,
114
-                //				"z": 0
115
-                //			}
116
-                //		},
117
-                //		"models": [
118
-                //			{
119
-                //				"modelPath": "model1.fbx",
120
-                //				"location": [
121
-                //					{
122
-                //						"rotation": {
123
-                //							"x": 0,
124
-                //							"y": 0,
125
-                //							"z": 0,
126
-                //							"w": 0
127
-                //						},
128
-                //						"translation": {
129
-                //							"x": 0,
130
-                //							"y": 0,
131
-                //							"z": 0
132
-                //						},
133
-                //						"scale3D": {
134
-                //							"x": 0,
135
-                //							"y": 0,
136
-                //							"z": 0
137
-                //						}
138
-                //					}
139
-                //				]
140
-                //			}
141
-                //		]
142
-                //	}
87
+        if (StringUtils.isNotBlank(zoneInfo.getTemplateFilePath())) {
88
+            // 清除元素与模型
89
+            geomInfoRepository.deleteAllByZoneId(zoneInfo1.getZoneId());
90
+            modelInfoRepository.deleteAllByTemplateIds(Collections.singletonList(zoneInfo1.getZoneId()));
91
+
92
+            // 解压压缩文件
93
+            // 文件目录格式:
94
+            // --geom.json
95
+            // --models
96
+            // ----model1.fbx 模型1文件
97
+            // ----model2.fbx 模型2文件
98
+            // ----model1.json 模型1材质
99
+            // ----model2.json 模型2材质
100
+            //
101
+            // geom.json文件总json格式:
102
+            // 	{
103
+            //		"init": {
104
+            //			"rotation": {
105
+            //				"x": 0,
106
+            //				"y": 0,
107
+            //				"z": 0,
108
+            //				"w": 0
109
+            //			},
110
+            //			"translation": {
111
+            //				"x": 0,
112
+            //				"y": 0,
113
+            //				"z": 0
114
+            //			},
115
+            //			"scale3D": {
116
+            //				"x": 0,
117
+            //				"y": 0,
118
+            //				"z": 0
119
+            //			}
120
+            //		},
121
+            //		"models": [
122
+            //			{
123
+            //				"modelPath": "model1.fbx",
124
+            //				"location": [
125
+            //					{
126
+            //						"rotation": {
127
+            //							"x": 0,
128
+            //							"y": 0,
129
+            //							"z": 0,
130
+            //							"w": 0
131
+            //						},
132
+            //						"translation": {
133
+            //							"x": 0,
134
+            //							"y": 0,
135
+            //							"z": 0
136
+            //						},
137
+            //						"scale3D": {
138
+            //							"x": 0,
139
+            //							"y": 0,
140
+            //							"z": 0
141
+            //						}
142
+            //					}
143
+            //				]
144
+            //			}
145
+            //		]
146
+            //	}
147
+            String geomJsonString = null;
148
+            try {
143 149
                 Unzipper.unZipFiles(new File("." + zoneInfo.getTemplateFilePath()), "./upload/template/" + zoneInfo1.getZoneId() + "/");
144
-                String geomJsonString = Files.readString(Paths.get("./upload/template/" + zoneInfo1.getZoneId() + "/geom.json"));
145
-                TemplateGeom templateGeom = JSON.parseObject(geomJsonString, TemplateGeom.class);
146
-
147
-                zoneInfo1.setInitLocation(templateGeom.getInit());
148
-                zoneInfoRepository.save(zoneInfo1);
149
-
150
-                List<TemplateInfo> templateInfoList = templateGeom.getModels();
151
-
152
-                String modelFolderPath = "./upload/template/" + zoneInfo1.getZoneId() + "/models";
153
-                File modelFolder = new File(modelFolderPath);
154
-                List<ModelInfo> list = new ArrayList<>();
155
-                File[] models = modelFolder.listFiles();
156
-                assert models != null;
157
-                for (File model : models) {
158
-                    if (model.isFile()) {
159
-                        String modelFileName = model.getName();
160
-                        if (modelFileName.endsWith(".fbx")) {
161
-                            String modelPath = "/upload/template/" + zoneInfo1.getZoneId() + "/models/";
162
-                            if (modelFileName.matches(".*[\u4e00-\u9fa5]+.*")) {
163
-                                // 如果包含中文,重命名
164
-                                String newFileName =UUID.randomUUID() + ".fbx";
165
-                                modelPath += newFileName;
166
-                                boolean rename = model.renameTo(new File("." + modelPath));
167
-                                System.out.println(rename);
168
-                                // 材质文件也需要重命名
169
-                                File oldMaterialFile = new File("." + "/upload/template/" + zoneInfo1.getZoneId() + "/models/" + modelFileName.replace(".fbx", ".json"));
170
-                                if (oldMaterialFile.exists()) {
171
-                                    File newMaterialFile = new File("." + "/upload/template/" + zoneInfo1.getZoneId() + "/models/" + newFileName.replace(".fbx", ".json"));
172
-                                    rename = oldMaterialFile.renameTo(newMaterialFile);
173
-                                    System.out.println(rename);
174
-                                }
175
-                            } else {
176
-                                modelPath += modelFileName;
177
-                            }
178
-                            list.add(new ModelInfo(null, null, modelFileName.substring(0, modelFileName.lastIndexOf(".")).toLowerCase(),
179
-                                    null, null, null, modelPath,
180
-                                    zoneInfo1.getThumbnail(), null, zoneInfo1.getZoneId()));
181
-                        }
150
+                geomJsonString = Files.readString(Paths.get("./upload/template/" + zoneInfo1.getZoneId() + "/geom.json"));
151
+            } catch (IOException e) {
152
+                modelInfoRepository.deleteAllByTemplateIds(Collections.singletonList(zoneInfo1.getZoneId()));
153
+                zoneInfoRepository.delete(zoneInfo1);
154
+                result.put("msg", "模板压缩文件解压失败!");
155
+                return result;
156
+            }
157
+            TemplateGeom templateGeom = JSON.parseObject(geomJsonString, TemplateGeom.class);
158
+
159
+            zoneInfo1.setInitLocation(templateGeom.getInit());
160
+            zoneInfoRepository.save(zoneInfo1);
161
+
162
+            List<TemplateInfo> templateInfoList = templateGeom.getModels();
163
+
164
+            String modelFolderPath = "./upload/template/" + zoneInfo1.getZoneId() + "/models";
165
+            File modelFolder = new File(modelFolderPath);
166
+            List<ModelInfo> list = new ArrayList<>();
167
+            File[] models = modelFolder.listFiles();
168
+            assert models != null;
169
+            for (File model : models) {
170
+                if (model.isFile()) {
171
+                    String modelFileName = model.getName();
172
+                    if (modelFileName.endsWith(".fbx")) {
173
+                        String modelPath = aliOSSUtils.upload(model);
174
+                        list.add(new ModelInfo(null, null, modelFileName.substring(0, modelFileName.lastIndexOf(".")).toLowerCase(),
175
+                                null, null, null, modelPath,
176
+                                zoneInfo1.getThumbnail(), null, zoneInfo1.getZoneId()));
182 177
                     }
183 178
                 }
184
-                List<ModelInfo> modelInfoList = modelInfoRepository.saveAll(list);
185
-                Map<String, ModelInfo> modelPathIdMap = modelInfoList
186
-                        .stream()
187
-                        .collect(Collectors.toMap(ModelInfo::getModelName, ModelInfo -> ModelInfo));
188
-
189
-                List<GeomInfo> geomInfoList = new ArrayList<>();
190
-                GeometryFactory geometryFactory = new GeometryFactory();
191
-                int j = 1;
192
-                for (TemplateInfo templateInfo : templateInfoList) {
193
-
194
-                    String modelName = templateInfo.getModelPath().substring(0, templateInfo.getModelPath().lastIndexOf(".")).toLowerCase();
195
-                    ModelInfo modelInfo = modelPathIdMap.get(modelName);
196
-                    for (int i = 0; i < templateInfo.getLocation().size(); i++) {
197
-                        JSONObject trans = templateInfo.getLocation().get(i);
198
-                        GeomInfo geomInfo = new GeomInfo();
199
-
200
-                        geomInfo.setGeomId(zoneInfo1.getZoneId() + "_" + j);
201
-                        geomInfo.setModelId(modelInfo.getModelId());
202
-                        geomInfo.setGeomName(modelInfo.getModelName());
203
-                        geomInfo.setZoneId(zoneInfo1.getZoneId());
204
-                        geomInfo.setLocking(false);
205
-
206
-                        geomInfo.setTrans(trans);
207
-                        JSONObject pointObject = trans.getJSONObject("translation");
208
-                        geomInfo.setGeom(geometryFactory.createPoint(new Coordinate(
209
-                                Double.parseDouble(pointObject.get("x").toString()),
210
-                                Double.parseDouble(pointObject.get("y").toString()),
211
-                                Double.parseDouble(pointObject.get("z").toString()))));
212
-                        j++;
213
-                        geomInfoList.add(geomInfo);
214
-                    }
179
+            }
180
+            List<ModelInfo> modelInfoList = modelInfoRepository.saveAll(list);
181
+            Map<String, ModelInfo> modelPathIdMap = modelInfoList
182
+                    .stream()
183
+                    .collect(Collectors.toMap(ModelInfo::getModelName, ModelInfo -> ModelInfo));
215 184
 
185
+            List<GeomInfo> geomInfoList = new ArrayList<>();
186
+            GeometryFactory geometryFactory = new GeometryFactory();
187
+            int j = 1;
188
+            for (TemplateInfo templateInfo : templateInfoList) {
189
+
190
+                String modelName = templateInfo.getModelPath().substring(0, templateInfo.getModelPath().lastIndexOf(".")).toLowerCase();
191
+                ModelInfo modelInfo = modelPathIdMap.get(modelName);
192
+                for (int i = 0; i < templateInfo.getLocation().size(); i++) {
193
+                    JSONObject trans = templateInfo.getLocation().get(i);
194
+                    GeomInfo geomInfo = new GeomInfo();
195
+
196
+                    geomInfo.setGeomId(zoneInfo1.getZoneId() + "_" + j);
197
+                    geomInfo.setModelId(modelInfo.getModelId());
198
+                    geomInfo.setGeomName(modelInfo.getModelName());
199
+                    geomInfo.setZoneId(zoneInfo1.getZoneId());
200
+                    geomInfo.setLocking(false);
201
+
202
+                    geomInfo.setTrans(trans);
203
+                    JSONObject pointObject = trans.getJSONObject("translation");
204
+                    geomInfo.setGeom(geometryFactory.createPoint(new Coordinate(
205
+                            Double.parseDouble(pointObject.get("x").toString()),
206
+                            Double.parseDouble(pointObject.get("y").toString()),
207
+                            Double.parseDouble(pointObject.get("z").toString()))));
208
+                    j++;
209
+                    geomInfoList.add(geomInfo);
216 210
                 }
217
-                geomInfoRepository.saveAll(geomInfoList);
218 211
 
219 212
             }
220
-        } catch (Exception e) {
221
-            modelInfoRepository.deleteAllByTemplateIds(Collections.singletonList(zoneInfo1.getZoneId()));
222
-            zoneInfoRepository.delete(zoneInfo1);
223
-            result.put("msg", "模板压缩文件解压失败!");
224
-            return result;
213
+            geomInfoRepository.saveAll(geomInfoList);
214
+
225 215
         }
216
+
226 217
         result.put("zone", zoneInfo1);
227 218
         return result;
228 219
     }

+ 67 - 0
src/main/java/com/lqkj/link/util/AliOSSUtils.java

@@ -0,0 +1,67 @@
1
+package com.lqkj.link.util;
2
+
3
+import com.aliyun.oss.OSS;
4
+import com.aliyun.oss.OSSClientBuilder;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.stereotype.Component;
7
+import org.springframework.web.multipart.MultipartFile;
8
+
9
+import java.io.File;
10
+import java.io.FileInputStream;
11
+import java.io.IOException;
12
+import java.io.InputStream;
13
+import java.util.UUID;
14
+import java.util.concurrent.Callable;
15
+
16
+@Component
17
+public class AliOSSUtils {
18
+ 
19
+    /**
20
+     * 注入AliProperties
21
+     */
22
+    private final AliProperties aliProperties;
23
+
24
+    private final ThreadPoolUtil threadPoolUtil;
25
+
26
+    public AliOSSUtils(AliProperties aliProperties, ThreadPoolUtil threadPoolUtil) {
27
+        this.aliProperties = aliProperties;
28
+        this.threadPoolUtil = threadPoolUtil;
29
+    }
30
+
31
+    /**
32
+     * 实现上传图片到OSS
33
+     */
34
+    public String upload(File file) {
35
+        String originalFilename = file.getName();
36
+        String fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
37
+        //上传文件到 OSS
38
+        OSS ossClient = new OSSClientBuilder().build(aliProperties.getEndpoint(), aliProperties.getAccessKeyId(), aliProperties.getAccessKeySecret());
39
+        threadPoolUtil.getTaskExecutor().execute(() -> {
40
+            ossClient.putObject(aliProperties.getBucketName(), fileName, file);
41
+            ossClient.shutdown();
42
+        });
43
+        //文件访问路径
44
+        String url = aliProperties.getEndpoint().split("//")[0] + "//" + aliProperties.getBucketName() + "." + aliProperties.getEndpoint().split("//")[1] + "/" + fileName;
45
+        // 关闭ossClient
46
+
47
+        return url;// 把上传到oss的路径返回
48
+    }
49
+
50
+    public String upload(MultipartFile file) throws IOException {
51
+        // 获取上传的文件的输入流
52
+        InputStream inputStream = file.getInputStream();
53
+        // 避免文件覆盖
54
+        String originalFilename = file.getOriginalFilename();
55
+        String fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
56
+        //上传文件到 OSS
57
+        OSS ossClient = new OSSClientBuilder().build(aliProperties.getEndpoint(), aliProperties.getAccessKeyId(), aliProperties.getAccessKeySecret());
58
+        threadPoolUtil.getTaskExecutor().execute(() -> {
59
+            ossClient.putObject(aliProperties.getBucketName(), fileName, inputStream);
60
+            ossClient.shutdown();
61
+        });
62
+        //文件访问路径
63
+        return aliProperties.getEndpoint().split("//")[0] + "//" + aliProperties.getBucketName() + "." + aliProperties.getEndpoint().split("//")[1] + "/" + fileName;
64
+    }
65
+
66
+ 
67
+}

+ 16 - 0
src/main/java/com/lqkj/link/util/AliProperties.java

@@ -0,0 +1,16 @@
1
+package com.lqkj.link.util;
2
+
3
+import lombok.Data;
4
+import org.springframework.boot.context.properties.ConfigurationProperties;
5
+import org.springframework.stereotype.Component;
6
+ 
7
+@Component
8
+@Data
9
+@ConfigurationProperties(prefix = "aliyun")
10
+public class AliProperties {
11
+    private String endpoint;		//Bucket域名
12
+    private String accessKeyId;		//阿里云账号AccessKey
13
+    private String accessKeySecret;	//阿里云账号AccessKey对应的秘钥
14
+    private String bucketName;		//Bucket名称
15
+
16
+}

+ 23 - 0
src/main/java/com/lqkj/link/util/ThreadPoolUtil.java

@@ -0,0 +1,23 @@
1
+package com.lqkj.link.util;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.stereotype.Component;
5
+
6
+import java.util.concurrent.Executor;
7
+
8
+@Component
9
+public class ThreadPoolUtil {
10
+
11
+    private final Executor taskExecutor;
12
+
13
+    @Autowired
14
+    public ThreadPoolUtil(Executor taskExecutor) {
15
+        this.taskExecutor = taskExecutor;
16
+    }
17
+
18
+    public Executor getTaskExecutor() {
19
+
20
+
21
+        return taskExecutor;
22
+    }
23
+}

+ 8 - 1
src/main/resources/application-install.yml

@@ -95,4 +95,11 @@ springdoc:
95 95
     path: /v3/api-docs
96 96
   swagger-ui:
97 97
     persist-authorization: true
98
-    path: /swagger-ui.html
98
+    path: /swagger-ui.html
99
+
100
+  # 阿里云
101
+aliyun:
102
+  endpoint: https://oss-cn-chengdu.aliyuncs.com
103
+  accessKeyId: LTAI5tLJsyF7YvFf48sC5yVL
104
+  accessKeySecret: v48ceFlBuXhyeoduxOBFcEdeN4pL1G
105
+  bucketName: link-meta