From ab02e6e14c1b31f3477c434f31213685246603f5 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 20 十一月 2024 16:57:20 +0800 Subject: [PATCH] 1 --- se-modules/se-system/src/main/java/com/se/system/service/impl/FastDfsSysFileServiceImpl.java | 48 ++++ se-modules/se-system/pom.xml | 20 ++ se-modules/se-system/src/main/java/com/se/system/controller/SysFileController.java | 49 ++++ se-modules/se-system/src/main/java/com/se/system/service/impl/LocalSysFileServiceImpl.java | 51 +++++ se-modules/se-system/src/main/java/com/se/system/service/impl/MinioSysFileServiceImpl.java | 51 +++++ 说明.txt | 23 ++ se-modules/se-system/src/main/java/com/se/system/config/ResourcesConfig.java | 51 +++++ se-modules/se-system/src/main/java/com/se/system/config/MinioConfig.java | 82 ++++++++ se-modules/se-system/src/main/java/com/se/system/service/inte/ISysFileService.java | 20 ++ se-modules/se-system/src/main/java/com/se/system/utils/FileUploadUtils.java | 186 ++++++++++++++++++ 10 files changed, 581 insertions(+), 0 deletions(-) diff --git a/se-modules/se-system/pom.xml b/se-modules/se-system/pom.xml index 10ee629..d80a606 100644 --- a/se-modules/se-system/pom.xml +++ b/se-modules/se-system/pom.xml @@ -85,6 +85,26 @@ <version>1.18.22</version> <optional>true</optional> </dependency> + + <!-- FastDFS --> + <dependency> + <groupId>com.github.tobato</groupId> + <artifactId>fastdfs-client</artifactId> + </dependency> + + <!-- Minio --> + <dependency> + <groupId>io.minio</groupId> + <artifactId>minio</artifactId> + <version>8.2.2</version> + <!-- <version>${minio.version}</version>--> + </dependency> + + <!-- SE Api System --> + <dependency> + <groupId>com.se</groupId> + <artifactId>se-api-system</artifactId> + </dependency> </dependencies> <build> diff --git a/se-modules/se-system/src/main/java/com/se/system/config/MinioConfig.java b/se-modules/se-system/src/main/java/com/se/system/config/MinioConfig.java new file mode 100644 index 0000000..f71e750 --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/config/MinioConfig.java @@ -0,0 +1,82 @@ +package com.se.system.config; + +import io.minio.MinioClient; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Minio 閰嶇疆淇℃伅 + * + * @author admin + */ +@Configuration +@ConfigurationProperties(prefix = "minio") +public class MinioConfig +{ + /** + * 鏈嶅姟鍦板潃 + */ + private String url; + + /** + * 鐢ㄦ埛鍚� + */ + private String accessKey; + + /** + * 瀵嗙爜 + */ + private String secretKey; + + /** + * 瀛樺偍妗跺悕绉� + */ + private String bucketName; + + public String getUrl() + { + return url; + } + + public void setUrl(String url) + { + this.url = url; + } + + public String getAccessKey() + { + return accessKey; + } + + public void setAccessKey(String accessKey) + { + this.accessKey = accessKey; + } + + public String getSecretKey() + { + return secretKey; + } + + public void setSecretKey(String secretKey) + { + this.secretKey = secretKey; + } + + public String getBucketName() + { + return bucketName; + } + + public void setBucketName(String bucketName) + { + this.bucketName = bucketName; + } + + @Bean + public MinioClient getMinioClient() + { + return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build(); + } +} diff --git a/se-modules/se-system/src/main/java/com/se/system/config/ResourcesConfig.java b/se-modules/se-system/src/main/java/com/se/system/config/ResourcesConfig.java new file mode 100644 index 0000000..1b1bded --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/config/ResourcesConfig.java @@ -0,0 +1,51 @@ +package com.se.system.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.io.File; + +/** + * 閫氱敤鏄犲皠閰嶇疆 + * + * @author admin + */ +@Configuration +public class ResourcesConfig implements WebMvcConfigurer +{ + /** + * 涓婁紶鏂囦欢瀛樺偍鍦ㄦ湰鍦扮殑鏍硅矾寰� + */ + @Value("${file.path}") + private String localFilePath; + + /** + * 璧勬簮鏄犲皠璺緞 鍓嶇紑 + */ + @Value("${file.prefix}") + public String localFilePrefix; + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) + { + /** 鏈湴鏂囦欢涓婁紶璺緞 */ + registry.addResourceHandler(localFilePrefix + "/**") + .addResourceLocations("file:" + localFilePath + File.separator); + } + + /** + * 寮�鍚法鍩� + */ + @Override + public void addCorsMappings(CorsRegistry registry) { + // 璁剧疆鍏佽璺ㄥ煙鐨勮矾鐢� + registry.addMapping(localFilePrefix + "/**") + // 璁剧疆鍏佽璺ㄥ煙璇锋眰鐨勫煙鍚� + .allowedOrigins("*") + // 璁剧疆鍏佽鐨勬柟娉� + .allowedMethods("GET"); + } +} \ No newline at end of file diff --git a/se-modules/se-system/src/main/java/com/se/system/controller/SysFileController.java b/se-modules/se-system/src/main/java/com/se/system/controller/SysFileController.java new file mode 100644 index 0000000..29f3e0b --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/controller/SysFileController.java @@ -0,0 +1,49 @@ +package com.se.system.controller; + +import com.se.common.core.domain.R; +import com.se.common.core.utils.file.FileUtils; +import com.se.system.api.domain.SysFile; +import com.se.system.service.inte.ISysFileService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; + +/** + * 鏂囦欢璇锋眰澶勭悊 + * + * @author admin + */ +@RestController +public class SysFileController +{ + private static final Logger log = LoggerFactory.getLogger(SysFileController.class); + + @Resource + private ISysFileService sysFileService; + + /** + * 鏂囦欢涓婁紶璇锋眰 + */ + @PostMapping("upload") + public R<SysFile> upload(MultipartFile file) + { + try + { + // 涓婁紶骞惰繑鍥炶闂湴鍧� + String url = sysFileService.uploadFile(file); + SysFile sysFile = new SysFile(); + sysFile.setName(FileUtils.getName(url)); + sysFile.setUrl(url); + return R.ok(sysFile); + } + catch (Exception e) + { + log.error("涓婁紶鏂囦欢澶辫触", e); + return R.fail(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/se-modules/se-system/src/main/java/com/se/system/service/impl/FastDfsSysFileServiceImpl.java b/se-modules/se-system/src/main/java/com/se/system/service/impl/FastDfsSysFileServiceImpl.java new file mode 100644 index 0000000..3984b66 --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/service/impl/FastDfsSysFileServiceImpl.java @@ -0,0 +1,48 @@ +package com.se.system.service.impl; + +import com.alibaba.nacos.common.utils.IoUtils; +import com.github.tobato.fastdfs.domain.fdfs.StorePath; +import com.github.tobato.fastdfs.service.FastFileStorageClient; +import com.se.common.core.utils.file.FileTypeUtils; +import com.se.system.service.inte.ISysFileService; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.io.InputStream; + +/** + * FastDFS 鏂囦欢瀛樺偍 + * + * @author admin + */ +@Service +public class FastDfsSysFileServiceImpl implements ISysFileService +{ + /** + * 鍩熷悕鎴栨湰鏈鸿闂湴鍧� + */ + @Value("${fdfs.domain}") + public String domain; + + @Resource + private FastFileStorageClient storageClient; + + /** + * FastDfs鏂囦欢涓婁紶鎺ュ彛 + * + * @param file 涓婁紶鐨勬枃浠� + * @return 璁块棶鍦板潃 + * @throws Exception + */ + @Override + public String uploadFile(MultipartFile file) throws Exception + { + InputStream inputStream = file.getInputStream(); + StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(), + FileTypeUtils.getExtension(file), null); + IoUtils.closeQuietly(inputStream); + return domain + "/" + storePath.getFullPath(); + } +} diff --git a/se-modules/se-system/src/main/java/com/se/system/service/impl/LocalSysFileServiceImpl.java b/se-modules/se-system/src/main/java/com/se/system/service/impl/LocalSysFileServiceImpl.java new file mode 100644 index 0000000..fb72e1c --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/service/impl/LocalSysFileServiceImpl.java @@ -0,0 +1,51 @@ +package com.se.system.service.impl; + +import com.se.system.service.inte.ISysFileService; +import com.se.system.utils.FileUploadUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +/** + * 鏈湴鏂囦欢瀛樺偍 + * + * @author admin + */ +@Primary +@Service +public class LocalSysFileServiceImpl implements ISysFileService +{ + /** + * 璧勬簮鏄犲皠璺緞 鍓嶇紑 + */ + @Value("${file.prefix}") + public String localFilePrefix; + + /** + * 鍩熷悕鎴栨湰鏈鸿闂湴鍧� + */ + @Value("${file.domain}") + public String domain; + + /** + * 涓婁紶鏂囦欢瀛樺偍鍦ㄦ湰鍦扮殑鏍硅矾寰� + */ + @Value("${file.path}") + private String localFilePath; + + /** + * 鏈湴鏂囦欢涓婁紶鎺ュ彛 + * + * @param file 涓婁紶鐨勬枃浠� + * @return 璁块棶鍦板潃 + * @throws Exception + */ + @Override + public String uploadFile(MultipartFile file) throws Exception + { + String name = FileUploadUtils.upload(localFilePath, file); + String url = domain + localFilePrefix + name; + return url; + } +} diff --git a/se-modules/se-system/src/main/java/com/se/system/service/impl/MinioSysFileServiceImpl.java b/se-modules/se-system/src/main/java/com/se/system/service/impl/MinioSysFileServiceImpl.java new file mode 100644 index 0000000..337ffa1 --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/service/impl/MinioSysFileServiceImpl.java @@ -0,0 +1,51 @@ +package com.se.system.service.impl; + +import com.alibaba.nacos.common.utils.IoUtils; +import com.se.system.config.MinioConfig; +import com.se.system.service.inte.ISysFileService; +import com.se.system.utils.FileUploadUtils; +import io.minio.MinioClient; +import io.minio.PutObjectArgs; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.io.InputStream; + +/** + * Minio 鏂囦欢瀛樺偍 + * + * @author admin + */ +@Service +public class MinioSysFileServiceImpl implements ISysFileService +{ + @Resource + private MinioConfig minioConfig; + + @Resource + private MinioClient client; + + /** + * Minio鏂囦欢涓婁紶鎺ュ彛 + * + * @param file 涓婁紶鐨勬枃浠� + * @return 璁块棶鍦板潃 + * @throws Exception + */ + @Override + public String uploadFile(MultipartFile file) throws Exception + { + String fileName = FileUploadUtils.extractFilename(file); + InputStream inputStream = file.getInputStream(); + PutObjectArgs args = PutObjectArgs.builder() + .bucket(minioConfig.getBucketName()) + .object(fileName) + .stream(inputStream, file.getSize(), -1) + .contentType(file.getContentType()) + .build(); + client.putObject(args); + IoUtils.closeQuietly(inputStream); + return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName; + } +} diff --git a/se-modules/se-system/src/main/java/com/se/system/service/inte/ISysFileService.java b/se-modules/se-system/src/main/java/com/se/system/service/inte/ISysFileService.java new file mode 100644 index 0000000..7077669 --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/service/inte/ISysFileService.java @@ -0,0 +1,20 @@ +package com.se.system.service.inte; + +import org.springframework.web.multipart.MultipartFile; + +/** + * 鏂囦欢涓婁紶鎺ュ彛 + * + * @author admin + */ +public interface ISysFileService +{ + /** + * 鏂囦欢涓婁紶鎺ュ彛 + * + * @param file 涓婁紶鐨勬枃浠� + * @return 璁块棶鍦板潃 + * @throws Exception + */ + public String uploadFile(MultipartFile file) throws Exception; +} diff --git a/se-modules/se-system/src/main/java/com/se/system/utils/FileUploadUtils.java b/se-modules/se-system/src/main/java/com/se/system/utils/FileUploadUtils.java new file mode 100644 index 0000000..d453adc --- /dev/null +++ b/se-modules/se-system/src/main/java/com/se/system/utils/FileUploadUtils.java @@ -0,0 +1,186 @@ +package com.se.system.utils; + +import com.se.common.core.exception.file.FileException; +import com.se.common.core.exception.file.FileNameLengthLimitExceededException; +import com.se.common.core.exception.file.FileSizeLimitExceededException; +import com.se.common.core.exception.file.InvalidExtensionException; +import com.se.common.core.utils.DateUtils; +import com.se.common.core.utils.StringUtils; +import com.se.common.core.utils.file.FileTypeUtils; +import com.se.common.core.utils.file.MimeTypeUtils; +import com.se.common.core.utils.uuid.Seq; +import org.apache.commons.io.FilenameUtils; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Objects; + +/** + * 鏂囦欢涓婁紶宸ュ叿绫� + * + * @author admin + */ +public class FileUploadUtils +{ + /** + * 榛樿澶у皬 50M + */ + public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024L; + + /** + * 榛樿鐨勬枃浠跺悕鏈�澶ч暱搴� 100 + */ + public static final int DEFAULT_FILE_NAME_LENGTH = 100; + + /** + * 鏍规嵁鏂囦欢璺緞涓婁紶 + * + * @param baseDir 鐩稿搴旂敤鐨勫熀鐩綍 + * @param file 涓婁紶鐨勬枃浠� + * @return 鏂囦欢鍚嶇О + * @throws IOException + */ + public static final String upload(String baseDir, MultipartFile file) throws IOException + { + try + { + return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); + } + catch (FileException fe) + { + throw new IOException(fe.getDefaultMessage(), fe); + } + catch (Exception e) + { + throw new IOException(e.getMessage(), e); + } + } + + /** + * 鏂囦欢涓婁紶 + * + * @param baseDir 鐩稿搴旂敤鐨勫熀鐩綍 + * @param file 涓婁紶鐨勬枃浠� + * @param allowedExtension 涓婁紶鏂囦欢绫诲瀷 + * @return 杩斿洖涓婁紶鎴愬姛鐨勬枃浠跺悕 + * @throws FileSizeLimitExceededException 濡傛灉瓒呭嚭鏈�澶уぇ灏� + * @throws FileNameLengthLimitExceededException 鏂囦欢鍚嶅お闀� + * @throws IOException 姣斿璇诲啓鏂囦欢鍑洪敊鏃� + * @throws InvalidExtensionException 鏂囦欢鏍¢獙寮傚父 + */ + public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension) + throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, + InvalidExtensionException + { + int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length(); + if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) + { + throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH); + } + + assertAllowed(file, allowedExtension); + + String fileName = extractFilename(file); + + String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath(); + file.transferTo(Paths.get(absPath)); + return getPathFileName(fileName); + } + + /** + * 缂栫爜鏂囦欢鍚� + */ + public static final String extractFilename(MultipartFile file) + { + return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(), + FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), FileTypeUtils.getExtension(file)); + } + + private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException + { + File desc = new File(uploadDir + File.separator + fileName); + + if (!desc.exists()) + { + if (!desc.getParentFile().exists()) + { + desc.getParentFile().mkdirs(); + } + } + return desc.isAbsolute() ? desc : desc.getAbsoluteFile(); + } + + private static final String getPathFileName(String fileName) throws IOException + { + String pathFileName = "/" + fileName; + return pathFileName; + } + + /** + * 鏂囦欢澶у皬鏍¢獙 + * + * @param file 涓婁紶鐨勬枃浠� + * @throws FileSizeLimitExceededException 濡傛灉瓒呭嚭鏈�澶уぇ灏� + * @throws InvalidExtensionException 鏂囦欢鏍¢獙寮傚父 + */ + public static final void assertAllowed(MultipartFile file, String[] allowedExtension) + throws FileSizeLimitExceededException, InvalidExtensionException + { + long size = file.getSize(); + if (size > DEFAULT_MAX_SIZE) + { + throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024); + } + + String fileName = file.getOriginalFilename(); + String extension = FileTypeUtils.getExtension(file); + if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) + { + if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) + { + throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension, + fileName); + } + else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) + { + throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, + fileName); + } + else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) + { + throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension, + fileName); + } + else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) + { + throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension, + fileName); + } + else + { + throw new InvalidExtensionException(allowedExtension, extension, fileName); + } + } + } + + /** + * 鍒ゆ柇MIME绫诲瀷鏄惁鏄厑璁哥殑MIME绫诲瀷 + * + * @param extension 涓婁紶鏂囦欢绫诲瀷 + * @param allowedExtension 鍏佽涓婁紶鏂囦欢绫诲瀷 + * @return true/false + */ + public static final boolean isAllowedExtension(String extension, String[] allowedExtension) + { + for (String str : allowedExtension) + { + if (str.equalsIgnoreCase(extension)) + { + return true; + } + } + return false; + } +} \ No newline at end of file diff --git "a/\350\257\264\346\230\216.txt" "b/\350\257\264\346\230\216.txt" new file mode 100644 index 0000000..2e7370f --- /dev/null +++ "b/\350\257\264\346\230\216.txt" @@ -0,0 +1,23 @@ +se-system-dev.yml锛屾坊鍔� + +# 鏈湴鏂囦欢涓婁紶 +file: + domain: http://127.0.0.1:9300 + path: D:/se/uploadPath + prefix: /statics + +# FastDFS閰嶇疆 +fdfs: + domain: http://8.129.231.12 + soTimeout: 3000 + connectTimeout: 2000 + trackerList: 8.129.231.12:22122 + +# Minio閰嶇疆 +minio: + url: http://8.129.231.12:9000 + accessKey: minioadmin + secretKey: minioadmin + bucketName: test + + -- Gitblit v1.9.3