Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
程序猿小郑
microservices-platform
提交
9b3e424b
microservices-platform
项目概览
程序猿小郑
/
microservices-platform
与 Fork 源项目一致
Fork自
zlt2000 / microservices-platform
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
microservices-platform
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9b3e424b
编写于
4月 16, 2019
作者:
zlt2000
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
重构`file-center`的自动化配置,并增加`FastDFS`的实现
上级
2e3a0308
变更
23
显示空白变更内容
内联
并排
Showing
23 changed file
with
428 addition
and
306 deletion
+428
-306
pom.xml
pom.xml
+8
-2
zlt-business/file-center/pom.xml
zlt-business/file-center/pom.xml
+16
-10
zlt-business/file-center/src/main/java/com/central/FileCenterApp.java
.../file-center/src/main/java/com/central/FileCenterApp.java
+4
-2
zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSAutoConfigure.java
.../java/com/central/file/config/AliyunOSSAutoConfigure.java
+61
-0
zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSConfig.java
...rc/main/java/com/central/file/config/AliyunOSSConfig.java
+0
-34
zlt-business/file-center/src/main/java/com/central/file/config/FastdfsAutoConfigure.java
...in/java/com/central/file/config/FastdfsAutoConfigure.java
+53
-0
zlt-business/file-center/src/main/java/com/central/file/config/OssServiceFactory.java
.../main/java/com/central/file/config/OssServiceFactory.java
+0
-40
zlt-business/file-center/src/main/java/com/central/file/config/PasswordConfig.java
...src/main/java/com/central/file/config/PasswordConfig.java
+0
-12
zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSSAutoConfigure.java
...n/java/com/central/file/config/QiniuOSSAutoConfigure.java
+101
-0
zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java
...main/java/com/central/file/controller/FileController.java
+7
-16
zlt-business/file-center/src/main/java/com/central/file/model/FileType.java
...center/src/main/java/com/central/file/model/FileType.java
+0
-13
zlt-business/file-center/src/main/java/com/central/file/properties/FdfsProperties.java
...main/java/com/central/file/properties/FdfsProperties.java
+16
-0
zlt-business/file-center/src/main/java/com/central/file/properties/FileServerProperties.java
...ava/com/central/file/properties/FileServerProperties.java
+33
-0
zlt-business/file-center/src/main/java/com/central/file/properties/OssProperties.java
.../main/java/com/central/file/properties/OssProperties.java
+32
-0
zlt-business/file-center/src/main/java/com/central/file/service/IFileService.java
.../src/main/java/com/central/file/service/IFileService.java
+3
-1
zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java
...a/com/central/file/service/impl/AbstractIFileService.java
+58
-42
zlt-business/file-center/src/main/java/com/central/file/service/impl/AliyunOssServiceImplI.java
.../com/central/file/service/impl/AliyunOssServiceImplI.java
+0
-42
zlt-business/file-center/src/main/java/com/central/file/service/impl/QiniuOssServiceImplI.java
...a/com/central/file/service/impl/QiniuOssServiceImplI.java
+0
-72
zlt-business/file-center/src/main/java/com/central/file/utils/FileUtil.java
...center/src/main/java/com/central/file/utils/FileUtil.java
+2
-1
zlt-business/file-center/src/main/resources/application.yml
zlt-business/file-center/src/main/resources/application.yml
+19
-16
zlt-config/src/main/resources/application-dev.properties
zlt-config/src/main/resources/application-dev.properties
+5
-1
zlt-config/src/main/resources/application-prod.properties
zlt-config/src/main/resources/application-prod.properties
+5
-1
zlt-config/src/main/resources/application-test.properties
zlt-config/src/main/resources/application-test.properties
+5
-1
未找到文件。
pom.xml
浏览文件 @
9b3e424b
...
...
@@ -29,13 +29,14 @@
<kaptcha.version>
0.0.9
</kaptcha.version>
<hutool.version>
4.3.1
</hutool.version>
<mybatis-plus-boot-starter.version>
3.0.6
</mybatis-plus-boot-starter.version>
<aliyun-sdk-oss>
3.4.
0
</aliyun-sdk-oss>
<qiniu-java-sdk>
7.2.1
7
</qiniu-java-sdk>
<aliyun-sdk-oss>
3.4.
2
</aliyun-sdk-oss>
<qiniu-java-sdk>
7.2.1
8
</qiniu-java-sdk>
<easypoi.version>
4.0.0
</easypoi.version>
<spring-boot-admin.version>
2.0.4
</spring-boot-admin.version>
<velocity.version>
1.7
</velocity.version>
<commons-configuration.version>
1.10
</commons-configuration.version>
<txlcn.version>
5.0.2.RELEASE
</txlcn.version>
<fastdfs-client.version>
1.26.5
</fastdfs-client.version>
<platform-bom>
Cairo-SR3
</platform-bom>
<spring-cloud-alibaba-dependencies.version>
0.2.1.RELEASE
</spring-cloud-alibaba-dependencies.version>
<spring-boot-dependencies.version>
2.0.8.RELEASE
</spring-boot-dependencies.version>
...
...
@@ -285,6 +286,11 @@
<artifactId>
sharding-jdbc-spring-boot-starter
</artifactId>
<version>
${sharding-sphere.version}
</version>
</dependency>
<dependency>
<groupId>
com.github.tobato
</groupId>
<artifactId>
fastdfs-client
</artifactId>
<version>
${fastdfs-client.version}
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
...
...
zlt-business/file-center/pom.xml
浏览文件 @
9b3e424b
...
...
@@ -47,16 +47,6 @@
<artifactId>
zlt-swagger2-spring-boot-starter
</artifactId>
</dependency>
<dependency>
<groupId>
com.aliyun.oss
</groupId>
<artifactId>
aliyun-sdk-oss
</artifactId>
</dependency>
<!-- 七牛依赖 -->
<dependency>
<groupId>
com.qiniu
</groupId>
<artifactId>
qiniu-java-sdk
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
...
...
@@ -71,6 +61,22 @@
<groupId>
io.micrometer
</groupId>
<artifactId>
micrometer-registry-prometheus
</artifactId>
</dependency>
<!-- 阿里云oss -->
<dependency>
<groupId>
com.aliyun.oss
</groupId>
<artifactId>
aliyun-sdk-oss
</artifactId>
</dependency>
<!-- 七牛oss -->
<dependency>
<groupId>
com.qiniu
</groupId>
<artifactId>
qiniu-java-sdk
</artifactId>
</dependency>
<!-- fastDFS -->
<dependency>
<groupId>
com.github.tobato
</groupId>
<artifactId>
fastdfs-client
</artifactId>
</dependency>
</dependencies>
<build>
...
...
zlt-business/file-center/src/main/java/com/central/FileCenterApp.java
浏览文件 @
9b3e424b
package
com.central
;
import
com.central.file.properties.FileServerProperties
;
import
com.central.file.properties.OssProperties
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.cloud.client.discovery.EnableDiscoveryClient
;
/**
...
...
@@ -9,11 +12,10 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
* @author 作者 owen E-mail: 624191343@qq.com
*/
@EnableDiscoveryClient
@EnableConfigurationProperties
(
FileServerProperties
.
class
)
@SpringBootApplication
public
class
FileCenterApp
{
public
static
void
main
(
String
[]
args
)
{
// 固定端口
SpringApplication
.
run
(
FileCenterApp
.
class
,
args
);
}
}
\ No newline at end of file
zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSAutoConfigure.java
0 → 100644
浏览文件 @
9b3e424b
package
com.central.file.config
;
import
com.aliyun.oss.common.auth.DefaultCredentialProvider
;
import
com.central.file.model.FileInfo
;
import
com.central.file.properties.FileServerProperties
;
import
com.central.file.service.impl.AbstractIFileService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
com.aliyun.oss.OSSClient
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
/**
* 阿里云配置
*
* @author 作者 owen E-mail: 624191343@qq.com
*/
@Configuration
@ConditionalOnProperty
(
name
=
"zlt.file-server.type"
,
havingValue
=
"aliyun"
)
public
class
AliyunOSSAutoConfigure
{
@Autowired
private
FileServerProperties
fileProperties
;
/**
* 阿里云文件存储client
* 只有配置了aliyun.oss.access-key才可以使用
*/
@Bean
public
OSSClient
ossClient
()
{
OSSClient
ossClient
=
new
OSSClient
(
fileProperties
.
getOss
().
getEndpoint
()
,
new
DefaultCredentialProvider
(
fileProperties
.
getOss
().
getAccessKey
(),
fileProperties
.
getOss
().
getAccessKeySecret
())
,
null
);
return
ossClient
;
}
@Service
public
class
AliyunOssServiceImpl
extends
AbstractIFileService
{
@Autowired
private
OSSClient
ossClient
;
@Override
protected
String
fileType
()
{
return
fileProperties
.
getType
();
}
@Override
protected
void
uploadFile
(
MultipartFile
file
,
FileInfo
fileInfo
)
throws
Exception
{
ossClient
.
putObject
(
fileProperties
.
getOss
().
getBucketName
(),
fileInfo
.
getName
(),
file
.
getInputStream
());
fileInfo
.
setUrl
(
fileProperties
.
getOss
().
getDomain
()
+
"/"
+
fileInfo
.
getName
());
}
@Override
protected
boolean
deleteFile
(
FileInfo
fileInfo
)
{
ossClient
.
deleteObject
(
fileProperties
.
getOss
().
getBucketName
(),
fileInfo
.
getName
());
return
true
;
}
}
}
zlt-business/file-center/src/main/java/com/central/file/config/AliyunOSSConfig.java
已删除
100644 → 0
浏览文件 @
2e3a0308
package
com.central.file.config
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
com.aliyun.oss.OSSClient
;
/**
* 阿里云配置
*
* @author 作者 owen E-mail: 624191343@qq.com
*/
@Configuration
public
class
AliyunOSSConfig
{
@Value
(
"${aliyun.oss.endpoint:xxxxx}"
)
private
String
endpoint
;
@Value
(
"${aliyun.oss.access-key:xxxxx}"
)
private
String
accessKeyId
;
@Value
(
"${aliyun.oss.accessKeySecret:xxxxx}"
)
private
String
accessKeySecret
;
/**
* 阿里云文件存储client
* 只有配置了aliyun.oss.access-key才可以使用
*/
@Bean
@ConditionalOnProperty
(
name
=
"aliyun.oss.access-key"
,
matchIfMissing
=
true
)
public
OSSClient
ossClient
()
{
OSSClient
ossClient
=
new
OSSClient
(
endpoint
,
accessKeyId
,
accessKeySecret
);
return
ossClient
;
}
}
zlt-business/file-center/src/main/java/com/central/file/config/FastdfsAutoConfigure.java
0 → 100644
浏览文件 @
9b3e424b
package
com.central.file.config
;
import
cn.hutool.core.util.StrUtil
;
import
com.central.file.model.FileInfo
;
import
com.central.file.properties.FileServerProperties
;
import
com.central.file.service.impl.AbstractIFileService
;
import
com.github.tobato.fastdfs.domain.fdfs.StorePath
;
import
com.github.tobato.fastdfs.service.FastFileStorageClient
;
import
org.apache.commons.io.FilenameUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
/**
* FastDFS配置
*
* @author zlt
*/
@Configuration
@ConditionalOnProperty
(
name
=
"zlt.file-server.type"
,
havingValue
=
"fastdfs"
)
public
class
FastdfsAutoConfigure
{
@Autowired
private
FileServerProperties
fileProperties
;
@Service
public
class
FastdfsServiceImpl
extends
AbstractIFileService
{
@Autowired
private
FastFileStorageClient
storageClient
;
@Override
protected
String
fileType
()
{
return
fileProperties
.
getType
();
}
@Override
protected
void
uploadFile
(
MultipartFile
file
,
FileInfo
fileInfo
)
throws
Exception
{
StorePath
storePath
=
storageClient
.
uploadFile
(
file
.
getInputStream
(),
file
.
getSize
(),
FilenameUtils
.
getExtension
(
file
.
getOriginalFilename
()),
null
);
fileInfo
.
setUrl
(
fileProperties
.
getFdfs
().
getWebUrl
()
+
"/"
+
storePath
.
getFullPath
());
fileInfo
.
setPath
(
storePath
.
getFullPath
());
}
@Override
protected
boolean
deleteFile
(
FileInfo
fileInfo
)
{
if
(
fileInfo
!=
null
&&
StrUtil
.
isNotEmpty
(
fileInfo
.
getPath
()))
{
StorePath
storePath
=
StorePath
.
parseFromUrl
(
fileInfo
.
getPath
());
storageClient
.
deleteFile
(
storePath
.
getGroup
(),
storePath
.
getPath
());
}
return
true
;
}
}
}
zlt-business/file-center/src/main/java/com/central/file/config/OssServiceFactory.java
已删除
100644 → 0
浏览文件 @
2e3a0308
package
com.central.file.config
;
import
java.util.EnumMap
;
import
java.util.HashMap
;
import
java.util.Map
;
import
javax.annotation.PostConstruct
;
import
com.central.file.service.IFileService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Configuration
;
import
com.central.file.model.FileType
;
/**
* FileService工厂<br>
* 将各个实现类放入map
*
* @author 作者 owen E-mail: 624191343@qq.com
*/
@Configuration
public
class
OssServiceFactory
{
private
Map
<
FileType
,
IFileService
>
map
=
new
EnumMap
<>(
FileType
.
class
);
@Autowired
private
IFileService
aliyunOssServiceImpl
;
@Autowired
private
IFileService
qiniuOssServiceImpl
;
@PostConstruct
public
void
init
()
{
map
.
put
(
FileType
.
ALIYUN
,
aliyunOssServiceImpl
);
map
.
put
(
FileType
.
QINIU
,
qiniuOssServiceImpl
);
}
public
IFileService
getFileService
(
String
fileType
)
{
return
map
.
get
(
FileType
.
valueOf
(
fileType
));
}
}
zlt-business/file-center/src/main/java/com/central/file/config/PasswordConfig.java
已删除
100644 → 0
浏览文件 @
2e3a0308
package
com.central.file.config
;
import
com.central.common.config.DefaultPasswordConfig
;
import
org.springframework.context.annotation.Configuration
;
/**
* @author zlt
* @date 2019/1/2
*/
@Configuration
public
class
PasswordConfig
extends
DefaultPasswordConfig
{
}
zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSS
Config
.java
→
zlt-business/file-center/src/main/java/com/central/file/config/QiniuOSS
AutoConfigure
.java
浏览文件 @
9b3e424b
package
com.central.file.config
;
import
org.springframework.beans.factory.annotation.Value
;
import
com.central.file.model.FileInfo
;
import
com.central.file.properties.FileServerProperties
;
import
com.central.file.service.impl.AbstractIFileService
;
import
com.qiniu.common.QiniuException
;
import
com.qiniu.http.Response
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -8,6 +14,8 @@ import com.qiniu.common.Zone;
import
com.qiniu.storage.BucketManager
;
import
com.qiniu.storage.UploadManager
;
import
com.qiniu.util.Auth
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
/**
* 七牛云配置
...
...
@@ -15,11 +23,10 @@ import com.qiniu.util.Auth;
* @author 作者 owen E-mail: 624191343@qq.com
*/
@Configuration
public
class
QiniuOSSConfig
{
@Value
(
"${qiniu.oss.access-key:xxxxx}"
)
private
String
accessKeyId
;
@Value
(
"${qiniu.oss.accessKeySecret:xxxxx}"
)
private
String
accessKeySecret
;
@ConditionalOnProperty
(
name
=
"zlt.file-server.type"
,
havingValue
=
"qiniu"
)
public
class
QiniuOSSAutoConfigure
{
@Autowired
private
FileServerProperties
fileProperties
;
/**
* 华东机房
...
...
@@ -44,7 +51,7 @@ public class QiniuOSSConfig {
*/
@Bean
public
Auth
auth
()
{
return
Auth
.
create
(
accessKeyId
,
accessKeySecret
);
return
Auth
.
create
(
fileProperties
.
getOss
().
getAccessKey
(),
fileProperties
.
getOss
().
getAccessKeySecret
());
}
/**
...
...
@@ -54,4 +61,41 @@ public class QiniuOSSConfig {
public
BucketManager
bucketManager
()
{
return
new
BucketManager
(
auth
(),
qiniuConfig
());
}
@Service
public
class
QiniuOssServiceImpl
extends
AbstractIFileService
{
@Autowired
private
UploadManager
uploadManager
;
@Autowired
private
BucketManager
bucketManager
;
@Autowired
private
Auth
auth
;
@Override
protected
String
fileType
()
{
return
fileProperties
.
getType
();
}
@Override
protected
void
uploadFile
(
MultipartFile
file
,
FileInfo
fileInfo
)
throws
Exception
{
// 调用put方法上传
uploadManager
.
put
(
file
.
getBytes
(),
fileInfo
.
getName
(),
auth
.
uploadToken
(
fileProperties
.
getOss
().
getBucketName
()));
fileInfo
.
setUrl
(
fileProperties
.
getOss
().
getEndpoint
()
+
"/"
+
fileInfo
.
getName
());
fileInfo
.
setPath
(
fileProperties
.
getOss
().
getEndpoint
()
+
"/"
+
fileInfo
.
getName
());
}
@Override
protected
boolean
deleteFile
(
FileInfo
fileInfo
)
{
try
{
Response
response
=
bucketManager
.
delete
(
fileProperties
.
getOss
().
getBucketName
(),
fileInfo
.
getPath
());
int
retry
=
0
;
while
(
response
.
needRetry
()
&&
retry
++
<
3
)
{
response
=
bucketManager
.
delete
(
fileProperties
.
getOss
().
getBucketName
(),
fileInfo
.
getPath
());
}
}
catch
(
QiniuException
e
)
{
return
false
;
}
return
true
;
}
}
}
zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java
浏览文件 @
9b3e424b
...
...
@@ -4,7 +4,6 @@ import java.util.Map;
import
com.central.common.model.Result
;
import
com.central.file.service.IFileService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.DeleteMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
...
...
@@ -14,21 +13,19 @@ import org.springframework.web.bind.annotation.RestController;
import
org.springframework.web.multipart.MultipartFile
;
import
com.central.common.model.PageResult
;
import
com.central.file.config.OssServiceFactory
;
import
com.central.file.model.FileInfo
;
import
com.central.file.model.FileType
;
import
javax.annotation.Resource
;
/**
* 文件上传 同步oss db双写 目前仅实现了阿里云,七牛云
* 参考src/main/view/upload.html
* 文件上传
*
* @author 作者 owen E-mail: 624191343@qq.com
*/
@RestController
public
class
FileController
{
@Autowired
private
OssServiceFactory
fileServiceFactory
;
@Resource
private
IFileService
fileService
;
/**
* 文件上传
...
...
@@ -40,8 +37,6 @@ public class FileController {
*/
@PostMapping
(
"/files-anon"
)
public
FileInfo
upload
(
@RequestParam
(
"file"
)
MultipartFile
file
)
throws
Exception
{
String
fileType
=
FileType
.
QINIU
.
toString
();
IFileService
fileService
=
fileServiceFactory
.
getFileService
(
fileType
);
return
fileService
.
upload
(
file
);
}
...
...
@@ -53,11 +48,7 @@ public class FileController {
@DeleteMapping
(
"/files/{id}"
)
public
Result
delete
(
@PathVariable
String
id
)
{
try
{
FileInfo
fileInfo
=
fileServiceFactory
.
getFileService
(
FileType
.
QINIU
.
toString
()).
getById
(
id
);
if
(
fileInfo
!=
null
)
{
IFileService
fileService
=
fileServiceFactory
.
getFileService
(
fileInfo
.
getSource
());
fileService
.
removeById
(
fileInfo
);
}
fileService
.
delete
(
id
);
return
Result
.
succeed
(
"操作成功"
);
}
catch
(
Exception
ex
)
{
return
Result
.
failed
(
"操作失败"
);
...
...
@@ -72,6 +63,6 @@ public class FileController {
*/
@GetMapping
(
"/files"
)
public
PageResult
<
FileInfo
>
findFiles
(
@RequestParam
Map
<
String
,
Object
>
params
)
{
return
fileService
Factory
.
getFileService
(
FileType
.
QINIU
.
toString
())
.
findList
(
params
);
return
fileService
.
findList
(
params
);
}
}
zlt-business/file-center/src/main/java/com/central/file/model/FileType.java
已删除
100644 → 0
浏览文件 @
2e3a0308
package
com.central.file.model
;
/**
* 仅支持阿里云 oss ,七牛云等
*
* @author 作者 owen E-mail: 624191343@qq.com
*/
public
enum
FileType
{
//七牛
QINIU
,
//阿里云
ALIYUN
}
zlt-business/file-center/src/main/java/com/central/file/properties/FdfsProperties.java
0 → 100644
浏览文件 @
9b3e424b
package
com.central.file.properties
;
import
lombok.Getter
;
import
lombok.Setter
;
/**
* @author zlt
*/
@Setter
@Getter
public
class
FdfsProperties
{
/**
* fastdfs的http访问地址
*/
private
String
webUrl
;
}
zlt-business/file-center/src/main/java/com/central/file/properties/FileServerProperties.java
0 → 100644
浏览文件 @
9b3e424b
package
com.central.file.properties
;
import
lombok.Getter
;
import
lombok.Setter
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.cloud.context.config.annotation.RefreshScope
;
/**
* @author zlt
*/
@Setter
@Getter
@ConfigurationProperties
(
prefix
=
"zlt.file-server"
)
@RefreshScope
public
class
FileServerProperties
{
/**
* 为以下3个值,指定不同的自动化配置
* qiniu:七牛oss
* aliyun:阿里云oss
* fastdfs:本地部署的fastDFS
*/
private
String
type
;
/**
* oss配置
*/
OssProperties
oss
=
new
OssProperties
();
/**
* fastDFS配置
*/
FdfsProperties
fdfs
=
new
FdfsProperties
();
}
zlt-business/file-center/src/main/java/com/central/file/properties/OssProperties.java
0 → 100644
浏览文件 @
9b3e424b
package
com.central.file.properties
;
import
lombok.Getter
;
import
lombok.Setter
;
/**
* @author zlt
*/
@Setter
@Getter
public
class
OssProperties
{
/**
* 密钥key
*/
private
String
accessKey
;
/**
* 密钥密码
*/
private
String
accessKeySecret
;
/**
* 端点
*/
private
String
endpoint
;
/**
* bucket名称
*/
private
String
bucketName
;
/**
* 说明
*/
private
String
domain
;
}
zlt-business/file-center/src/main/java/com/central/file/service/IFileService.java
浏览文件 @
9b3e424b
...
...
@@ -9,7 +9,7 @@ import org.springframework.web.multipart.MultipartFile;
import
com.central.file.model.FileInfo
;
/**
* 文件service
目前仅支持阿里云oss,七牛云
* 文件service
*
* @author 作者 owen E-mail: 624191343@qq.com
*/
...
...
@@ -17,4 +17,6 @@ public interface IFileService extends IService<FileInfo> {
FileInfo
upload
(
MultipartFile
file
)
throws
Exception
;
PageResult
<
FileInfo
>
findList
(
Map
<
String
,
Object
>
params
);
void
delete
(
String
id
);
}
zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java
浏览文件 @
9b3e424b
...
...
@@ -11,7 +11,6 @@ import org.springframework.web.multipart.MultipartFile;
import
com.central.common.model.PageResult
;
import
com.central.file.mapper.FileMapper
;
import
com.central.file.model.FileInfo
;
import
com.central.file.model.FileType
;
import
com.central.file.service.IFileService
;
import
com.central.file.utils.FileUtil
;
...
...
@@ -19,25 +18,29 @@ import lombok.extern.slf4j.Slf4j;
/**
* AbstractIFileService 抽取类
* 根据
filetype 实例化具体oss
对象
* 根据
zlt.file-server.type 实例化具体
对象
*
* @author 作者 owen E-mail: 624191343@qq.com
*/
@Slf4j
public
abstract
class
AbstractIFileService
extends
ServiceImpl
<
FileMapper
,
FileInfo
>
implements
IFileService
{
private
static
final
String
FILE_SPLIT
=
"."
;
@Override
public
FileInfo
upload
(
MultipartFile
file
)
throws
Exception
{
public
FileInfo
upload
(
MultipartFile
file
)
throws
Exception
{
FileInfo
fileInfo
=
FileUtil
.
getFileInfo
(
file
);
FileInfo
oldFileInfo
=
baseMapper
.
selectById
(
fileInfo
.
getId
());
if
(
oldFileInfo
!=
null
)
{
return
oldFileInfo
;
}
if
(!
fileInfo
.
getName
().
contains
(
"."
))
{
if
(!
fileInfo
.
getName
().
contains
(
FILE_SPLIT
))
{
throw
new
IllegalArgumentException
(
"缺少后缀名"
);
}
uploadFile
(
file
,
fileInfo
);
fileInfo
.
setSource
(
fileType
().
name
());
// 设置文件来源
baseMapper
.
insert
(
fileInfo
);
// 将文件信息保存到数据库
// 设置文件来源
fileInfo
.
setSource
(
fileType
());
// 将文件信息保存到数据库
baseMapper
.
insert
(
fileInfo
);
return
fileInfo
;
}
...
...
@@ -47,7 +50,7 @@ public abstract class AbstractIFileService extends ServiceImpl<FileMapper, FileI
*
* @return
*/
protected
abstract
FileType
fileType
();
protected
abstract
String
fileType
();
/**
* 上传文件
...
...
@@ -57,6 +60,19 @@ public abstract class AbstractIFileService extends ServiceImpl<FileMapper, FileI
*/
protected
abstract
void
uploadFile
(
MultipartFile
file
,
FileInfo
fileInfo
)
throws
Exception
;
/**
* 删除文件
* @param id 文件id
*/
@Override
public
void
delete
(
String
id
)
{
FileInfo
fileInfo
=
baseMapper
.
selectById
(
id
);
if
(
fileInfo
!=
null
)
{
baseMapper
.
deleteById
(
fileInfo
.
getId
());
this
.
deleteFile
(
fileInfo
);
}
}
/**
* 删除文件资源
*
...
...
@@ -66,7 +82,7 @@ public abstract class AbstractIFileService extends ServiceImpl<FileMapper, FileI
protected
abstract
boolean
deleteFile
(
FileInfo
fileInfo
);
@Override
public
PageResult
<
FileInfo
>
findList
(
Map
<
String
,
Object
>
params
)
{
public
PageResult
<
FileInfo
>
findList
(
Map
<
String
,
Object
>
params
)
{
Page
<
FileInfo
>
page
=
new
Page
<>(
MapUtils
.
getInteger
(
params
,
"page"
),
MapUtils
.
getInteger
(
params
,
"limit"
));
List
<
FileInfo
>
list
=
baseMapper
.
findList
(
page
,
params
);
return
PageResult
.<
FileInfo
>
builder
().
data
(
list
).
code
(
0
).
count
(
page
.
getTotal
()).
build
();
...
...
zlt-business/file-center/src/main/java/com/central/file/service/impl/AliyunOssServiceImplI.java
已删除
100644 → 0
浏览文件 @
2e3a0308
package
com.central.file.service.impl
;
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.aliyun.oss.OSSClient
;
import
com.central.file.model.FileInfo
;
import
com.central.file.model.FileType
;
/**
* 阿里云oss存储文件
*
* @author 作者 owen E-mail: 624191343@qq.com
*/
@Service
(
"aliyunOssServiceImpl"
)
public
class
AliyunOssServiceImplI
extends
AbstractIFileService
{
@Autowired
private
OSSClient
ossClient
;
@Value
(
"${aliyun.oss.bucketName:xxxxx}"
)
private
String
bucketName
;
@Value
(
"${aliyun.oss.domain:xxxxx}"
)
private
String
domain
;
@Override
protected
FileType
fileType
()
{
return
FileType
.
ALIYUN
;
}
@Override
protected
void
uploadFile
(
MultipartFile
file
,
FileInfo
fileInfo
)
throws
Exception
{
ossClient
.
putObject
(
bucketName
,
fileInfo
.
getName
(),
file
.
getInputStream
());
fileInfo
.
setUrl
(
domain
+
"/"
+
fileInfo
.
getName
());
}
@Override
protected
boolean
deleteFile
(
FileInfo
fileInfo
)
{
ossClient
.
deleteObject
(
bucketName
,
fileInfo
.
getName
());
return
true
;
}
}
zlt-business/file-center/src/main/java/com/central/file/service/impl/QiniuOssServiceImplI.java
已删除
100644 → 0
浏览文件 @
2e3a0308
package
com.central.file.service.impl
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.InitializingBean
;
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.central.file.model.FileInfo
;
import
com.central.file.model.FileType
;
import
com.qiniu.common.QiniuException
;
import
com.qiniu.http.Response
;
import
com.qiniu.storage.BucketManager
;
import
com.qiniu.storage.UploadManager
;
import
com.qiniu.util.Auth
;
import
com.qiniu.util.StringMap
;
/**
* 七牛云oss存储文件
*
* @author 作者 owen E-mail: 624191343@qq.com
*/
@Slf4j
@Service
(
"qiniuOssServiceImpl"
)
public
class
QiniuOssServiceImplI
extends
AbstractIFileService
implements
InitializingBean
{
@Autowired
private
UploadManager
uploadManager
;
@Autowired
private
BucketManager
bucketManager
;
@Autowired
private
Auth
auth
;
@Value
(
"${qiniu.oss.bucketName:xxxxx}"
)
private
String
bucket
;
@Value
(
"${qiniu.oss.endpoint:xxxxx}"
)
private
String
endpoint
;
private
StringMap
putPolicy
;
@Override
protected
FileType
fileType
()
{
return
FileType
.
QINIU
;
}
@Override
protected
void
uploadFile
(
MultipartFile
file
,
FileInfo
fileInfo
)
throws
Exception
{
// 调用put方法上传
uploadManager
.
put
(
file
.
getBytes
(),
fileInfo
.
getName
(),
auth
.
uploadToken
(
bucket
));
fileInfo
.
setUrl
(
endpoint
+
"/"
+
fileInfo
.
getName
());
fileInfo
.
setPath
(
endpoint
+
"/"
+
fileInfo
.
getName
());
}
@Override
protected
boolean
deleteFile
(
FileInfo
fileInfo
)
{
try
{
Response
response
=
bucketManager
.
delete
(
this
.
bucket
,
fileInfo
.
getPath
());
int
retry
=
0
;
while
(
response
.
needRetry
()
&&
retry
++
<
3
)
{
response
=
bucketManager
.
delete
(
bucket
,
fileInfo
.
getPath
());
}
}
catch
(
QiniuException
e
)
{
return
false
;
}
return
true
;
}
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
this
.
putPolicy
=
new
StringMap
();
putPolicy
.
put
(
"returnBody"
,
"{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}"
);
}
}
zlt-business/file-center/src/main/java/com/central/file/utils/FileUtil.java
浏览文件 @
9b3e424b
...
...
@@ -24,7 +24,8 @@ public class FileUtil {
public
static
FileInfo
getFileInfo
(
MultipartFile
file
)
throws
Exception
{
String
md5
=
fileMd5
(
file
.
getInputStream
());
FileInfo
fileInfo
=
new
FileInfo
();
fileInfo
.
setId
(
md5
);
// 将文件的md5设置为文件表的id
// 将文件的md5设置为文件表的id
fileInfo
.
setId
(
md5
);
fileInfo
.
setName
(
file
.
getOriginalFilename
());
fileInfo
.
setContentType
(
file
.
getContentType
());
fileInfo
.
setIsImg
(
fileInfo
.
getContentType
().
startsWith
(
"image/"
));
...
...
zlt-business/file-center/src/main/resources/application.yml
浏览文件 @
9b3e424b
#aliyun:
# oss:
# access-key: 你的密钥
# accessKeySecret: 你的密钥
# endpoint: 你的端点
# bucketName: 你的名称
# domain: 你的说明
qiniu
:
oss
:
access-key
:
tpi8mObnfzZi4ggBX8Bw7zydjoTQ0WeML3RkPKsX
accessKeySecret
:
HZBXmSyUTy-haYp0KbBTtsil-GoKjVS2kDKT8Yow
endpoint
:
http://pkqtmn0p1.bkt.clouddn.com
bucketName
:
public-oss
spring
:
datasource
:
url
:
jdbc:mysql://${zlt.datasource.ip}:3306/file_center?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
...
...
@@ -27,10 +12,28 @@ mybatis-plus:
global-config
:
db-config
:
id-type
:
INPUT
zlt
:
file-server
:
type
:
fastdfs
fdfs
:
web-url
:
${zlt.fdfs.web-url}
#oss配置
#oss:
# access-key: tpi8mObnfzZi4ggBX8Bw7zydjoTQ0WeML3RkPKsX
# accessKeySecret: HZBXmSyUTy-haYp0KbBTtsil-GoKjVS2kDKT8Yow
# endpoint: http://pkqtmn0p1.bkt.clouddn.com
# bucketName: public-oss
# domain:
swagger
:
enabled
:
true
title
:
文件中心
description
:
文件中心接口文档
version
:
1.0
base-package
:
com.central.file.controller
#fastDFS配置
fdfs
:
soTimeout
:
1500
connectTimeout
:
600
trackerList
:
${zlt.fdfs.trackerList}
\ No newline at end of file
zlt-config/src/main/resources/application-dev.properties
浏览文件 @
9b3e424b
...
...
@@ -15,3 +15,7 @@ zlt.elasticsearch.cluster-nodes=192.168.28.130:9300
##### sentinel配置
zlt.sentinel.dashboard
=
127.0.0.1:6999
##### fastDFS配置
zlt.fdfs.trackerList
=
192.168.28.130:22122
zlt.fdfs.web-url
=
192.168.28.130
\ No newline at end of file
zlt-config/src/main/resources/application-prod.properties
浏览文件 @
9b3e424b
...
...
@@ -15,3 +15,7 @@ zlt.elasticsearch.cluster-nodes=47.107.114.25:9300
##### sentinel配置
zlt.sentinel.dashboard
=
120.78.94.191:6999
##### fastDFS配置
zlt.fdfs.trackerList[0]=
47.107.114.25
:
22122
zlt.fdfs.web-url
=
47.107.114.25
\ No newline at end of file
zlt-config/src/main/resources/application-test.properties
浏览文件 @
9b3e424b
...
...
@@ -15,3 +15,7 @@ zlt.elasticsearch.cluster-nodes=47.107.114.25:9300
##### sentinel配置
zlt.sentinel.dashboard
=
120.78.94.191:6999
##### fastDFS配置
zlt.fdfs.trackerList[0]=
47.107.114.25
:
22122
zlt.fdfs.web-url
=
47.107.114.25
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录