完善对文件上传的梳理
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>
|
||||
<java.version>17</java.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>
|
||||
<dynamic.version>3.5.2</dynamic.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.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Objects;
|
||||
@ -17,12 +16,10 @@ import com.ruoyi.common.config.RuoYiConfig;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.entity.FileEntity;
|
||||
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.file.FileOperateUtils;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
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")
|
||||
public class DiskFileService implements FileService {
|
||||
|
||||
private static String defaultBaseDir = RuoYiConfig.getProfile();
|
||||
|
||||
public static void setDefaultBaseDir(String defaultBaseDir) {
|
||||
DiskFileService.defaultBaseDir = defaultBaseDir;
|
||||
}
|
||||
|
||||
public static String getDefaultBaseDir() {
|
||||
return defaultBaseDir;
|
||||
@Override
|
||||
public String upload(String filePath, MultipartFile file) throws Exception {
|
||||
return upload(RuoYiConfig.getProfile(), filePath, file);
|
||||
}
|
||||
|
||||
@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();
|
||||
if (fileNamelength > FileUtils.DEFAULT_FILE_NAME_LENGTH) {
|
||||
throw new FileNameLengthLimitExceededException(FileUtils.DEFAULT_FILE_NAME_LENGTH);
|
||||
}
|
||||
|
||||
// String fileName = extractFilename(file);
|
||||
|
||||
String absPath = getAbsoluteFile(filePath).getAbsolutePath();
|
||||
String absPath = getAbsoluteFile(baseDir + File.separator + filePath).getAbsolutePath();
|
||||
file.transferTo(Paths.get(absPath));
|
||||
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
|
||||
@ -88,7 +51,6 @@ public class DiskFileService implements FileService {
|
||||
// 数据库资源地址
|
||||
String downloadPath = localPath + StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX);
|
||||
// 下载名称
|
||||
|
||||
File file = new File(downloadPath);
|
||||
if (!file.exists()) {
|
||||
throw new FileNotFoundException("未找到文件");
|
||||
@ -113,13 +75,6 @@ public class DiskFileService implements FileService {
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
*
|
||||
* @param filePath
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public FileEntity getFile(String filePath) throws Exception {
|
||||
String localPath = RuoYiConfig.getProfile();
|
||||
|
@ -1,25 +1,51 @@
|
||||
package com.ruoyi.common.service.file;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.ruoyi.common.core.domain.entity.FileEntity;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
|
||||
//默认上传下载
|
||||
/**
|
||||
* 文件操作接口
|
||||
*/
|
||||
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 file 文件对象
|
||||
* @return 返回上传成功的文路径
|
||||
* @throws IOException 比如读写文件出错时
|
||||
* @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 文件对象
|
||||
* @return 返回上传成功的文路径
|
||||
* @throws IOException 比如读写文件出错时
|
||||
* @throws Exception 比如读写文件出错时
|
||||
*
|
||||
*/
|
||||
public String upload(MultipartFile file, String name) 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;
|
||||
public String upload(String baseDir, String filePath, MultipartFile file) throws Exception;
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
*
|
||||
* @param filePath 下载的文件路径
|
||||
* @return 返回上传成功的文路径
|
||||
* @throws IOException 比如读写文件出错时
|
||||
* @throws Exception 比如读写文件出错时
|
||||
*
|
||||
*/
|
||||
public InputStream downLoad(String filePath) throws Exception;
|
||||
@ -72,7 +77,7 @@ public interface FileService {
|
||||
*
|
||||
* @param filePath 删除的文件路径
|
||||
* @return 返回上传成功的文路径
|
||||
* @throws IOException 比如读写文件出错时
|
||||
* @throws Exception 比如读写文件出错时
|
||||
*
|
||||
*/
|
||||
public boolean deleteFile(String filePath) throws Exception;
|
||||
@ -80,8 +85,8 @@ public interface FileService {
|
||||
/**
|
||||
* 获取文件
|
||||
*
|
||||
* @param filePath
|
||||
* @return
|
||||
* @param filePath 文件路径
|
||||
* @return 文件对象
|
||||
* @throws Exception
|
||||
*/
|
||||
public FileEntity getFile(String filePath) throws Exception;
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.ruoyi.common.utils.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@ -33,8 +32,15 @@ public class FileOperateUtils {
|
||||
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 文件路径
|
||||
* @throws Exception
|
||||
*/
|
||||
public static final String upload(MultipartFile file) throws IOException {
|
||||
public static final String upload(MultipartFile file, String[] allowedExtension) throws IOException {
|
||||
try {
|
||||
String md5 = Md5Utils.getMd5(file);
|
||||
String pathForMd5 = FileOperateUtils.getFilePathForMd5(md5);
|
||||
if (StringUtils.isNotEmpty(pathForMd5)) {
|
||||
return pathForMd5;
|
||||
}
|
||||
FileUtils.assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||
FileUtils.assertAllowed(file, allowedExtension);
|
||||
String pathFileName = fileService.upload(file);
|
||||
FileOperateUtils.saveFileAndMd5(pathFileName, md5);
|
||||
return pathFileName;
|
||||
@ -68,34 +74,74 @@ public class FileOperateUtils {
|
||||
* @return 文件名称
|
||||
* @throws IOException
|
||||
*/
|
||||
public static final String upload(String filePath, MultipartFile file, String[] allowedExtension) throws Exception {
|
||||
String md5 = Md5Utils.getMd5(file);
|
||||
String pathForMd5 = FileOperateUtils.getFilePathForMd5(md5);
|
||||
if (StringUtils.isNotEmpty(pathForMd5)) {
|
||||
return pathForMd5;
|
||||
}
|
||||
FileUtils.assertAllowed(file, allowedExtension);
|
||||
fileService.upload(filePath, file);
|
||||
FileOperateUtils.saveFileAndMd5(filePath, md5);
|
||||
return filePath;
|
||||
public static final String upload(String filePath, MultipartFile file) throws Exception {
|
||||
return upload(filePath, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件路径上传
|
||||
*
|
||||
* @param baseDir 相对应用的基目录
|
||||
* @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 pathForMd5 = FileOperateUtils.getFilePathForMd5(md5);
|
||||
if (StringUtils.isNotEmpty(pathForMd5)) {
|
||||
return pathForMd5;
|
||||
}
|
||||
FileUtils.assertAllowed(file, allowedExtension);
|
||||
fileService.upload(filePath, file);
|
||||
FileOperateUtils.saveFileAndMd5(filePath, md5);
|
||||
return filePath;
|
||||
} catch (Exception e) {
|
||||
throw new IOException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件路径上传
|
||||
*
|
||||
* @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 fileName 上传文件名
|
||||
* @param allowedExtension 允许的扩展名
|
||||
* @return 文件名称
|
||||
* @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)
|
||||
throws IOException {
|
||||
try {
|
||||
String filePath = baseDir + File.separator + fileName;
|
||||
return upload(filePath, file, allowedExtension);
|
||||
String md5 = Md5Utils.getMd5(file);
|
||||
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) {
|
||||
throw new IOException(e.getMessage(), e);
|
||||
}
|
||||
@ -148,7 +194,7 @@ public class FileOperateUtils {
|
||||
* 根据md5获取文件的路径
|
||||
*
|
||||
* @param md5 文件的md5
|
||||
* @return
|
||||
* @return 文件路径
|
||||
*/
|
||||
public static String getFilePathForMd5(String md5) {
|
||||
return CacheUtils.get(CacheConstants.FILE_MD5_PATH_KEY, md5, String.class);
|
||||
@ -166,7 +212,7 @@ public class FileOperateUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件的md5
|
||||
* 根据md5删除文件的缓存信息
|
||||
*
|
||||
* @param md5 文件的md5
|
||||
*/
|
||||
@ -178,6 +224,11 @@ public class FileOperateUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件路径删除文件的缓存信息
|
||||
*
|
||||
* @param filePath 文件的路径
|
||||
*/
|
||||
public static void deleteFileAndMd5ByFilePath(String filePath) {
|
||||
String md5ByFilePath = CacheUtils.get(CacheConstants.FILE_PATH_MD5_KEY, filePath, String.class);
|
||||
if (StringUtils.isNotEmpty(md5ByFilePath)) {
|
||||
|
@ -1,5 +1,24 @@
|
||||
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.constant.Constants;
|
||||
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.Seq;
|
||||
import com.ruoyi.common.utils.uuid.UUID;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
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 {
|
||||
File desc = new File(filePath);
|
||||
|
||||
if (!desc.exists()) {
|
||||
if (!desc.getParentFile().exists()) {
|
||||
desc.getParentFile().mkdirs();
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.ruoyi.middleware.minio.service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
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.web.multipart.MultipartFile;
|
||||
|
||||
import com.ruoyi.common.config.RuoYiConfig;
|
||||
import com.ruoyi.common.core.domain.entity.FileEntity;
|
||||
import com.ruoyi.common.service.file.FileService;
|
||||
import com.ruoyi.common.utils.file.FileOperateUtils;
|
||||
@ -23,35 +21,24 @@ import com.ruoyi.middleware.minio.utils.MinioUtil;
|
||||
@Component("file:strategy:minio")
|
||||
@ConditionalOnProperty(prefix = "minio", name = { "enable" }, havingValue = "true", matchIfMissing = false)
|
||||
public class MinioFileService implements FileService {
|
||||
|
||||
@Autowired
|
||||
private MinioConfig minioConfig;
|
||||
|
||||
@Override
|
||||
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;
|
||||
if (FileUtils.isAbsolutePath(filePath)) {
|
||||
relativePath = FileUtils.getRelativePath(filePath);
|
||||
} else {
|
||||
relativePath = filePath;
|
||||
}
|
||||
|
||||
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);
|
||||
return MinioUtil.uploadFile(baseDir, relativePath, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -67,13 +54,6 @@ public class MinioFileService implements FileService {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
*
|
||||
* @param filePath
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public FileEntity getFile(String filePath) throws Exception {
|
||||
return MinioUtil.getFile(minioConfig.getPrimary(), filePath);
|
||||
|
@ -1,7 +1,9 @@
|
||||
package com.ruoyi.middleware.minio.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.ruoyi.alibaba.oss.service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
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.domain.AliOssFileVO;
|
||||
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.service.file.FileService;
|
||||
import com.ruoyi.common.utils.file.FileOperateUtils;
|
||||
@ -28,32 +26,18 @@ public class AliOssFileService implements FileService {
|
||||
|
||||
@Override
|
||||
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;
|
||||
if (FileUtils.isAbsolutePath(filePath)) {
|
||||
relativePath = FileUtils.getRelativePath(filePath);
|
||||
} else {
|
||||
relativePath = filePath;
|
||||
}
|
||||
|
||||
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);
|
||||
return AliOssUtil.uploadFile(baseDir, relativePath, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -63,38 +47,14 @@ public class AliOssFileService implements FileService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteFile(String fileUrl) throws Exception {
|
||||
AliOssUtil.removeFile(aliOssConfig.getPrimary(), fileUrl);
|
||||
FileOperateUtils.deleteFileAndMd5ByFilePath(fileUrl);
|
||||
public boolean deleteFile(String filePath) throws Exception {
|
||||
AliOssUtil.removeFile(aliOssConfig.getPrimary(), filePath);
|
||||
FileOperateUtils.deleteFileAndMd5ByFilePath(filePath);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
*
|
||||
* @param filePath
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public FileEntity getFile(String filePath) throws Exception {
|
||||
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 比如读写文件出错时
|
||||
*/
|
||||
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 比如读写文件出错时
|
||||
*/
|
||||
public static AliOssFileVO getFile(String client, String filePath) throws Exception {
|
||||
AliOssBucket ossBucket = client == null ? getAliOssConfig().getMasterBucket()
|
||||
: getAliOssConfig().getBucket(client);
|
||||
String bucketName = client == null ? getAliOssConfig().getMasterBucketName()
|
||||
: getAliOssConfig().getBucketName(client);
|
||||
AliOssBucket ossBucket = getAliOssConfig().getBucket(client);
|
||||
String bucketName = getAliOssConfig().getBucketName(client);
|
||||
|
||||
if (bucketName == null) {
|
||||
throw new AliOssClientErrorException("参数 \"bucketName\" 为空指针");
|
||||
|
Loading…
Reference in New Issue
Block a user