增加分布式文件FastDFS支持

2.X
RuoYi 4 years ago committed by 疯狂的狮子li
parent fa6be7a77d
commit 3b27a85952

@ -20,6 +20,12 @@ import com.ruoyi.common.core.utils.StringUtils;
*/
public class FileUtils extends org.apache.commons.io.FileUtils
{
/** 字符常量:斜杠 {@code '/'} */
public static final char SLASH = '/';
/** 字符常量:反斜杠 {@code '\\'} */
public static final char BACKSLASH = '\\';
public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
/**
@ -167,6 +173,57 @@ public class FileUtils extends org.apache.commons.io.FileUtils
return filename;
}
/**
*
*
* @param filePath
* @return
*/
public static String getName(String filePath)
{
if (null == filePath)
{
return null;
}
int len = filePath.length();
if (0 == len)
{
return filePath;
}
if (isFileSeparator(filePath.charAt(len - 1)))
{
// 以分隔符结尾的去掉结尾分隔符
len--;
}
int begin = 0;
char c;
for (int i = len - 1; i > -1; i--)
{
c = filePath.charAt(i);
if (isFileSeparator(c))
{
// 查找最后一个路径分隔符(/或者\
begin = i + 1;
break;
}
}
return filePath.substring(begin, len);
}
/**
* WindowsLinuxUnix<br>
* Windows\LinuxUnix/
*
* @param c
* @return WindowsLinuxUnix
*/
public static boolean isFileSeparator(char c)
{
return SLASH == c || BACKSLASH == c;
}
/**
*
*

@ -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;
}
}

Loading…
Cancel
Save