mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2025-09-07 12:59:01 +00:00
增加分布式文件FastDFS支持
This commit is contained in:
@@ -48,6 +48,13 @@
|
||||
<version>${swagger.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- FastDFS -->
|
||||
<dependency>
|
||||
<groupId>com.github.tobato</groupId>
|
||||
<artifactId>fastdfs-client</artifactId>
|
||||
<version>1.26.5</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Ruoyi Common Security -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
|
@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.utils.file.FileUtils;
|
||||
import com.ruoyi.file.service.ISysFileService;
|
||||
import com.ruoyi.system.api.domain.SysFile;
|
||||
|
||||
@@ -27,18 +28,6 @@ public class SysFileController
|
||||
@Value("${file.path}")
|
||||
private String localFilePath;
|
||||
|
||||
/**
|
||||
* 资源映射路径 前缀
|
||||
*/
|
||||
@Value("${file.prefix}")
|
||||
public String localFilePrefix;
|
||||
|
||||
/**
|
||||
* 域名或本机访问地址
|
||||
*/
|
||||
@Value("${file.domain}")
|
||||
public String domain;
|
||||
|
||||
@Autowired
|
||||
private ISysFileService sysFileService;
|
||||
|
||||
@@ -50,11 +39,10 @@ public class SysFileController
|
||||
{
|
||||
try
|
||||
{
|
||||
// 上传并返回新文件名称
|
||||
String fileName = sysFileService.uploadFile(file, localFilePath);
|
||||
String url = domain + localFilePrefix + fileName;
|
||||
// 上传并返回访问地址
|
||||
String url = sysFileService.uploadFile(file, localFilePath);
|
||||
SysFile sysFile = new SysFile();
|
||||
sysFile.setName(fileName);
|
||||
sysFile.setName(FileUtils.getName(url));
|
||||
sysFile.setUrl(url);
|
||||
return R.ok(sysFile);
|
||||
}
|
||||
|
@@ -0,0 +1,43 @@
|
||||
package com.ruoyi.file.service;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
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;
|
||||
|
||||
/**
|
||||
* FastDFS文件存储
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Service
|
||||
public class FastDfsSysFileServiceImpl implements ISysFileService
|
||||
{
|
||||
/**
|
||||
* 域名或本机访问地址
|
||||
*/
|
||||
@Value("${fdfs.domain}")
|
||||
public String domain;
|
||||
|
||||
@Autowired
|
||||
private FastFileStorageClient storageClient;
|
||||
|
||||
/**
|
||||
* FastDfs文件上传接口
|
||||
*
|
||||
* @param file 上传的文件
|
||||
* @param baseDir 相对应用的基目录
|
||||
* @return 访问地址
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public String uploadFile(MultipartFile file, String baseDir) throws Exception
|
||||
{
|
||||
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
|
||||
FilenameUtils.getExtension(file.getOriginalFilename()), null);
|
||||
return domain + "/" + storePath.getFullPath();
|
||||
}
|
||||
}
|
@@ -14,7 +14,7 @@ public interface ISysFileService
|
||||
*
|
||||
* @param file 上传的文件
|
||||
* @param baseDir 相对应用的基目录
|
||||
* @return 文件名称
|
||||
* @return 访问地址
|
||||
* @throws Exception
|
||||
*/
|
||||
public String uploadFile(MultipartFile file, String baseDir) throws Exception;
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package com.ruoyi.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.ruoyi.file.utils.FileUploadUtils;
|
||||
@@ -9,19 +11,34 @@ import com.ruoyi.file.utils.FileUploadUtils;
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Primary
|
||||
@Service
|
||||
public class LocalSysFileServiceImpl implements ISysFileService
|
||||
{
|
||||
/**
|
||||
* 文件上传接口
|
||||
* 资源映射路径 前缀
|
||||
*/
|
||||
@Value("${file.prefix}")
|
||||
public String localFilePrefix;
|
||||
|
||||
/**
|
||||
* 域名或本机访问地址
|
||||
*/
|
||||
@Value("${file.domain}")
|
||||
public String domain;
|
||||
|
||||
/**
|
||||
* 本地文件上传接口
|
||||
*
|
||||
* @param file 上传的文件
|
||||
* @param baseDir 相对应用的基目录
|
||||
* @return 文件名称
|
||||
* @return 访问地址
|
||||
* @throws Exception
|
||||
*/
|
||||
public String uploadFile(MultipartFile file, String baseDir) throws Exception
|
||||
{
|
||||
return FileUploadUtils.upload(baseDir, file);
|
||||
String name = FileUploadUtils.upload(baseDir, file);
|
||||
String url = domain + localFilePrefix + name;
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user