Files
ecom-dmb/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/utils/FileUploadUtils.java
T

150 lines
4.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package top.xyquan.ecomdmbbackend.utils;
import org.springframework.web.multipart.MultipartFile;
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
/**
* 文件上传工具类
*/
public class FileUploadUtils {
// 上传目录
private static final String UPLOAD_DIR = "uploads";
// 图片目录
private static final String IMAGE_DIR = "images";
// 文件目录
private static final String FILE_DIR = "files";
// 头像目录
private static final String AVATAR_DIR = "avatar";
// Logo目录
private static final String LOGO_DIR = "logo";
/**
* 上传图片
*/
public static String uploadImage(MultipartFile file) {
return uploadFile(file, IMAGE_DIR);
}
/**
* 上传文件
*/
public static String uploadFile(MultipartFile file) {
return uploadFile(file, FILE_DIR);
}
/**
* 上传头像
*/
public static String uploadAvatar(MultipartFile file) {
return uploadFile(file, AVATAR_DIR);
}
/**
* 上传Logo
*/
public static String uploadLogo(MultipartFile file) {
return uploadFile(file, LOGO_DIR);
}
/**
* 上传文件到指定目录
*/
private static String uploadFile(MultipartFile file, String subDir) {
if (file == null || file.isEmpty()) {
throw new BusinessException("文件不能为空");
}
// 检查文件大小(限制为50MB)
if (file.getSize() > 50 * 1024 * 1024) {
throw new BusinessException("文件大小不能超过50MB");
}
try {
// 创建上传目录
String datePath = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
String uploadPath = UPLOAD_DIR + "/" + subDir + "/" + datePath;
Path dirPath = Paths.get(uploadPath);
if (!Files.exists(dirPath)) {
Files.createDirectories(dirPath);
}
// 生成唯一文件名
String originalFilename = file.getOriginalFilename();
String extension = "";
if (originalFilename != null && originalFilename.contains(".")) {
extension = originalFilename.substring(originalFilename.lastIndexOf("."));
}
String filename = UUID.randomUUID().toString().replace("-", "") + extension;
// 保存文件
Path filePath = dirPath.resolve(filename);
Files.copy(file.getInputStream(), filePath);
// 返回相对路径
return "/" + uploadPath + "/" + filename;
} catch (IOException e) {
throw new BusinessException("文件上传失败: " + e.getMessage());
}
}
/**
* 删除文件
*/
public static void deleteFile(String filePath) {
if (filePath == null || filePath.isEmpty()) {
return;
}
try {
// 移除开头的斜杠
if (filePath.startsWith("/")) {
filePath = filePath.substring(1);
}
Path path = Paths.get(filePath);
if (Files.exists(path)) {
Files.delete(path);
}
} catch (IOException e) {
// 删除失败不影响主流程
System.err.println("删除文件失败: " + e.getMessage());
}
}
/**
* 获取文件扩展名
*/
public static String getFileExtension(String filename) {
if (filename == null || filename.isEmpty()) {
return "";
}
int lastDotIndex = filename.lastIndexOf(".");
if (lastDotIndex > 0) {
return filename.substring(lastDotIndex + 1).toLowerCase();
}
return "";
}
/**
* 检查是否为图片文件
*/
public static boolean isImageFile(String filename) {
String extension = getFileExtension(filename);
return extension.matches("(jpg|jpeg|png|gif|bmp|webp)");
}
}