This commit is contained in:
lu7213 committed 2026-03-07 13:34:04 +08:00
1 parent 6c73388fdd
commit 30da249b6b
14 files changed
+540 -69

No files matched your search

@@ -0,0 +1,16 @@
package com.ruoyi.system.service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
public interface IFileUploadService {
String uploadFile(MultipartFile file, String module);
List<String> uploadFiles(List<MultipartFile> files, String module);
boolean deleteFile(String filePath);
String getFileUrl(String filePath);
}
@@ -0,0 +1,172 @@
package com.ruoyi.system.service.impl;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.IFileUploadService;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class FileUploadServiceImpl implements IFileUploadService {
private static final Logger log = LoggerFactory.getLogger(FileUploadServiceImpl.class);
private static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
private static final int DEFAULT_FILE_NAME_LENGTH = 100;
private static final String[] DEFAULT_ALLOWED_EXTENSION = {
"png", "jpg", "jpeg", "gif", "bmp", "webp",
"doc", "docx", "xls", "xlsx", "ppt", "pptx",
"pdf", "txt", "zip", "rar", "7z",
"mp4", "avi", "mov", "wmv", "flv", "mp3", "wav"
};
@Override
public String uploadFile(MultipartFile file, String module) {
try {
String baseDir = getUploadPath(module);
return upload(baseDir, file, DEFAULT_ALLOWED_EXTENSION);
} catch (Exception e) {
log.error("文件上传失败: {}", e.getMessage(), e);
throw new RuntimeException("文件上传失败: " + e.getMessage());
}
}
@Override
public List<String> uploadFiles(List<MultipartFile> files, String module) {
List<String> urls = new ArrayList<>();
for (MultipartFile file : files) {
if (!file.isEmpty()) {
String url = uploadFile(file, module);
urls.add(url);
}
}
return urls;
}
@Override
public boolean deleteFile(String filePath) {
if (StringUtils.isEmpty(filePath)) {
return false;
}
try {
String localPath = RuoYiConfig.getProfile();
String fullPath = localPath + StringUtils.substringAfter(filePath, Constants.RESOURCE_PREFIX);
File file = new File(fullPath);
if (file.exists()) {
return file.delete();
}
} catch (Exception e) {
log.error("删除文件失败: {}", e.getMessage(), e);
}
return false;
}
@Override
public String getFileUrl(String filePath) {
if (StringUtils.isEmpty(filePath)) {
return "";
}
if (filePath.startsWith(Constants.RESOURCE_PREFIX)) {
return filePath;
}
return Constants.RESOURCE_PREFIX + "/" + filePath;
}
private String getUploadPath(String module) {
String basePath = RuoYiConfig.getUploadPath();
if (StringUtils.isNotEmpty(module)) {
return basePath + File.separator + module;
}
return basePath;
}
private String upload(String baseDir, MultipartFile file, String[] allowedExtension)
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
InvalidExtensionException {
int fileNamelength = file.getOriginalFilename().length();
if (fileNamelength > DEFAULT_FILE_NAME_LENGTH) {
throw new FileNameLengthLimitExceededException(DEFAULT_FILE_NAME_LENGTH);
}
assertAllowed(file, allowedExtension);
String fileName = extractFilename(file);
File desc = getAbsoluteFile(baseDir, fileName);
file.transferTo(desc);
String pathFileName = getPathFileName(baseDir, fileName);
return pathFileName;
}
private String extractFilename(MultipartFile file) {
String extension = getExtension(file);
return DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
}
private File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
File desc = new File(uploadDir + File.separator + fileName);
if (!desc.getParentFile().exists()) {
desc.getParentFile().mkdirs();
}
if (!desc.exists()) {
desc.createNewFile();
}
return desc;
}
private String getPathFileName(String uploadDir, String fileName) throws IOException {
int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
}
private void assertAllowed(MultipartFile file, String[] allowedExtension)
throws FileSizeLimitExceededException, InvalidExtensionException {
long size = file.getSize();
if (size > DEFAULT_MAX_SIZE) {
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
}
String fileName = file.getOriginalFilename();
String extension = getExtension(file);
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
throw new InvalidExtensionException(allowedExtension, extension, fileName);
}
}
private boolean isAllowedExtension(String extension, String[] allowedExtension) {
for (String str : allowedExtension) {
if (str.equalsIgnoreCase(extension)) {
return true;
}
}
return false;
}
private String getExtension(MultipartFile file) {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
if (StringUtils.isEmpty(extension)) {
extension = MimeTypeUtils.getExtension(file.getContentType());
}
return extension;
}
}