完善对文件上传的梳理
This commit is contained in:
parent
a84343c9c1
commit
10676c0e65
2
pom.xml
2
pom.xml
@ -17,7 +17,7 @@
|
|||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
|
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
|
||||||
<spring-boot.version>3.3.5</spring-boot.version>
|
<spring-boot.version>3.4.0</spring-boot.version>
|
||||||
<druid.version>1.2.23</druid.version>
|
<druid.version>1.2.23</druid.version>
|
||||||
<dynamic.version>3.5.2</dynamic.version>
|
<dynamic.version>3.5.2</dynamic.version>
|
||||||
<jta.version>1.1</jta.version>
|
<jta.version>1.1</jta.version>
|
||||||
|
@ -5,7 +5,6 @@ import static com.ruoyi.common.utils.file.FileUtils.*;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -17,12 +16,10 @@ import com.ruoyi.common.config.RuoYiConfig;
|
|||||||
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.constant.Constants;
|
||||||
import com.ruoyi.common.core.domain.entity.FileEntity;
|
import com.ruoyi.common.core.domain.entity.FileEntity;
|
||||||
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
|
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
|
||||||
import com.ruoyi.common.utils.DateUtils;
|
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
import com.ruoyi.common.utils.file.FileOperateUtils;
|
import com.ruoyi.common.utils.file.FileOperateUtils;
|
||||||
import com.ruoyi.common.utils.file.FileUtils;
|
import com.ruoyi.common.utils.file.FileUtils;
|
||||||
import com.ruoyi.common.utils.sign.Md5Utils;
|
import com.ruoyi.common.utils.sign.Md5Utils;
|
||||||
import com.ruoyi.common.utils.uuid.UUID;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 磁盘文件操作实现类
|
* 磁盘文件操作实现类
|
||||||
@ -30,55 +27,21 @@ import com.ruoyi.common.utils.uuid.UUID;
|
|||||||
@Component("file:strategy:disk")
|
@Component("file:strategy:disk")
|
||||||
public class DiskFileService implements FileService {
|
public class DiskFileService implements FileService {
|
||||||
|
|
||||||
private static String defaultBaseDir = RuoYiConfig.getProfile();
|
@Override
|
||||||
|
public String upload(String filePath, MultipartFile file) throws Exception {
|
||||||
public static void setDefaultBaseDir(String defaultBaseDir) {
|
return upload(RuoYiConfig.getProfile(), filePath, file);
|
||||||
DiskFileService.defaultBaseDir = defaultBaseDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getDefaultBaseDir() {
|
|
||||||
return defaultBaseDir;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String upload(String filePath, MultipartFile file) throws Exception {
|
public String upload(String baseDir, String filePath, MultipartFile file) throws Exception {
|
||||||
int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
|
int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
|
||||||
if (fileNamelength > FileUtils.DEFAULT_FILE_NAME_LENGTH) {
|
if (fileNamelength > FileUtils.DEFAULT_FILE_NAME_LENGTH) {
|
||||||
throw new FileNameLengthLimitExceededException(FileUtils.DEFAULT_FILE_NAME_LENGTH);
|
throw new FileNameLengthLimitExceededException(FileUtils.DEFAULT_FILE_NAME_LENGTH);
|
||||||
}
|
}
|
||||||
|
String absPath = getAbsoluteFile(baseDir + File.separator + filePath).getAbsolutePath();
|
||||||
// String fileName = extractFilename(file);
|
|
||||||
|
|
||||||
String absPath = getAbsoluteFile(filePath).getAbsolutePath();
|
|
||||||
file.transferTo(Paths.get(absPath));
|
file.transferTo(Paths.get(absPath));
|
||||||
return getPathFileName(filePath);
|
return getPathFileName(filePath);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String upload(MultipartFile file, String name) throws Exception {
|
|
||||||
try {
|
|
||||||
return upload(getDefaultBaseDir(), file);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IOException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String upload(MultipartFile file) throws Exception {
|
|
||||||
try {
|
|
||||||
String filePath = getDefaultBaseDir() + File.separator + DateUtils.dateTime() + File.separator
|
|
||||||
+ DateUtils.dateTimeNow() + UUID.fastUUID().toString().substring(0, 6)
|
|
||||||
+ "." + FileUtils.getExtension(file);
|
|
||||||
return upload(filePath, file);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IOException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String upload(String baseDir, String fileName, MultipartFile file) throws Exception {
|
|
||||||
String filePath = RuoYiConfig.getProfile() + File.separator + baseDir + File.separator + fileName;
|
|
||||||
return upload(filePath, file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -88,7 +51,6 @@ public class DiskFileService implements FileService {
|
|||||||
// 数据库资源地址
|
// 数据库资源地址
|
||||||
String downloadPath = localPath + StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX);
|
String downloadPath = localPath + StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX);
|
||||||
// 下载名称
|
// 下载名称
|
||||||
|
|
||||||
File file = new File(downloadPath);
|
File file = new File(downloadPath);
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
throw new FileNotFoundException("未找到文件");
|
throw new FileNotFoundException("未找到文件");
|
||||||
@ -113,13 +75,6 @@ public class DiskFileService implements FileService {
|
|||||||
return flag;
|
return flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取文件
|
|
||||||
*
|
|
||||||
* @param filePath
|
|
||||||
* @return
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public FileEntity getFile(String filePath) throws Exception {
|
public FileEntity getFile(String filePath) throws Exception {
|
||||||
String localPath = RuoYiConfig.getProfile();
|
String localPath = RuoYiConfig.getProfile();
|
||||||
|
@ -1,25 +1,51 @@
|
|||||||
package com.ruoyi.common.service.file;
|
package com.ruoyi.common.service.file;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.ruoyi.common.core.domain.entity.FileEntity;
|
import com.ruoyi.common.core.domain.entity.FileEntity;
|
||||||
|
import com.ruoyi.common.utils.DateUtils;
|
||||||
|
import com.ruoyi.common.utils.file.FileUtils;
|
||||||
|
|
||||||
//默认上传下载
|
|
||||||
/**
|
/**
|
||||||
* 文件操作接口
|
* 文件操作接口
|
||||||
*/
|
*/
|
||||||
public interface FileService {
|
public interface FileService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传
|
||||||
|
*
|
||||||
|
* @param file 文件对象
|
||||||
|
* @return 返回上传成功的文路径
|
||||||
|
* @throws Exception 比如读写文件出错时
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
default public String upload(MultipartFile file) throws Exception {
|
||||||
|
return upload(FileUtils.fastFilePath(file), file);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传
|
||||||
|
*
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @param fileName 上传文件的名称
|
||||||
|
* @return 返回上传成功的文路径
|
||||||
|
* @throws Exception 比如读写文件出错时
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
default public String upload(MultipartFile file, String fileName) throws Exception {
|
||||||
|
return upload(DateUtils.datePath() + File.separator + fileName, file);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传
|
* 文件上传
|
||||||
*
|
*
|
||||||
* @param filePath 上传的文件路径
|
* @param filePath 上传的文件路径
|
||||||
* @param file 文件对象
|
* @param file 文件对象
|
||||||
* @return 返回上传成功的文路径
|
* @return 返回上传成功的文路径
|
||||||
* @throws IOException 比如读写文件出错时
|
* @throws Exception 比如读写文件出错时
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public String upload(String filePath, MultipartFile file) throws Exception;
|
public String upload(String filePath, MultipartFile file) throws Exception;
|
||||||
@ -27,42 +53,21 @@ public interface FileService {
|
|||||||
/**
|
/**
|
||||||
* 文件上传
|
* 文件上传
|
||||||
*
|
*
|
||||||
* @param filePath 上传的文件路径
|
* @param baseDir 上传文件的根坐标
|
||||||
|
* @param filePath 文件路径
|
||||||
* @param file 文件对象
|
* @param file 文件对象
|
||||||
* @return 返回上传成功的文路径
|
* @return 返回上传成功的文路径
|
||||||
* @throws IOException 比如读写文件出错时
|
* @throws Exception 比如读写文件出错时
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public String upload(MultipartFile file, String name) throws Exception;
|
public String upload(String baseDir, String filePath, MultipartFile file) throws Exception;
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件上传
|
|
||||||
*
|
|
||||||
* @param file 文件对象
|
|
||||||
* @return 返回上传成功的文路径
|
|
||||||
* @throws IOException 比如读写文件出错时
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public String upload(MultipartFile file) throws Exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文件上传
|
|
||||||
*
|
|
||||||
* @param baseDir 上传的文件基路径
|
|
||||||
* @param fileName 文件名称
|
|
||||||
* @param file 文件对象
|
|
||||||
* @return 返回上传成功的文路径
|
|
||||||
* @throws IOException 比如读写文件出错时
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public String upload(String baseDir, String fileName, MultipartFile file) throws Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件下载
|
* 文件下载
|
||||||
*
|
*
|
||||||
* @param filePath 下载的文件路径
|
* @param filePath 下载的文件路径
|
||||||
* @return 返回上传成功的文路径
|
* @return 返回上传成功的文路径
|
||||||
* @throws IOException 比如读写文件出错时
|
* @throws Exception 比如读写文件出错时
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public InputStream downLoad(String filePath) throws Exception;
|
public InputStream downLoad(String filePath) throws Exception;
|
||||||
@ -72,7 +77,7 @@ public interface FileService {
|
|||||||
*
|
*
|
||||||
* @param filePath 删除的文件路径
|
* @param filePath 删除的文件路径
|
||||||
* @return 返回上传成功的文路径
|
* @return 返回上传成功的文路径
|
||||||
* @throws IOException 比如读写文件出错时
|
* @throws Exception 比如读写文件出错时
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public boolean deleteFile(String filePath) throws Exception;
|
public boolean deleteFile(String filePath) throws Exception;
|
||||||
@ -80,8 +85,8 @@ public interface FileService {
|
|||||||
/**
|
/**
|
||||||
* 获取文件
|
* 获取文件
|
||||||
*
|
*
|
||||||
* @param filePath
|
* @param filePath 文件路径
|
||||||
* @return
|
* @return 文件对象
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public FileEntity getFile(String filePath) throws Exception;
|
public FileEntity getFile(String filePath) throws Exception;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.ruoyi.common.utils.file;
|
package com.ruoyi.common.utils.file;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@ -33,8 +32,15 @@ public class FileOperateUtils {
|
|||||||
public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
|
public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认上传的地址
|
* 以默认配置进行文件上传
|
||||||
|
*
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @return 文件路径
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
public static final String upload(MultipartFile file) throws IOException {
|
||||||
|
return upload(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 以默认配置进行文件上传
|
* 以默认配置进行文件上传
|
||||||
@ -43,14 +49,14 @@ public class FileOperateUtils {
|
|||||||
* @return 文件路径
|
* @return 文件路径
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static final String upload(MultipartFile file) throws IOException {
|
public static final String upload(MultipartFile file, String[] allowedExtension) throws IOException {
|
||||||
try {
|
try {
|
||||||
String md5 = Md5Utils.getMd5(file);
|
String md5 = Md5Utils.getMd5(file);
|
||||||
String pathForMd5 = FileOperateUtils.getFilePathForMd5(md5);
|
String pathForMd5 = FileOperateUtils.getFilePathForMd5(md5);
|
||||||
if (StringUtils.isNotEmpty(pathForMd5)) {
|
if (StringUtils.isNotEmpty(pathForMd5)) {
|
||||||
return pathForMd5;
|
return pathForMd5;
|
||||||
}
|
}
|
||||||
FileUtils.assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
FileUtils.assertAllowed(file, allowedExtension);
|
||||||
String pathFileName = fileService.upload(file);
|
String pathFileName = fileService.upload(file);
|
||||||
FileOperateUtils.saveFileAndMd5(pathFileName, md5);
|
FileOperateUtils.saveFileAndMd5(pathFileName, md5);
|
||||||
return pathFileName;
|
return pathFileName;
|
||||||
@ -68,7 +74,22 @@ public class FileOperateUtils {
|
|||||||
* @return 文件名称
|
* @return 文件名称
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static final String upload(String filePath, MultipartFile file, String[] allowedExtension) throws Exception {
|
public static final String upload(String filePath, MultipartFile file) throws Exception {
|
||||||
|
return upload(filePath, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据文件路径上传
|
||||||
|
*
|
||||||
|
* @param filePath 上传文件的路径
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @param allowedExtension 允许的扩展名
|
||||||
|
* @return 文件名称
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static final String upload(String filePath, MultipartFile file, String[] allowedExtension)
|
||||||
|
throws IOException {
|
||||||
|
try {
|
||||||
String md5 = Md5Utils.getMd5(file);
|
String md5 = Md5Utils.getMd5(file);
|
||||||
String pathForMd5 = FileOperateUtils.getFilePathForMd5(md5);
|
String pathForMd5 = FileOperateUtils.getFilePathForMd5(md5);
|
||||||
if (StringUtils.isNotEmpty(pathForMd5)) {
|
if (StringUtils.isNotEmpty(pathForMd5)) {
|
||||||
@ -78,24 +99,49 @@ public class FileOperateUtils {
|
|||||||
fileService.upload(filePath, file);
|
fileService.upload(filePath, file);
|
||||||
FileOperateUtils.saveFileAndMd5(filePath, md5);
|
FileOperateUtils.saveFileAndMd5(filePath, md5);
|
||||||
return filePath;
|
return filePath;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException(e.getMessage(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据文件路径上传
|
* 根据文件路径上传
|
||||||
*
|
*
|
||||||
* @param baseDir 相对应用的基目录
|
* @param baseDir 上传文件的根坐标
|
||||||
|
* @param file 上传的文件
|
||||||
|
* @param filePath 上传文件路径
|
||||||
|
* @param allowedExtension 允许的扩展名
|
||||||
|
* @return 文件名称
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static final String upload(String baseDir, String filePath, MultipartFile file)
|
||||||
|
throws IOException {
|
||||||
|
return upload(baseDir, filePath, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据文件路径上传
|
||||||
|
*
|
||||||
|
* @param baseDir 上传文件的根坐标
|
||||||
* @param file 上传的文件
|
* @param file 上传的文件
|
||||||
* @param fileName 上传文件名
|
* @param fileName 上传文件名
|
||||||
* @param allowedExtension 允许的扩展名
|
* @param allowedExtension 允许的扩展名
|
||||||
* @return 文件名称
|
* @return 文件名称
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static final String upload(String baseDir, String fileName, MultipartFile file,
|
public static final String upload(String baseDir, String filePath, MultipartFile file,
|
||||||
String[] allowedExtension)
|
String[] allowedExtension)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
try {
|
try {
|
||||||
String filePath = baseDir + File.separator + fileName;
|
String md5 = Md5Utils.getMd5(file);
|
||||||
return upload(filePath, file, allowedExtension);
|
String pathForMd5 = FileOperateUtils.getFilePathForMd5(md5);
|
||||||
|
if (StringUtils.isNotEmpty(pathForMd5)) {
|
||||||
|
return pathForMd5;
|
||||||
|
}
|
||||||
|
FileUtils.assertAllowed(file, allowedExtension);
|
||||||
|
fileService.upload(baseDir, filePath, file);
|
||||||
|
FileOperateUtils.saveFileAndMd5(filePath, md5);
|
||||||
|
return filePath;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IOException(e.getMessage(), e);
|
throw new IOException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@ -148,7 +194,7 @@ public class FileOperateUtils {
|
|||||||
* 根据md5获取文件的路径
|
* 根据md5获取文件的路径
|
||||||
*
|
*
|
||||||
* @param md5 文件的md5
|
* @param md5 文件的md5
|
||||||
* @return
|
* @return 文件路径
|
||||||
*/
|
*/
|
||||||
public static String getFilePathForMd5(String md5) {
|
public static String getFilePathForMd5(String md5) {
|
||||||
return CacheUtils.get(CacheConstants.FILE_MD5_PATH_KEY, md5, String.class);
|
return CacheUtils.get(CacheConstants.FILE_MD5_PATH_KEY, md5, String.class);
|
||||||
@ -166,7 +212,7 @@ public class FileOperateUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除文件的md5
|
* 根据md5删除文件的缓存信息
|
||||||
*
|
*
|
||||||
* @param md5 文件的md5
|
* @param md5 文件的md5
|
||||||
*/
|
*/
|
||||||
@ -178,6 +224,11 @@ public class FileOperateUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据文件路径删除文件的缓存信息
|
||||||
|
*
|
||||||
|
* @param filePath 文件的路径
|
||||||
|
*/
|
||||||
public static void deleteFileAndMd5ByFilePath(String filePath) {
|
public static void deleteFileAndMd5ByFilePath(String filePath) {
|
||||||
String md5ByFilePath = CacheUtils.get(CacheConstants.FILE_PATH_MD5_KEY, filePath, String.class);
|
String md5ByFilePath = CacheUtils.get(CacheConstants.FILE_PATH_MD5_KEY, filePath, String.class);
|
||||||
if (StringUtils.isNotEmpty(md5ByFilePath)) {
|
if (StringUtils.isNotEmpty(md5ByFilePath)) {
|
||||||
|
@ -1,5 +1,24 @@
|
|||||||
package com.ruoyi.common.utils.file;
|
package com.ruoyi.common.utils.file;
|
||||||
|
|
||||||
|
import static com.ruoyi.common.utils.file.FileOperateUtils.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FilenameUtils;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.ruoyi.common.config.RuoYiConfig;
|
import com.ruoyi.common.config.RuoYiConfig;
|
||||||
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.constant.Constants;
|
||||||
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
|
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
|
||||||
@ -9,21 +28,9 @@ import com.ruoyi.common.utils.StringUtils;
|
|||||||
import com.ruoyi.common.utils.uuid.IdUtils;
|
import com.ruoyi.common.utils.uuid.IdUtils;
|
||||||
import com.ruoyi.common.utils.uuid.Seq;
|
import com.ruoyi.common.utils.uuid.Seq;
|
||||||
import com.ruoyi.common.utils.uuid.UUID;
|
import com.ruoyi.common.utils.uuid.UUID;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.apache.commons.io.FilenameUtils;
|
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import static com.ruoyi.common.utils.file.FileOperateUtils.DEFAULT_MAX_SIZE;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件处理工具类
|
* 文件处理工具类
|
||||||
@ -277,7 +284,6 @@ public class FileUtils {
|
|||||||
|
|
||||||
public static final File getAbsoluteFile(String filePath) throws IOException {
|
public static final File getAbsoluteFile(String filePath) throws IOException {
|
||||||
File desc = new File(filePath);
|
File desc = new File(filePath);
|
||||||
|
|
||||||
if (!desc.exists()) {
|
if (!desc.exists()) {
|
||||||
if (!desc.getParentFile().exists()) {
|
if (!desc.getParentFile().exists()) {
|
||||||
desc.getParentFile().mkdirs();
|
desc.getParentFile().mkdirs();
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.ruoyi.middleware.minio.service;
|
package com.ruoyi.middleware.minio.service;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -8,7 +7,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.ruoyi.common.config.RuoYiConfig;
|
|
||||||
import com.ruoyi.common.core.domain.entity.FileEntity;
|
import com.ruoyi.common.core.domain.entity.FileEntity;
|
||||||
import com.ruoyi.common.service.file.FileService;
|
import com.ruoyi.common.service.file.FileService;
|
||||||
import com.ruoyi.common.utils.file.FileOperateUtils;
|
import com.ruoyi.common.utils.file.FileOperateUtils;
|
||||||
@ -23,35 +21,24 @@ import com.ruoyi.middleware.minio.utils.MinioUtil;
|
|||||||
@Component("file:strategy:minio")
|
@Component("file:strategy:minio")
|
||||||
@ConditionalOnProperty(prefix = "minio", name = { "enable" }, havingValue = "true", matchIfMissing = false)
|
@ConditionalOnProperty(prefix = "minio", name = { "enable" }, havingValue = "true", matchIfMissing = false)
|
||||||
public class MinioFileService implements FileService {
|
public class MinioFileService implements FileService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MinioConfig minioConfig;
|
private MinioConfig minioConfig;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String upload(String filePath, MultipartFile file) throws Exception {
|
public String upload(String filePath, MultipartFile file) throws Exception {
|
||||||
|
return upload(minioConfig.getPrimary(), filePath, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String upload(String baseDir, String filePath, MultipartFile file) throws Exception {
|
||||||
String relativePath = null;
|
String relativePath = null;
|
||||||
if (FileUtils.isAbsolutePath(filePath)) {
|
if (FileUtils.isAbsolutePath(filePath)) {
|
||||||
relativePath = FileUtils.getRelativePath(filePath);
|
relativePath = FileUtils.getRelativePath(filePath);
|
||||||
} else {
|
} else {
|
||||||
relativePath = filePath;
|
relativePath = filePath;
|
||||||
}
|
}
|
||||||
|
return MinioUtil.uploadFile(baseDir, relativePath, file);
|
||||||
return MinioUtil.uploadFile(minioConfig.getPrimary(), relativePath, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String upload(MultipartFile file, String name) throws Exception {
|
|
||||||
return MinioUtil.uploadFile(minioConfig.getPrimary(), name, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String upload(MultipartFile file) throws Exception {
|
|
||||||
String filePath = RuoYiConfig.getProfile() + File.separator + FileUtils.fastFilePath(file);
|
|
||||||
return upload(filePath, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String upload(String baseDir, String fileName, MultipartFile file) throws Exception {
|
|
||||||
return upload(baseDir + File.pathSeparator + fileName, file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -67,13 +54,6 @@ public class MinioFileService implements FileService {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取文件
|
|
||||||
*
|
|
||||||
* @param filePath
|
|
||||||
* @return
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public FileEntity getFile(String filePath) throws Exception {
|
public FileEntity getFile(String filePath) throws Exception {
|
||||||
return MinioUtil.getFile(minioConfig.getPrimary(), filePath);
|
return MinioUtil.getFile(minioConfig.getPrimary(), filePath);
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package com.ruoyi.middleware.minio.utils;
|
package com.ruoyi.middleware.minio.utils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.ruoyi.common.utils.DateUtils;
|
import com.ruoyi.common.utils.DateUtils;
|
||||||
import com.ruoyi.common.utils.file.FileUtils;
|
import com.ruoyi.common.utils.file.FileUtils;
|
||||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.ruoyi.alibaba.oss.service;
|
package com.ruoyi.alibaba.oss.service;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -11,7 +10,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
import com.ruoyi.alibaba.oss.config.AliOssConfig;
|
import com.ruoyi.alibaba.oss.config.AliOssConfig;
|
||||||
import com.ruoyi.alibaba.oss.domain.AliOssFileVO;
|
import com.ruoyi.alibaba.oss.domain.AliOssFileVO;
|
||||||
import com.ruoyi.alibaba.oss.utils.AliOssUtil;
|
import com.ruoyi.alibaba.oss.utils.AliOssUtil;
|
||||||
import com.ruoyi.common.config.RuoYiConfig;
|
|
||||||
import com.ruoyi.common.core.domain.entity.FileEntity;
|
import com.ruoyi.common.core.domain.entity.FileEntity;
|
||||||
import com.ruoyi.common.service.file.FileService;
|
import com.ruoyi.common.service.file.FileService;
|
||||||
import com.ruoyi.common.utils.file.FileOperateUtils;
|
import com.ruoyi.common.utils.file.FileOperateUtils;
|
||||||
@ -28,32 +26,18 @@ public class AliOssFileService implements FileService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String upload(String filePath, MultipartFile file) throws Exception {
|
public String upload(String filePath, MultipartFile file) throws Exception {
|
||||||
|
return upload(aliOssConfig.getPrimary(), filePath, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String upload(String baseDir, String filePath, MultipartFile file) throws Exception {
|
||||||
String relativePath = null;
|
String relativePath = null;
|
||||||
if (FileUtils.isAbsolutePath(filePath)) {
|
if (FileUtils.isAbsolutePath(filePath)) {
|
||||||
relativePath = FileUtils.getRelativePath(filePath);
|
relativePath = FileUtils.getRelativePath(filePath);
|
||||||
} else {
|
} else {
|
||||||
relativePath = filePath;
|
relativePath = filePath;
|
||||||
}
|
}
|
||||||
|
return AliOssUtil.uploadFile(baseDir, relativePath, file);
|
||||||
String fullPath = AliOssUtil.uploadFile(aliOssConfig.getPrimary(), relativePath, file);
|
|
||||||
return extractFileName(fullPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String upload(MultipartFile file, String name) throws Exception {
|
|
||||||
String fullPath = AliOssUtil.uploadFile(aliOssConfig.getPrimary(), name, file);
|
|
||||||
return extractFileName(fullPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String upload(MultipartFile file) throws Exception {
|
|
||||||
String filePath = RuoYiConfig.getProfile() + File.separator + FileUtils.fastFilePath(file);
|
|
||||||
return upload(filePath, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String upload(String baseDir, String fileName, MultipartFile file) throws Exception {
|
|
||||||
return upload(baseDir + File.pathSeparator + fileName, file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -63,38 +47,14 @@ public class AliOssFileService implements FileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteFile(String fileUrl) throws Exception {
|
public boolean deleteFile(String filePath) throws Exception {
|
||||||
AliOssUtil.removeFile(aliOssConfig.getPrimary(), fileUrl);
|
AliOssUtil.removeFile(aliOssConfig.getPrimary(), filePath);
|
||||||
FileOperateUtils.deleteFileAndMd5ByFilePath(fileUrl);
|
FileOperateUtils.deleteFileAndMd5ByFilePath(filePath);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取文件
|
|
||||||
*
|
|
||||||
* @param filePath
|
|
||||||
* @return
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public FileEntity getFile(String filePath) throws Exception {
|
public FileEntity getFile(String filePath) throws Exception {
|
||||||
return AliOssUtil.getFile(filePath);
|
return AliOssUtil.getFile(filePath);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* 提取文件名
|
|
||||||
*
|
|
||||||
* @param fullPath 完整的文件路径
|
|
||||||
* @return 文件名
|
|
||||||
*/
|
|
||||||
private String extractFileName(String fullPath) {
|
|
||||||
if (fullPath == null || !fullPath.contains("fileName=")) {
|
|
||||||
return fullPath;
|
|
||||||
}
|
|
||||||
int startIndex = fullPath.indexOf("fileName=") + "fileName=".length();
|
|
||||||
if (startIndex < 0 || startIndex >= fullPath.length()) {
|
|
||||||
return fullPath; // 如果没有找到 "fileName=",则返回原路径
|
|
||||||
}
|
|
||||||
return fullPath.substring(startIndex);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -108,7 +108,7 @@ public class AliOssUtil {
|
|||||||
* @throws IOException 比如读写文件出错时
|
* @throws IOException 比如读写文件出错时
|
||||||
*/
|
*/
|
||||||
public static AliOssFileVO getFile(String filePath) throws Exception {
|
public static AliOssFileVO getFile(String filePath) throws Exception {
|
||||||
return getFile(null, filePath);
|
return getAliOssConfig().getMasterBucket().get(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -120,10 +120,8 @@ public class AliOssUtil {
|
|||||||
* @throws IOException 比如读写文件出错时
|
* @throws IOException 比如读写文件出错时
|
||||||
*/
|
*/
|
||||||
public static AliOssFileVO getFile(String client, String filePath) throws Exception {
|
public static AliOssFileVO getFile(String client, String filePath) throws Exception {
|
||||||
AliOssBucket ossBucket = client == null ? getAliOssConfig().getMasterBucket()
|
AliOssBucket ossBucket = getAliOssConfig().getBucket(client);
|
||||||
: getAliOssConfig().getBucket(client);
|
String bucketName = getAliOssConfig().getBucketName(client);
|
||||||
String bucketName = client == null ? getAliOssConfig().getMasterBucketName()
|
|
||||||
: getAliOssConfig().getBucketName(client);
|
|
||||||
|
|
||||||
if (bucketName == null) {
|
if (bucketName == null) {
|
||||||
throw new AliOssClientErrorException("参数 \"bucketName\" 为空指针");
|
throw new AliOssClientErrorException("参数 \"bucketName\" 为空指针");
|
||||||
|
Loading…
Reference in New Issue
Block a user