From 18fc9de0765ac1359b6c38d0f045536a3f10c2bc Mon Sep 17 00:00:00 2001
From: fst1996 <2411194573@qq.com>
Date: Fri, 15 Sep 2023 20:10:34 +0800
Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 47 +++++
menghang-base-file-common/pom.xml | 30 +++
.../java/com/bawei/file/domain/SysFile.java | 50 +++++
menghang-base-file-remote/pom.xml | 30 +++
.../file/remote/api/RemoteFileService.java | 29 +++
.../factory/RemoteFileFallbackFactory.java | 35 ++++
...ot.autoconfigure.AutoConfiguration.imports | 1 +
menghang-base-file-server/pom.xml | 92 +++++++++
.../com/bawei/file/BaWeiFileApplication.java | 22 +++
.../com/bawei/file/config/MinioConfig.java | 82 ++++++++
.../bawei/file/config/ResourcesConfig.java | 50 +++++
.../file/controller/SysFileController.java | 48 +++++
.../service/FastDfsSysFileServiceImpl.java | 42 ++++
.../bawei/file/service/ISysFileService.java | 20 ++
.../file/service/LocalSysFileServiceImpl.java | 50 +++++
.../file/service/MinioSysFileServiceImpl.java | 45 +++++
.../com/bawei/file/utils/FileUploadUtils.java | 180 ++++++++++++++++++
.../src/main/resources/banner.txt | 10 +
.../src/main/resources/bootstrap.yml | 25 +++
.../src/main/resources/logback.xml | 74 +++++++
pom.xml | 25 +++
21 files changed, 987 insertions(+)
create mode 100644 .gitignore
create mode 100644 menghang-base-file-common/pom.xml
create mode 100644 menghang-base-file-common/src/main/java/com/bawei/file/domain/SysFile.java
create mode 100644 menghang-base-file-remote/pom.xml
create mode 100644 menghang-base-file-remote/src/main/java/com/bawei/file/remote/api/RemoteFileService.java
create mode 100644 menghang-base-file-remote/src/main/java/com/bawei/file/remote/factory/RemoteFileFallbackFactory.java
create mode 100644 menghang-base-file-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
create mode 100644 menghang-base-file-server/pom.xml
create mode 100644 menghang-base-file-server/src/main/java/com/bawei/file/BaWeiFileApplication.java
create mode 100644 menghang-base-file-server/src/main/java/com/bawei/file/config/MinioConfig.java
create mode 100644 menghang-base-file-server/src/main/java/com/bawei/file/config/ResourcesConfig.java
create mode 100644 menghang-base-file-server/src/main/java/com/bawei/file/controller/SysFileController.java
create mode 100644 menghang-base-file-server/src/main/java/com/bawei/file/service/FastDfsSysFileServiceImpl.java
create mode 100644 menghang-base-file-server/src/main/java/com/bawei/file/service/ISysFileService.java
create mode 100644 menghang-base-file-server/src/main/java/com/bawei/file/service/LocalSysFileServiceImpl.java
create mode 100644 menghang-base-file-server/src/main/java/com/bawei/file/service/MinioSysFileServiceImpl.java
create mode 100644 menghang-base-file-server/src/main/java/com/bawei/file/utils/FileUploadUtils.java
create mode 100644 menghang-base-file-server/src/main/resources/banner.txt
create mode 100644 menghang-base-file-server/src/main/resources/bootstrap.yml
create mode 100644 menghang-base-file-server/src/main/resources/logback.xml
create mode 100644 pom.xml
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..05d7c76
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,47 @@
+######################################################################
+# Build Tools
+
+.gradle
+/build/
+!gradle/wrapper/gradle-wrapper.jar
+
+target/
+!.mvn/wrapper/maven-wrapper.jar
+
+######################################################################
+# IDE
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### JRebel ###
+rebel.xml
+### NetBeans ###
+nbproject/private/
+build/*
+nbbuild/
+dist/
+nbdist/
+.nb-gradle/
+target/
+
+######################################################################
+# Others
+*.log
+*.xml.versionsBackup
+*.swp
+
+!*/build/*.java
+!*/build/*.html
+!*/build/*.xml
diff --git a/menghang-base-file-common/pom.xml b/menghang-base-file-common/pom.xml
new file mode 100644
index 0000000..06e9dd0
--- /dev/null
+++ b/menghang-base-file-common/pom.xml
@@ -0,0 +1,30 @@
+
+
+
+ menghang-base-file
+ com.bawei
+ 3.6.0
+
+ 4.0.0
+
+ 3.6.0
+ menghang-base-file-common
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+
+
+ com.bawei
+ menghang-common-core
+ 3.6.0
+
+
+
diff --git a/menghang-base-file-common/src/main/java/com/bawei/file/domain/SysFile.java b/menghang-base-file-common/src/main/java/com/bawei/file/domain/SysFile.java
new file mode 100644
index 0000000..518679c
--- /dev/null
+++ b/menghang-base-file-common/src/main/java/com/bawei/file/domain/SysFile.java
@@ -0,0 +1,50 @@
+package com.bawei.file.domain;
+
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+
+/**
+ * 文件信息
+ *
+ * @author bawei
+ */
+public class SysFile
+{
+ /**
+ * 文件名称
+ */
+ private String name;
+
+ /**
+ * 文件地址
+ */
+ private String url;
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public String getUrl()
+ {
+ return url;
+ }
+
+ public void setUrl(String url)
+ {
+ this.url = url;
+ }
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
+ .append("name", getName())
+ .append("url", getUrl())
+ .toString();
+ }
+}
diff --git a/menghang-base-file-remote/pom.xml b/menghang-base-file-remote/pom.xml
new file mode 100644
index 0000000..2bd6040
--- /dev/null
+++ b/menghang-base-file-remote/pom.xml
@@ -0,0 +1,30 @@
+
+
+
+ menghang-base-file
+ com.bawei
+ 3.6.0
+
+ 4.0.0
+
+ 3.6.0
+ menghang-base-file-remote
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+
+
+ com.bawei
+ menghang-base-file-common
+ 3.6.0
+
+
+
diff --git a/menghang-base-file-remote/src/main/java/com/bawei/file/remote/api/RemoteFileService.java b/menghang-base-file-remote/src/main/java/com/bawei/file/remote/api/RemoteFileService.java
new file mode 100644
index 0000000..78e1930
--- /dev/null
+++ b/menghang-base-file-remote/src/main/java/com/bawei/file/remote/api/RemoteFileService.java
@@ -0,0 +1,29 @@
+package com.bawei.file.remote.api;
+
+import com.bawei.file.domain.SysFile;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestPart;
+import org.springframework.web.multipart.MultipartFile;
+import com.bawei.common.core.constant.ServiceNameConstants;
+import com.bawei.common.core.domain.R;
+import com.bawei.file.remote.factory.RemoteFileFallbackFactory;
+
+/**
+ * 文件服务
+ *
+ * @author bawei
+ */
+@FeignClient(contextId = "remoteFileService", value = ServiceNameConstants.FILE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class)
+public interface RemoteFileService
+{
+ /**
+ * 上传文件
+ *
+ * @param file 文件信息
+ * @return 结果
+ */
+ @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
+ public R upload(@RequestPart(value = "file") MultipartFile file);
+}
diff --git a/menghang-base-file-remote/src/main/java/com/bawei/file/remote/factory/RemoteFileFallbackFactory.java b/menghang-base-file-remote/src/main/java/com/bawei/file/remote/factory/RemoteFileFallbackFactory.java
new file mode 100644
index 0000000..1a6bc6f
--- /dev/null
+++ b/menghang-base-file-remote/src/main/java/com/bawei/file/remote/factory/RemoteFileFallbackFactory.java
@@ -0,0 +1,35 @@
+package com.bawei.file.remote.factory;
+
+import com.bawei.file.domain.SysFile;
+import com.bawei.file.remote.api.RemoteFileService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.cloud.openfeign.FallbackFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+import com.bawei.common.core.domain.R;
+
+/**
+ * 文件服务降级处理
+ *
+ * @author bawei
+ */
+@Component
+public class RemoteFileFallbackFactory implements FallbackFactory
+{
+ private static final Logger log = LoggerFactory.getLogger(RemoteFileFallbackFactory.class);
+
+ @Override
+ public RemoteFileService create(Throwable throwable)
+ {
+ log.error("文件服务调用失败:{}", throwable.getMessage());
+ return new RemoteFileService()
+ {
+ @Override
+ public R upload(MultipartFile file)
+ {
+ return R.fail("上传文件失败:" + throwable.getMessage());
+ }
+ };
+ }
+}
diff --git a/menghang-base-file-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/menghang-base-file-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
new file mode 100644
index 0000000..78e39fb
--- /dev/null
+++ b/menghang-base-file-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -0,0 +1 @@
+com.bawei.file.remote.factory.RemoteFileFallbackFactory
diff --git a/menghang-base-file-server/pom.xml b/menghang-base-file-server/pom.xml
new file mode 100644
index 0000000..727e095
--- /dev/null
+++ b/menghang-base-file-server/pom.xml
@@ -0,0 +1,92 @@
+
+
+
+ menghang-base-file
+ com.bawei
+ 3.6.0
+
+ 4.0.0
+
+ 3.6.0
+ menghang-base-file-server
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-discovery
+
+
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-config
+
+
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-sentinel
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-actuator
+
+
+
+
+ com.github.tobato
+ fastdfs-client
+
+
+
+
+ io.minio
+ minio
+ ${minio.version}
+
+
+
+
+ com.bawei
+ menghang-common-swagger
+ 3.6.0
+
+
+
+
+ com.bawei
+ menghang-base-file-common
+ 3.6.0
+
+
+
+
+
+ ${project.artifactId}
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+ repackage
+
+
+
+
+
+
+
diff --git a/menghang-base-file-server/src/main/java/com/bawei/file/BaWeiFileApplication.java b/menghang-base-file-server/src/main/java/com/bawei/file/BaWeiFileApplication.java
new file mode 100644
index 0000000..24f455e
--- /dev/null
+++ b/menghang-base-file-server/src/main/java/com/bawei/file/BaWeiFileApplication.java
@@ -0,0 +1,22 @@
+package com.bawei.file;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import com.bawei.common.swagger.annotation.EnableCustomSwagger2;
+
+/**
+ * 文件服务
+ *
+ * @author bawei
+ */
+@EnableCustomSwagger2
+@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
+public class BaWeiFileApplication
+{
+ public static void main(String[] args)
+ {
+ SpringApplication.run(BaWeiFileApplication.class, args);
+ System.out.println("(♥◠‿◠)ノ゙ 文件服务模块启动成功 ლ(´ڡ`ლ)゙ ");
+ }
+}
diff --git a/menghang-base-file-server/src/main/java/com/bawei/file/config/MinioConfig.java b/menghang-base-file-server/src/main/java/com/bawei/file/config/MinioConfig.java
new file mode 100644
index 0000000..dc91b09
--- /dev/null
+++ b/menghang-base-file-server/src/main/java/com/bawei/file/config/MinioConfig.java
@@ -0,0 +1,82 @@
+package com.bawei.file.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import io.minio.MinioClient;
+
+/**
+ * Minio 配置信息
+ *
+ * @author bawei
+ */
+@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/menghang-base-file-server/src/main/java/com/bawei/file/config/ResourcesConfig.java b/menghang-base-file-server/src/main/java/com/bawei/file/config/ResourcesConfig.java
new file mode 100644
index 0000000..57f5977
--- /dev/null
+++ b/menghang-base-file-server/src/main/java/com/bawei/file/config/ResourcesConfig.java
@@ -0,0 +1,50 @@
+package com.bawei.file.config;
+
+import java.io.File;
+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;
+
+/**
+ * 通用映射配置
+ *
+ * @author bawei
+ */
+@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");
+ }
+}
diff --git a/menghang-base-file-server/src/main/java/com/bawei/file/controller/SysFileController.java b/menghang-base-file-server/src/main/java/com/bawei/file/controller/SysFileController.java
new file mode 100644
index 0000000..8b878ca
--- /dev/null
+++ b/menghang-base-file-server/src/main/java/com/bawei/file/controller/SysFileController.java
@@ -0,0 +1,48 @@
+package com.bawei.file.controller;
+
+import com.bawei.file.domain.SysFile;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+import com.bawei.common.core.domain.R;
+import com.bawei.common.core.utils.file.FileUtils;
+import com.bawei.file.service.ISysFileService;
+
+/**
+ * 文件请求处理
+ *
+ * @author bawei
+ */
+@RestController
+public class SysFileController
+{
+ private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
+
+ @Autowired
+ private ISysFileService sysFileService;
+
+ /**
+ * 文件上传请求
+ */
+ @PostMapping("upload")
+ public R 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());
+ }
+ }
+}
diff --git a/menghang-base-file-server/src/main/java/com/bawei/file/service/FastDfsSysFileServiceImpl.java b/menghang-base-file-server/src/main/java/com/bawei/file/service/FastDfsSysFileServiceImpl.java
new file mode 100644
index 0000000..5809cc7
--- /dev/null
+++ b/menghang-base-file-server/src/main/java/com/bawei/file/service/FastDfsSysFileServiceImpl.java
@@ -0,0 +1,42 @@
+package com.bawei.file.service;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+import com.github.tobato.fastdfs.domain.fdfs.StorePath;
+import com.github.tobato.fastdfs.service.FastFileStorageClient;
+import com.bawei.common.core.utils.file.FileTypeUtils;
+
+/**
+ * FastDFS 文件存储
+ *
+ * @author bawei
+ */
+@Service
+public class FastDfsSysFileServiceImpl implements ISysFileService
+{
+ /**
+ * 域名或本机访问地址
+ */
+ @Value("${fdfs.domain}")
+ public String domain;
+
+ @Autowired
+ private FastFileStorageClient storageClient;
+
+ /**
+ * FastDfs文件上传接口
+ *
+ * @param file 上传的文件
+ * @return 访问地址
+ * @throws Exception
+ */
+ @Override
+ public String uploadFile(MultipartFile file) throws Exception
+ {
+ StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
+ FileTypeUtils.getExtension(file), null);
+ return domain + "/" + storePath.getFullPath();
+ }
+}
diff --git a/menghang-base-file-server/src/main/java/com/bawei/file/service/ISysFileService.java b/menghang-base-file-server/src/main/java/com/bawei/file/service/ISysFileService.java
new file mode 100644
index 0000000..941e2ca
--- /dev/null
+++ b/menghang-base-file-server/src/main/java/com/bawei/file/service/ISysFileService.java
@@ -0,0 +1,20 @@
+package com.bawei.file.service;
+
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * 文件上传接口
+ *
+ * @author bawei
+ */
+public interface ISysFileService
+{
+ /**
+ * 文件上传接口
+ *
+ * @param file 上传的文件
+ * @return 访问地址
+ * @throws Exception
+ */
+ public String uploadFile(MultipartFile file) throws Exception;
+}
diff --git a/menghang-base-file-server/src/main/java/com/bawei/file/service/LocalSysFileServiceImpl.java b/menghang-base-file-server/src/main/java/com/bawei/file/service/LocalSysFileServiceImpl.java
new file mode 100644
index 0000000..db04461
--- /dev/null
+++ b/menghang-base-file-server/src/main/java/com/bawei/file/service/LocalSysFileServiceImpl.java
@@ -0,0 +1,50 @@
+package com.bawei.file.service;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Primary;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+import com.bawei.file.utils.FileUploadUtils;
+
+/**
+ * 本地文件存储
+ *
+ * @author bawei
+ */
+@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/menghang-base-file-server/src/main/java/com/bawei/file/service/MinioSysFileServiceImpl.java b/menghang-base-file-server/src/main/java/com/bawei/file/service/MinioSysFileServiceImpl.java
new file mode 100644
index 0000000..3c47fbb
--- /dev/null
+++ b/menghang-base-file-server/src/main/java/com/bawei/file/service/MinioSysFileServiceImpl.java
@@ -0,0 +1,45 @@
+package com.bawei.file.service;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+import com.bawei.file.config.MinioConfig;
+import com.bawei.file.utils.FileUploadUtils;
+import io.minio.MinioClient;
+import io.minio.PutObjectArgs;
+
+/**
+ * Minio 文件存储
+ *
+ * @author bawei
+ */
+@Service
+public class MinioSysFileServiceImpl implements ISysFileService
+{
+ @Autowired
+ private MinioConfig minioConfig;
+
+ @Autowired
+ private MinioClient client;
+
+ /**
+ * 本地文件上传接口
+ *
+ * @param file 上传的文件
+ * @return 访问地址
+ * @throws Exception
+ */
+ @Override
+ public String uploadFile(MultipartFile file) throws Exception
+ {
+ String fileName = FileUploadUtils.extractFilename(file);
+ PutObjectArgs args = PutObjectArgs.builder()
+ .bucket(minioConfig.getBucketName())
+ .object(fileName)
+ .stream(file.getInputStream(), file.getSize(), -1)
+ .contentType(file.getContentType())
+ .build();
+ client.putObject(args);
+ return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
+ }
+}
diff --git a/menghang-base-file-server/src/main/java/com/bawei/file/utils/FileUploadUtils.java b/menghang-base-file-server/src/main/java/com/bawei/file/utils/FileUploadUtils.java
new file mode 100644
index 0000000..376caa0
--- /dev/null
+++ b/menghang-base-file-server/src/main/java/com/bawei/file/utils/FileUploadUtils.java
@@ -0,0 +1,180 @@
+package com.bawei.file.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.Objects;
+import org.apache.commons.io.FilenameUtils;
+import org.springframework.web.multipart.MultipartFile;
+import com.bawei.common.core.exception.file.FileNameLengthLimitExceededException;
+import com.bawei.common.core.exception.file.FileSizeLimitExceededException;
+import com.bawei.common.core.exception.file.InvalidExtensionException;
+import com.bawei.common.core.utils.DateUtils;
+import com.bawei.common.core.utils.StringUtils;
+import com.bawei.common.core.utils.file.FileTypeUtils;
+import com.bawei.common.core.utils.file.MimeTypeUtils;
+import com.bawei.common.core.utils.uuid.Seq;
+
+/**
+ * 文件上传工具类
+ *
+ * @author bawei
+ */
+public class FileUploadUtils
+{
+ /**
+ * 默认大小 50M
+ */
+ public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
+
+ /**
+ * 默认的文件名最大长度 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 (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;
+ }
+}
diff --git a/menghang-base-file-server/src/main/resources/banner.txt b/menghang-base-file-server/src/main/resources/banner.txt
new file mode 100644
index 0000000..27cacb9
--- /dev/null
+++ b/menghang-base-file-server/src/main/resources/banner.txt
@@ -0,0 +1,10 @@
+Spring Boot Version: ${spring-boot.version}
+Spring Application Name: ${spring.application.name}
+ _ __ _ _
+ (_) / _|(_)| |
+ _ __ _ _ ___ _ _ _ ______ | |_ _ | | ___
+| '__|| | | | / _ \ | | | || ||______|| _|| || | / _ \
+| | | |_| || (_) || |_| || | | | | || || __/
+|_| \__,_| \___/ \__, ||_| |_| |_||_| \___|
+ __/ |
+ |___/
\ No newline at end of file
diff --git a/menghang-base-file-server/src/main/resources/bootstrap.yml b/menghang-base-file-server/src/main/resources/bootstrap.yml
new file mode 100644
index 0000000..f8c4f76
--- /dev/null
+++ b/menghang-base-file-server/src/main/resources/bootstrap.yml
@@ -0,0 +1,25 @@
+# Tomcat
+server:
+ port: 9300
+
+# Spring
+spring:
+ application:
+ # 应用名称
+ name: bawei-file
+ profiles:
+ # 环境配置
+ active: dev
+ cloud:
+ nacos:
+ discovery:
+ # 服务注册地址
+ server-addr: 192.168.111.130:8848
+ config:
+ # 配置中心地址
+ server-addr: 192.168.111.130:8848
+ # 配置文件格式
+ file-extension: yml
+ # 共享配置
+ shared-configs:
+ - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
diff --git a/menghang-base-file-server/src/main/resources/logback.xml b/menghang-base-file-server/src/main/resources/logback.xml
new file mode 100644
index 0000000..55159ec
--- /dev/null
+++ b/menghang-base-file-server/src/main/resources/logback.xml
@@ -0,0 +1,74 @@
+
+
+
+
+
+
+
+
+
+
+ ${log.pattern}
+
+
+
+
+
+ ${log.path}/info.log
+
+
+
+ ${log.path}/info.%d{yyyy-MM-dd}.log
+
+ 60
+
+
+ ${log.pattern}
+
+
+
+ INFO
+
+ ACCEPT
+
+ DENY
+
+
+
+
+ ${log.path}/error.log
+
+
+
+ ${log.path}/error.%d{yyyy-MM-dd}.log
+
+ 60
+
+
+ ${log.pattern}
+
+
+
+ ERROR
+
+ ACCEPT
+
+ DENY
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..82dee68
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,25 @@
+
+
+
+ com.bawei
+ menghang-base
+ 3.6.0
+
+ 4.0.0
+
+ 3.6.0
+ menghang-base-file
+ pom
+
+
+ menghang-base-file文件服务
+
+
+ menghang-base-file-common
+ menghang-base-file-remote
+ menghang-base-file-server
+
+
+