|
@@ -1,21 +1,23 @@
|
|
|
package cn.com.qmth.stmms.biz.file.service.impl;
|
|
|
|
|
|
-import cn.com.qmth.stmms.biz.file.enums.FileType;
|
|
|
-import cn.com.qmth.stmms.biz.file.enums.FormatType;
|
|
|
-import cn.com.qmth.stmms.biz.file.service.FileService;
|
|
|
-import cn.com.qmth.stmms.biz.file.store.FileStore;
|
|
|
-import cn.com.qmth.stmms.biz.file.store.impl.DiskStore;
|
|
|
-import cn.com.qmth.stmms.biz.file.store.impl.OssStore;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.LinkedList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
+import org.apache.commons.lang.math.RandomUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.InputStream;
|
|
|
-import java.util.LinkedList;
|
|
|
-import java.util.List;
|
|
|
+import cn.com.qmth.stmms.biz.file.enums.FileType;
|
|
|
+import cn.com.qmth.stmms.biz.file.enums.FormatType;
|
|
|
+import cn.com.qmth.stmms.biz.file.service.FileService;
|
|
|
+import cn.com.qmth.stmms.biz.file.store.FileStore;
|
|
|
+import cn.com.qmth.stmms.biz.file.store.impl.DiskStore;
|
|
|
+import cn.com.qmth.stmms.biz.file.store.impl.OssStore;
|
|
|
|
|
|
@Service("fileService")
|
|
|
public class FileServiceImpl implements FileService, InitializingBean, DisposableBean {
|
|
@@ -24,8 +26,12 @@ public class FileServiceImpl implements FileService, InitializingBean, Disposabl
|
|
|
|
|
|
private static final int DEFAULT_SUFFIX_LENGTH = 3;
|
|
|
|
|
|
+ public static final String SERVER_SPLIT = ",";
|
|
|
+
|
|
|
@Value("${file.server}")
|
|
|
- private String fileServer;
|
|
|
+ private String fileServerString;
|
|
|
+
|
|
|
+ private List<String> fileServers;
|
|
|
|
|
|
@Value("${file.store}")
|
|
|
private String fileStore;
|
|
@@ -47,11 +53,21 @@ public class FileServiceImpl implements FileService, InitializingBean, Disposabl
|
|
|
|
|
|
@Override
|
|
|
public String getFileServer() {
|
|
|
- return fileServer;
|
|
|
+ return fileServers.get(0);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getRandomFileServer() {
|
|
|
+ return fileServers.get(RandomUtils.nextInt(fileServers.size()));
|
|
|
}
|
|
|
|
|
|
- public void setFileServer(String fileServer) {
|
|
|
- this.fileServer = fileServer;
|
|
|
+ public String getFileServerString() {
|
|
|
+ return fileServerString;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setFileServerString(String fileServerString) {
|
|
|
+ this.fileServerString = fileServerString;
|
|
|
}
|
|
|
|
|
|
public String getFileStore() {
|
|
@@ -217,17 +233,31 @@ public class FileServiceImpl implements FileService, InitializingBean, Disposabl
|
|
|
|
|
|
@Override
|
|
|
public void afterPropertiesSet() {
|
|
|
- fileServer = StringUtils.trimToNull(fileServer);
|
|
|
+ fileServerString = StringUtils.trimToNull(fileServerString);
|
|
|
fileStore = StringUtils.trimToNull(fileStore);
|
|
|
- if (fileServer == null) {
|
|
|
+
|
|
|
+ if (fileServerString == null) {
|
|
|
throw new RuntimeException("invald property: ${file.server} should not be empty");
|
|
|
}
|
|
|
if (fileStore == null) {
|
|
|
throw new RuntimeException("invald property: ${file.store} should not be empty");
|
|
|
}
|
|
|
- if (!fileServer.endsWith("/")) {
|
|
|
- fileServer = fileServer.concat("/");
|
|
|
+ // 按逗号拆分多个文件服务器地址
|
|
|
+ fileServers = new ArrayList<>();
|
|
|
+ String[] servers = StringUtils.split(fileServerString, SERVER_SPLIT);
|
|
|
+ for (String server : servers) {
|
|
|
+ server = StringUtils.trimToNull(server);
|
|
|
+ if (server != null) {
|
|
|
+ if (!server.endsWith("/")) {
|
|
|
+ server = server.concat("/");
|
|
|
+ }
|
|
|
+ fileServers.add(server);
|
|
|
+ }
|
|
|
}
|
|
|
+ if (fileServers.isEmpty()) {
|
|
|
+ throw new RuntimeException("invald property: ${file.server} should not be empty");
|
|
|
+ }
|
|
|
+ // 按前缀解析文件存储引擎
|
|
|
if (fileStore.startsWith("oss")) {
|
|
|
store = new OssStore(fileStore);
|
|
|
} else {
|
|
@@ -257,7 +287,8 @@ public class FileServiceImpl implements FileService, InitializingBean, Disposabl
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
FileServiceImpl service = new FileServiceImpl();
|
|
|
- service.fileServer = "123";
|
|
|
+ service.fileServerString = "123";
|
|
|
+
|
|
|
service.fileStore = "oss://LTAI4FnJ2pgV6aGceYcCkeEi:ktrMEVE7PfoxRPeJUPDFeygOIH4aU7@qmth-test.oss-cn-shenzhen.aliyuncs.com";
|
|
|
service.afterPropertiesSet();
|
|
|
|