mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
初始化项目
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
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 = "static/uploads";
|
||||
|
||||
// 图片目录
|
||||
private static final String IMAGE_DIR = "images";
|
||||
|
||||
// 文件目录
|
||||
private static final String FILE_DIR = "files";
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
*/
|
||||
public static String uploadImage(MultipartFile file) {
|
||||
return uploadFile(file, IMAGE_DIR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
public static String uploadFile(MultipartFile file) {
|
||||
return uploadFile(file, FILE_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)");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user