1、DfsService积累 新增删除、获取磁盘空间(总空间、可用空间)方法
2、本地文件LocalFile 实现完毕 & 测试通过
This commit is contained in:
parent
fb028d7091
commit
d832c1a2f7
|
|
@ -41,17 +41,11 @@
|
||||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- FastDFS -->
|
<!-- 解决 使用了 【ConfigurationProperties】出现 Spring Boot Configuration Annotation Processor not configured 提示问题 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.tobato</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>fastdfs-client</artifactId>
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
</dependency>
|
<optional>true</optional>
|
||||||
|
|
||||||
<!-- Minio -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>io.minio</groupId>
|
|
||||||
<artifactId>minio</artifactId>
|
|
||||||
<version>${minio.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- RuoYi Api System -->
|
<!-- RuoYi Api System -->
|
||||||
|
|
@ -66,7 +60,20 @@
|
||||||
<artifactId>ruoyi-common-swagger</artifactId>
|
<artifactId>ruoyi-common-swagger</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- apache commons net ftp工具类; hutool 里面 scope 是 compile -->
|
<!-- 文件服务器1:FastDFS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.tobato</groupId>
|
||||||
|
<artifactId>fastdfs-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 文件服务器2:Minio -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.minio</groupId>
|
||||||
|
<artifactId>minio</artifactId>
|
||||||
|
<version>${minio.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 文件服务器3:apache commons net ftp工具类; hutool 里面 scope 是 compile -->
|
||||||
<!--用来ftp上传-->
|
<!--用来ftp上传-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-net</groupId>
|
<groupId>commons-net</groupId>
|
||||||
|
|
@ -79,7 +86,7 @@
|
||||||
<version>5.7.3</version>
|
<version>5.7.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!--阿里云OSS库-->
|
<!-- 文件服务器4:阿里云OSS库-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.aliyun.oss</groupId>
|
<groupId>com.aliyun.oss</groupId>
|
||||||
<artifactId>aliyun-sdk-oss</artifactId>
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
|
@ -105,7 +112,7 @@
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!--ceph upload start -->
|
<!-- 文件服务器5:ceph upload start -->
|
||||||
<!--文件上传 ceph 形式,一般项目都用不上,注释掉; 不要删-->
|
<!--文件上传 ceph 形式,一般项目都用不上,注释掉; 不要删-->
|
||||||
<!-- ceph实现s3 文件上传-->
|
<!-- ceph实现s3 文件上传-->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
@ -120,7 +127,7 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
<!--ceph upload end -->
|
<!--ceph upload end -->
|
||||||
|
|
||||||
<!--七牛云文件存储 start -->
|
<!--文件服务器6:七牛云文件存储 start -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.qiniu</groupId>
|
<groupId>com.qiniu</groupId>
|
||||||
<artifactId>qiniu-java-sdk</artifactId>
|
<artifactId>qiniu-java-sdk</artifactId>
|
||||||
|
|
@ -142,6 +149,15 @@
|
||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<!-- 打包的时候,相关的包不打入依赖-->
|
||||||
|
<excludes>
|
||||||
|
<exclude>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,28 @@
|
||||||
package com.ruoyi.file;
|
package com.ruoyi.file;
|
||||||
|
|
||||||
|
import com.ruoyi.file.config.*;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
|
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
|
||||||
|
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件服务
|
* 文件服务
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
|
@EnableConfigurationProperties(value = {
|
||||||
|
LocalFileConfig.class,
|
||||||
|
FtpConfig.class,
|
||||||
|
FastDfsConfig.class,
|
||||||
|
AliyunOssConfig.class,
|
||||||
|
CephConfig.class,
|
||||||
|
MinioConfig.class,
|
||||||
|
QiniuKodoConfig.class,
|
||||||
|
ServerProperties.class
|
||||||
|
})
|
||||||
@EnableCustomSwagger2
|
@EnableCustomSwagger2
|
||||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
|
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
|
||||||
public class RuoYiFileApplication
|
public class RuoYiFileApplication
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,9 @@ import org.springframework.context.annotation.Configuration;
|
||||||
*/
|
*/
|
||||||
@RefreshScope
|
@RefreshScope
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties(prefix = "aliyunoss")
|
@ConfigurationProperties(prefix = AliyunOssConfig.PREFIX)
|
||||||
public class AliyunOssConfig {
|
public class AliyunOssConfig {
|
||||||
|
public static final String PREFIX = "aliyun-oss";
|
||||||
/**
|
/**
|
||||||
* aliyun oss相关配置
|
* aliyun oss相关配置
|
||||||
* ACCESS_KEY_SECRET
|
* ACCESS_KEY_SECRET
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,10 @@ import org.springframework.context.annotation.Configuration;
|
||||||
@RefreshScope
|
@RefreshScope
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties(
|
@ConfigurationProperties(
|
||||||
prefix = "ceph"
|
prefix = CephConfig.PREFIX
|
||||||
)
|
)
|
||||||
public class CephConfig {
|
public class CephConfig {
|
||||||
|
public static final String PREFIX = "ceph";
|
||||||
/**
|
/**
|
||||||
* s3 提供的 accessKey secretKey
|
* s3 提供的 accessKey secretKey
|
||||||
* 示例:
|
* 示例:
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,9 @@ import org.springframework.context.annotation.Configuration;
|
||||||
@ConfigurationProperties(
|
@ConfigurationProperties(
|
||||||
prefix = FdfsClientConstants.ROOT_CONFIG_PREFIX
|
prefix = FdfsClientConstants.ROOT_CONFIG_PREFIX
|
||||||
)
|
)
|
||||||
public class FastDfsConfig extends PooledConnectionFactory {
|
public class FastDfsConfig {
|
||||||
/**
|
/**
|
||||||
* 域名或本机访问地址
|
* 文件对外访问域名or ip
|
||||||
* FastDFS配置 其他参数见:{@link PooledConnectionFactory}
|
* FastDFS配置 其他参数见:{@link PooledConnectionFactory}
|
||||||
* //@Value("${fdfs.domain}")
|
* //@Value("${fdfs.domain}")
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.ruoyi.file.config;
|
package com.ruoyi.file.config;
|
||||||
|
|
||||||
|
import com.ruoyi.file.service.FtpFileServiceImpl;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -9,13 +10,15 @@ import org.springframework.context.annotation.Configuration;
|
||||||
* vsftpd 服务器搭建、ftp客户端filezilla使、ceph分布式文件系统
|
* vsftpd 服务器搭建、ftp客户端filezilla使、ceph分布式文件系统
|
||||||
* https://blog.csdn.net/ab601026460/article/details/105928311
|
* https://blog.csdn.net/ab601026460/article/details/105928311
|
||||||
* @author dazer
|
* @author dazer
|
||||||
|
* @see FtpFileServiceImpl
|
||||||
*/
|
*/
|
||||||
@RefreshScope
|
@RefreshScope
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties(
|
@ConfigurationProperties(
|
||||||
prefix = "ftp"
|
prefix = FtpConfig.PREFIX
|
||||||
)
|
)
|
||||||
public class FtpConfig {
|
public class FtpConfig {
|
||||||
|
public static final String PREFIX = "ftp";
|
||||||
/**
|
/**
|
||||||
* ftp访问地址
|
* ftp访问地址
|
||||||
* eg1: www.ourslook.com
|
* eg1: www.ourslook.com
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
package com.ruoyi.file.config;
|
package com.ruoyi.file.config;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
|
import com.ruoyi.file.service.LocalFileServiceImpl;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -12,47 +13,54 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
* 本地文件配置
|
* 本地文件配置
|
||||||
* 通用映射配置
|
* 通用映射配置
|
||||||
*
|
*
|
||||||
|
* @see LocalFileServiceImpl
|
||||||
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@RefreshScope
|
@RefreshScope
|
||||||
@Configuration
|
@Configuration
|
||||||
public class LocalConfig implements WebMvcConfigurer
|
@ConfigurationProperties(prefix = LocalFileConfig.PREFIX)
|
||||||
|
public class LocalFileConfig implements WebMvcConfigurer
|
||||||
{
|
{
|
||||||
|
public static final String PREFIX = "file";
|
||||||
|
/**
|
||||||
|
* 文件对外访问地址,一般就是应用 或者 nginx对外地址
|
||||||
|
* eg: http://127.0.0.1:9300
|
||||||
|
* ///@Value("${file.domain}")
|
||||||
|
*/
|
||||||
|
private String domain;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 资源映射路径 前缀
|
* 资源映射路径 前缀
|
||||||
* eg: eg: /statics
|
* eg: eg: /statics
|
||||||
|
* 一般访问:${domain}/${prefix}/key
|
||||||
|
* ///@Value("${file.prefix}")
|
||||||
|
*
|
||||||
|
* 上传之后的路径,形如:"url": "http://localhost:9300/statics/2021/07/16/25292b96-a107-4cf8-baca-e1cb693fd078.jpg"
|
||||||
*/
|
*/
|
||||||
@Value("${file.prefix}")
|
private String prefix;
|
||||||
private String localFilePrefix;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 域名或本机访问地址
|
|
||||||
* eg: http://127.0.0.1:9300
|
|
||||||
*/
|
|
||||||
@Value("${file.domain}")
|
|
||||||
private String domain;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传文件存储在本地的根路径
|
* 上传文件存储在本地的根路径
|
||||||
* eg: D:/ruoyi/uploadPath
|
* eg: D:/ruoyi/uploadPath
|
||||||
|
* ///@Value("${file.path}")
|
||||||
*/
|
*/
|
||||||
@Value("${file.path}")
|
private String path;
|
||||||
private String localFilePath;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addResourceHandlers(ResourceHandlerRegistry registry)
|
public void addResourceHandlers(ResourceHandlerRegistry registry)
|
||||||
{
|
{
|
||||||
/** 本地文件上传路径 */
|
/** 本地文件上传路径 */
|
||||||
registry.addResourceHandler(localFilePrefix + "/**")
|
registry.addResourceHandler(prefix + "/**")
|
||||||
.addResourceLocations("file:" + localFilePath + File.separator);
|
.addResourceLocations("file:" + path + File.separator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLocalFilePrefix() {
|
public String getPrefix() {
|
||||||
return localFilePrefix;
|
return prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLocalFilePrefix(String localFilePrefix) {
|
public void setPrefix(String prefix) {
|
||||||
this.localFilePrefix = localFilePrefix;
|
this.prefix = prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDomain() {
|
public String getDomain() {
|
||||||
|
|
@ -63,11 +71,11 @@ public class LocalConfig implements WebMvcConfigurer
|
||||||
this.domain = domain;
|
this.domain = domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLocalFilePath() {
|
public String getPath() {
|
||||||
return localFilePath;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLocalFilePath(String localFilePath) {
|
public void setPath(String path) {
|
||||||
this.localFilePath = localFilePath;
|
this.path = path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,21 +1,25 @@
|
||||||
package com.ruoyi.file.config;
|
package com.ruoyi.file.config;
|
||||||
|
|
||||||
|
import com.ruoyi.file.service.MinioDfsServiceImpl;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import io.minio.MinioClient;
|
import io.minio.MinioClient;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minio 配置信息
|
* Minio 配置信息
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
|
* @see MinioDfsServiceImpl 实现
|
||||||
*/
|
*/
|
||||||
@RefreshScope
|
@RefreshScope
|
||||||
|
@Component
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties(prefix = "minio")
|
@ConfigurationProperties(prefix = MinioConfig.PREFIX)
|
||||||
public class MinioConfig
|
public class MinioConfig {
|
||||||
{
|
public static final String PREFIX = "minio";
|
||||||
/**
|
/**
|
||||||
* 服务地址
|
* 服务地址
|
||||||
* eg: http://192.168.254.100:9900
|
* eg: http://192.168.254.100:9900
|
||||||
|
|
@ -40,49 +44,40 @@ public class MinioConfig
|
||||||
*/
|
*/
|
||||||
private String bucketName;
|
private String bucketName;
|
||||||
|
|
||||||
public String getUrl()
|
public String getUrl() {
|
||||||
{
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUrl(String url)
|
public void setUrl(String url) {
|
||||||
{
|
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAccessKey()
|
public String getAccessKey() {
|
||||||
{
|
|
||||||
return accessKey;
|
return accessKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAccessKey(String accessKey)
|
public void setAccessKey(String accessKey) {
|
||||||
{
|
|
||||||
this.accessKey = accessKey;
|
this.accessKey = accessKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSecretKey()
|
public String getSecretKey() {
|
||||||
{
|
|
||||||
return secretKey;
|
return secretKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSecretKey(String secretKey)
|
public void setSecretKey(String secretKey) {
|
||||||
{
|
|
||||||
this.secretKey = secretKey;
|
this.secretKey = secretKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getBucketName()
|
public String getBucketName() {
|
||||||
{
|
|
||||||
return bucketName;
|
return bucketName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBucketName(String bucketName)
|
public void setBucketName(String bucketName) {
|
||||||
{
|
|
||||||
this.bucketName = bucketName;
|
this.bucketName = bucketName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public MinioClient getMinioClient()
|
public MinioClient getMinioClient() {
|
||||||
{
|
|
||||||
return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
|
return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,9 @@ import org.springframework.context.annotation.Configuration;
|
||||||
*/
|
*/
|
||||||
@RefreshScope
|
@RefreshScope
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties(prefix = "qiniu")
|
@ConfigurationProperties(prefix = QiniuKodoConfig.PREFIX)
|
||||||
public class QiniuKodoConfig {
|
public class QiniuKodoConfig {
|
||||||
|
public static final String PREFIX = "qiniu";
|
||||||
/**
|
/**
|
||||||
* AccessKey 简称ak
|
* AccessKey 简称ak
|
||||||
* 申请地址=>登录七牛云:https://portal.qiniu.com/user/key
|
* 申请地址=>登录七牛云:https://portal.qiniu.com/user/key
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
package com.ruoyi.file.controller;
|
package com.ruoyi.file.controller;
|
||||||
|
|
||||||
import com.ruoyi.file.service.IDfsService;
|
import com.ruoyi.file.service.IDfsService;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
@ -17,32 +19,54 @@ import com.ruoyi.system.api.domain.SysFile;
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class SysFileController
|
public class SysFileController {
|
||||||
{
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
|
private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IDfsService sysFileService;
|
private IDfsService dfsService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传请求
|
* 文件上传请求
|
||||||
*/
|
*/
|
||||||
@PostMapping("upload")
|
@PostMapping("upload")
|
||||||
public R<SysFile> upload(MultipartFile file)
|
public R<SysFile> upload(MultipartFile file) {
|
||||||
{
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
// 上传并返回访问地址
|
// 上传并返回访问地址
|
||||||
String url = sysFileService.uploadFile(file);
|
String url = dfsService.uploadFile(file);
|
||||||
SysFile sysFile = new SysFile();
|
SysFile sysFile = new SysFile();
|
||||||
sysFile.setName(FileUtils.getName(url));
|
sysFile.setName(FileUtils.getName(url));
|
||||||
sysFile.setUrl(url);
|
sysFile.setUrl(url);
|
||||||
return R.ok(sysFile);
|
return R.ok(sysFile);
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
log.error("上传文件失败", e);
|
log.error("上传文件失败", e);
|
||||||
return R.fail(e.getMessage());
|
return R.fail(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除文件
|
||||||
|
*/
|
||||||
|
@PostMapping("delete")
|
||||||
|
public R<Boolean> delete(String fileUrl) {
|
||||||
|
if (StringUtils.isBlank(fileUrl)) {
|
||||||
|
return R.fail(false, "fileUrl 不能为空");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// 上传并返回访问地址
|
||||||
|
boolean isOk = dfsService.deleteFile(fileUrl);
|
||||||
|
return isOk ? R.ok(true, "删除成功") : R.fail(false, "删除失败");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("删除文件失败", e);
|
||||||
|
return R.fail(false, "删除失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件总大小,占用内容大小;
|
||||||
|
* 形如:总 233.57 GB, 可用 72.12 GB
|
||||||
|
*/
|
||||||
|
@GetMapping("objectsCapacityStr")
|
||||||
|
public R<String> objectsCapacityStr() {
|
||||||
|
return R.ok(dfsService.objectsCapacityStr(), "获取成功");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -16,7 +16,7 @@ import java.io.IOException;
|
||||||
* @author dazer
|
* @author dazer
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class FtpDfsServiceImpl implements IDfsService {
|
public class FtpFileServiceImpl implements IDfsService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private FtpConfig ftpConfig;
|
private FtpConfig ftpConfig;
|
||||||
public static final String ACCESS_PREFIX = "";
|
public static final String ACCESS_PREFIX = "";
|
||||||
|
|
@ -60,7 +60,7 @@ public interface IDfsService
|
||||||
/**
|
/**
|
||||||
* 获取文件占用空间
|
* 获取文件占用空间
|
||||||
* 别名:objectsCapacity
|
* 别名:objectsCapacity
|
||||||
* @return 文件大小字符串,eg: 100MB、2G
|
* @return 文件大小字符串,eg: 100MB、2G; 形如:总 233.57 GB, 可用 72.12 GB
|
||||||
*/
|
*/
|
||||||
String objectsCapacityStr();
|
String objectsCapacityStr();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,58 +0,0 @@
|
||||||
package com.ruoyi.file.service;
|
|
||||||
|
|
||||||
import com.ruoyi.common.core.exception.CustomException;
|
|
||||||
import com.ruoyi.file.config.LocalConfig;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.context.annotation.Primary;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
import com.ruoyi.file.utils.FileUploadUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 本地文件存储
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*/
|
|
||||||
@Primary
|
|
||||||
@Service
|
|
||||||
public class LocalDfsServiceImpl implements IDfsService
|
|
||||||
{
|
|
||||||
private final LocalConfig localConfig;
|
|
||||||
|
|
||||||
public LocalDfsServiceImpl(LocalConfig localConfig) {
|
|
||||||
this.localConfig = localConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 本地文件上传接口
|
|
||||||
*
|
|
||||||
* @param file 上传的文件
|
|
||||||
* @return 访问地址
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String uploadFile(MultipartFile file) throws Exception
|
|
||||||
{
|
|
||||||
return this.uploadFile(file, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String uploadFile(MultipartFile file, String modules) throws Exception {
|
|
||||||
String localFilePath = localConfig.getLocalFilePath();
|
|
||||||
String domain = localConfig.getDomain();
|
|
||||||
String localFilePrefix = localConfig.getLocalFilePrefix();
|
|
||||||
|
|
||||||
String name = FileUploadUtils.upload(localFilePath + "/" + StringUtils.defaultString(modules, ""), file);
|
|
||||||
return domain + localFilePrefix + name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean deleteFile(String fileUrl) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String objectsCapacityStr() {
|
|
||||||
throw new CustomException("本地存储-获取文件占用空间功能,敬请期待");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,111 @@
|
||||||
|
package com.ruoyi.file.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import com.ruoyi.common.core.exception.CustomException;
|
||||||
|
import com.ruoyi.file.config.LocalFileConfig;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import com.ruoyi.file.utils.FileUploadUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本地文件存储
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
@Primary
|
||||||
|
@Service
|
||||||
|
public class LocalFileServiceImpl implements IDfsService
|
||||||
|
{
|
||||||
|
private final LocalFileConfig localFileConfig;
|
||||||
|
|
||||||
|
public LocalFileServiceImpl(LocalFileConfig localFileConfig) {
|
||||||
|
this.localFileConfig = localFileConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本地文件上传接口
|
||||||
|
*
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @return 访问地址
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String uploadFile(MultipartFile file) throws Exception
|
||||||
|
{
|
||||||
|
return this.uploadFile(file, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String uploadFile(MultipartFile file, String modules) throws Exception {
|
||||||
|
String localFilePath = localFileConfig.getPath();
|
||||||
|
String domain = localFileConfig.getDomain();
|
||||||
|
String localFilePrefix = localFileConfig.getPrefix();
|
||||||
|
if (StringUtils.isBlank(localFilePath) ||
|
||||||
|
StringUtils.isBlank(localFilePath) ||
|
||||||
|
StringUtils.isBlank(localFilePath) ) {
|
||||||
|
throw new CustomException("文件服务器:file 相关配置不能为空!");
|
||||||
|
}
|
||||||
|
|
||||||
|
String name = FileUploadUtils.upload(localFilePath + "/" + StringUtils.defaultString(modules, ""), file);
|
||||||
|
return domain + localFilePrefix + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean deleteFile(String fileUrl) {
|
||||||
|
if (StringUtils.isBlank(fileUrl)) {
|
||||||
|
throw new CustomException("fileUrl不能为空!");
|
||||||
|
}
|
||||||
|
String key = this.getStorePath(fileUrl);
|
||||||
|
String localFilePath = localFileConfig.getPath();
|
||||||
|
|
||||||
|
String filePath = localFilePath + "/" + key;
|
||||||
|
File file = new File(filePath);
|
||||||
|
if (!file.exists()) {
|
||||||
|
throw new CustomException("文件不存在,或者已经删除:" + filePath);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return file.delete();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new CustomException("文件删除失败:" + e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String objectsCapacityStr() {
|
||||||
|
String localFilePath = localFileConfig.getPath();
|
||||||
|
File file = new File(localFilePath);
|
||||||
|
long total = file.getTotalSpace();
|
||||||
|
long free = file.getFreeSpace();
|
||||||
|
String totalSpace = FileUtil.readableFileSize(total);
|
||||||
|
String freeSpace = FileUtil.readableFileSize(free);
|
||||||
|
return "总 " + totalSpace + ", 可用 " + freeSpace;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换url,为原始的key
|
||||||
|
*
|
||||||
|
* @param filePath http://localhost:9300/statics/2021/07/16/25292b96-a107-4cf8-baca-e1cb693fd078.jpg
|
||||||
|
* @return 2021/07/16/25292b96-a107-4cf8-baca-e1cb693fd078.jpg
|
||||||
|
*/
|
||||||
|
private String getStorePath(String filePath) {
|
||||||
|
// 使用方式1
|
||||||
|
String domain = localFileConfig.getDomain();
|
||||||
|
String localFilePrefix = localFileConfig.getPrefix();
|
||||||
|
String publicPath1 = domain + localFilePrefix;
|
||||||
|
String key = filePath.replace(publicPath1, "");
|
||||||
|
|
||||||
|
if (key.equals(filePath)) {
|
||||||
|
// 使用方式2
|
||||||
|
String group = localFileConfig.getPrefix();
|
||||||
|
// 获取group起始位置
|
||||||
|
int pathStartPos = filePath.indexOf(group) + group.length() + 1;
|
||||||
|
key = filePath.substring(pathStartPos, filePath.length());
|
||||||
|
}
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -111,7 +111,7 @@ public class QiniuDfsServiceImpl implements IDfsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换url
|
* 转换url,为原始的key
|
||||||
*
|
*
|
||||||
* @param filePath http://guangdong-oss.ityun.ltd/upload/default/header.jpg
|
* @param filePath http://guangdong-oss.ityun.ltd/upload/default/header.jpg
|
||||||
* @return upload/default/header.jpg
|
* @return upload/default/header.jpg
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ spring:
|
||||||
application:
|
application:
|
||||||
# 应用名称
|
# 应用名称
|
||||||
name: ruoyi-file
|
name: ruoyi-file
|
||||||
|
main:
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
profiles:
|
profiles:
|
||||||
# 环境配置
|
# 环境配置
|
||||||
active: dev
|
active: dev
|
||||||
|
|
@ -23,3 +25,26 @@ spring:
|
||||||
# 共享配置
|
# 共享配置
|
||||||
shared-configs:
|
shared-configs:
|
||||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
|
|
||||||
|
|
||||||
|
# 文件服务器之1 本地文件上传
|
||||||
|
file:
|
||||||
|
domain: http://localhost:9300
|
||||||
|
prefix: /statics
|
||||||
|
path: D:/ruoyi/uploadPath
|
||||||
|
|
||||||
|
# 文件服务器之3 FastDFS配置
|
||||||
|
fdfs:
|
||||||
|
domain: http://8.129.231.12
|
||||||
|
soTimeout: 3000
|
||||||
|
connectTimeout: 2000
|
||||||
|
trackerList: 8.129.231.12:22122
|
||||||
|
|
||||||
|
# 文件服务器之4 Minio配置
|
||||||
|
minio:
|
||||||
|
url: http://192.168.254.100:9900
|
||||||
|
accessKey: D99KGE6ZTQXSATTJWU24
|
||||||
|
secretKey: QyVqGnhIQQE734UYSUFlGOZViE6+ZlDEfUG3NjhJ
|
||||||
|
bucketName: neusoft-appt
|
||||||
|
#regex: .*appt-file/
|
||||||
|
#encryptFlag: true
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue