package com.ruoyi.web.controller.common;
|
|
import java.io.File;
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLDecoder;
|
import java.net.URLEncoder;
|
import java.nio.charset.StandardCharsets;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.nio.file.StandardCopyOption;
|
import java.util.*;
|
import java.util.List;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
import javax.imageio.IIOImage;
|
import javax.imageio.ImageWriteParam;
|
import javax.imageio.ImageWriter;
|
import javax.imageio.stream.MemoryCacheImageOutputStream;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import javax.websocket.server.PathParam;
|
|
import com.ruoyi.common.annotation.Anonymous;
|
import com.ruoyi.common.utils.RenamedMultipartFile;
|
import com.ruoyi.common.utils.uuid.UUID;
|
import com.ruoyi.service.DownLoadFileService;
|
import com.ruoyi.service.impl.VideoProcessService;
|
import lombok.Data;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.http.MediaType;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
import com.ruoyi.common.config.RuoYiConfig;
|
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.file.FileUploadUtils;
|
import com.ruoyi.common.utils.file.FileUtils;
|
import com.ruoyi.framework.config.ServerConfig;
|
import springfox.bean.validators.plugins.schema.NotNullAnnotationPlugin;
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.*;
|
import java.awt.image.BufferedImage;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
|
import java.util.Base64;
|
|
import java.util.Arrays;
|
import java.util.HashSet;
|
import java.util.Set;
|
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipOutputStream;
|
|
|
/**
|
* 通用请求处理
|
*
|
* @author ruoyi
|
*/
|
@RestController
|
@RequestMapping("/common")
|
public class CommonController {
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
|
@Autowired
|
private ServerConfig serverConfig;
|
|
@Autowired
|
private DownLoadFileService downLoadFileService;
|
|
|
@Autowired
|
private VideoProcessService videoProcessService;
|
|
private static final String FILE_DELIMETER = ",";
|
|
private static final Pattern CHINESE_PATTERN = Pattern.compile("[\u4e00-\u9fa5]");
|
|
// 缩略图配置
|
@Value("${thumbnail.default-width:300}")
|
private int defaultThumbnailWidth;
|
|
@Value("${thumbnail.default-height:200}")
|
private int defaultThumbnailHeight;
|
|
@Value("${thumbnail.quality:0.8}")
|
private double thumbnailQuality;
|
|
@Value("${thumbnail.cache-dir:./cache/thumbnails}")
|
private String thumbnailCacheDir;
|
|
|
@Value("${thumbnail.max-width:1920}")
|
private int maxThumbnailWidth;
|
|
@Value("${thumbnail.max-height:1080}")
|
private int maxThumbnailHeight;
|
|
@Value("${thumbnail.format:jpg}")
|
private String thumbnailFormat;
|
|
@Value("${thumbnail.keep-aspect-ratio:true}")
|
private boolean keepAspectRatio;
|
|
// 支持的图片格式
|
private static final String[] IMAGE_FORMATS = {"jpg", "jpeg", "png", "gif", "bmp", "webp"};
|
|
// 支持的视频格式
|
private static final String[] VIDEO_FORMATS = {"mp4", "avi", "mov", "wmv", "flv", "mkv", "webm"};
|
@Autowired
|
private NotNullAnnotationPlugin notNullPlugin;
|
|
@Anonymous
|
@GetMapping("/generateThumbnail")
|
public AjaxResult generateThumbnail(@PathParam(value = "url") String url) throws Exception {
|
|
return AjaxResult.success( );
|
}
|
// @GetMapping("/downloadFile")
|
// public void fileDownload(@PathParam("path") String path, HttpServletResponse response)
|
// {
|
// path=path.substring(8);
|
// try
|
// {
|
// if (!FileUtils.checkAllowDownload(path))
|
// {
|
// throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", path));
|
// }
|
// String realFileName = System.currentTimeMillis() + path.substring(path.indexOf("_") + 1);
|
// String filePath = RuoYiConfig.getProfile() + path;
|
//
|
// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
// FileUtils.setAttachmentResponseHeader(response, realFileName);
|
// FileUtils.writeBytes(filePath, response.getOutputStream());
|
//
|
// }
|
// catch (Exception e)
|
// {
|
// log.error("下载文件失败", e);
|
// }
|
// }
|
|
|
// /**
|
// * 通用下载请求
|
// *
|
// * @param fileName 文件名称
|
// * @param delete 是否删除
|
// */
|
// @GetMapping("/download")
|
// public void fileDownload(@PathParam("fileName") String fileName, @PathParam("delete") Boolean delete, HttpServletResponse response)
|
// {
|
// try
|
// {
|
// if (!FileUtils.checkAllowDownload(fileName))
|
// {
|
// throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
|
// }
|
// String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
|
// String filePath = RuoYiConfig.getDownloadPath() + fileName;
|
//
|
// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
// FileUtils.setAttachmentResponseHeader(response, realFileName);
|
// FileUtils.writeBytes(filePath, response.getOutputStream());
|
// if (delete)
|
// {
|
// FileUtils.deleteFile(filePath);
|
// }
|
// }
|
// catch (Exception e)
|
// {
|
// log.error("下载文件失败", e);
|
// }
|
// }
|
@Anonymous
|
|
@GetMapping("/downLoadFile")
|
public void downLoadFile(@PathParam("path") String path, HttpServletResponse response) throws Exception {
|
downLoadFileService.downLoadFile(path, response);
|
}
|
|
|
/**
|
* 通用上传请求(单个)
|
*/
|
@PostMapping("/upload")
|
public AjaxResult uploadFile(@RequestParam("uploadFile") MultipartFile file, String fname) throws Exception {
|
try {
|
// 上传文件路径
|
String filePath = RuoYiConfig.getUploadPath();
|
// 上传并返回新文件名称
|
String fileName = FileUploadUtils.upload(filePath, file, fname);
|
String url = serverConfig.getUrl() + fileName;
|
|
AjaxResult ajax = AjaxResult.success();
|
|
HashMap<String, Object> data = new HashMap<>();
|
data.put("url", url);
|
data.put("fileName", fileName);
|
data.put("newFileName", FileUtils.getName(fileName));
|
data.put("originalFilename", file.getOriginalFilename());
|
|
ajax.put("msg", "操作成功");
|
ajax.put("data", data);
|
return ajax;
|
} catch (Exception e) {
|
return AjaxResult.error(e.getMessage());
|
}
|
}
|
|
|
/**
|
* 通用上传请求(多个)
|
*/
|
@PostMapping("/uploads")
|
public AjaxResult uploadFiles(@RequestParam("files") List<MultipartFile> files) throws Exception
|
{
|
//System.out.println("99999999999999999999999990000000000000000");
|
try
|
{
|
// 上传文件路径
|
String filePath = RuoYiConfig.getUploadPath();
|
List<String> urls = new ArrayList<String>();
|
List<String> fileNames = new ArrayList<String>();
|
List<String> newFileNames = new ArrayList<String>();
|
List<String> originalFilenames = new ArrayList<String>();
|
// System.out.println("99999999999999999999999990000000000000000");
|
// System.out.println(files);
|
// System.out.println(files.size());
|
|
for (MultipartFile file : files)
|
{
|
// 上传并返回新文件名称
|
String filename = "";
|
// System.out.println("1122123330+++++++++++++++++++++++++++++");
|
|
String fileName = FileUploadUtils.upload(filePath, file, filename);
|
String url = serverConfig.getUrl() + fileName;
|
urls.add(url);
|
fileNames.add(fileName);
|
newFileNames.add(FileUtils.getName(fileName));
|
originalFilenames.add(file.getOriginalFilename());
|
}
|
AjaxResult ajax = AjaxResult.success();
|
// System.out.println("99999999999999999999999990000000000000000");
|
|
ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
|
ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
|
ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
|
ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
|
// System.out.println("1122123330+++++++++++++++++++++++++++++");
|
return ajax;
|
}
|
catch (Exception e)
|
{
|
return AjaxResult.error(e.getMessage());
|
}
|
}
|
|
/**
|
* 通用上传请求(多个) 将中文修改为其他
|
*/
|
@PostMapping("/noChinese/uploads")
|
public AjaxResult noChineseUploadFiles(@RequestParam("files") List<MultipartFile> files) throws Exception {
|
//System.out.println("99999999999999999999999990000000000000000");
|
try {
|
// 上传文件路径
|
String filePath = RuoYiConfig.getUploadPath();
|
List<String> urls = new ArrayList<String>();
|
List<String> fileNames = new ArrayList<String>();
|
List<String> newFileNames = new ArrayList<String>();
|
List<String> originalFilenames = new ArrayList<String>();
|
|
for (MultipartFile file : files) {
|
originalFilenames.add(file.getOriginalFilename());
|
|
String safeFilename = generateSafeFilename(file.getOriginalFilename());
|
|
MultipartFile renamedFile = new RenamedMultipartFile(file, safeFilename);
|
|
String lastName="";
|
|
String fileName = FileUploadUtils.upload(filePath, renamedFile, lastName);
|
|
String url = serverConfig.getUrl() + fileName;
|
urls.add(url);
|
fileNames.add(fileName);
|
newFileNames.add(FileUtils.getName(fileName));
|
}
|
AjaxResult ajax = AjaxResult.success();
|
|
ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
|
ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
|
ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
|
ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
|
return ajax;
|
} catch (Exception e) {
|
return AjaxResult.error(e.getMessage());
|
}
|
}
|
|
/**
|
* 生成安全文件名(只替换中文部分)
|
*/
|
private String generateSafeFilename(String originalName) {
|
if (originalName == null) {
|
return "";
|
}
|
|
// 1. 获取文件扩展名
|
String extension = "";
|
int dotIndex = originalName.lastIndexOf('.');
|
if (dotIndex > 0) {
|
extension = originalName.substring(dotIndex);
|
originalName = originalName.substring(0, dotIndex);
|
}
|
|
// 2. 只替换中文部分
|
StringBuilder safeName = new StringBuilder();
|
Matcher matcher = CHINESE_PATTERN.matcher(originalName);
|
int lastEnd = 0;
|
|
while (matcher.find()) {
|
// 添加非中文部分
|
safeName.append(originalName, lastEnd, matcher.start());
|
|
// 添加随机字符串替换中文
|
safeName.append(generateRandomString(4));
|
|
lastEnd = matcher.end();
|
}
|
|
// 添加剩余部分
|
safeName.append(originalName.substring(lastEnd));
|
String noSpaceName = safeName.toString().replaceAll("\\s", "");
|
|
// 3. 添加扩展名
|
return noSpaceName + extension;
|
}
|
/**
|
* 生成随机字符串(字母+数字)
|
*/
|
private String generateRandomString(int length) {
|
String uuid = UUID.randomUUID().toString().replace("-", "");
|
return uuid.substring(0, Math.min(length, uuid.length()));
|
}
|
|
|
@PostMapping("/uploads1")
|
public AjaxResult uploadFiles1(@RequestParam("files") List<MultipartFile> files) {
|
try {
|
String filePath = RuoYiConfig.getUploadPath();
|
List<String> urls = new ArrayList<>();
|
List<String> fileNames = new ArrayList<>();
|
List<String> newFileNames = new ArrayList<>();
|
List<String> originalFilenames = new ArrayList<>();
|
List<String> httpSafePaths = new ArrayList<>();
|
|
for (MultipartFile file : files) {
|
// 1. 上传文件
|
String fileName = FileUploadUtils.upload(filePath, file, "");
|
String originalFilename = file.getOriginalFilename();
|
|
// 2. 获取HTTP安全路径
|
String httpSafePath = toHttpPath(fileName);
|
|
// 3. 构建完整URL(确保有斜杠分隔)
|
String baseUrl = serverConfig.getUrl();
|
if (!baseUrl.endsWith("/") && !httpSafePath.startsWith("/")) {
|
baseUrl += "/";
|
}
|
String url = baseUrl + httpSafePath;
|
|
urls.add(url);
|
fileNames.add(fileName);
|
newFileNames.add(FileUtils.getName(fileName));
|
originalFilenames.add(originalFilename);
|
httpSafePaths.add(httpSafePath);
|
}
|
|
AjaxResult ajax = AjaxResult.success();
|
ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
|
ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
|
ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
|
ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
|
ajax.put("httpSafePaths", StringUtils.join(httpSafePaths, FILE_DELIMETER));
|
return ajax;
|
} catch (Exception e) {
|
return AjaxResult.error(e.getMessage());
|
}
|
}
|
|
|
/**
|
* 将包含中文的文件路径转换为 HTTP 安全的 URL 路径
|
*/
|
|
public String toHttpPath(String filePath) {
|
try {
|
// 1. 标准化路径
|
Path normalizedPath = Paths.get(filePath).normalize();
|
|
// 2. 统一使用正斜杠
|
String pathStr = normalizedPath.toString().replace("\\", "/");
|
|
// 3. 分割路径组件
|
String[] parts = pathStr.split("/");
|
StringBuilder encodedPath = new StringBuilder();
|
|
// 4. 对每个组件单独编码并处理空格
|
for (String part : parts) {
|
if (!part.isEmpty()) {
|
// 编码并替换空格为 %20
|
String encodedPart = URLEncoder.encode(part, StandardCharsets.UTF_8.name())
|
.replace("+", "%20");
|
encodedPath.append("/").append(encodedPart);
|
}
|
}
|
|
// 5. 处理绝对路径和相对路径
|
return filePath.startsWith("/") || filePath.startsWith("\\") ?
|
encodedPath.toString() :
|
encodedPath.substring(1);
|
} catch (UnsupportedEncodingException e) {
|
throw new RuntimeException("UTF-8 encoding not supported", e);
|
}
|
}
|
|
/**
|
* 从 HTTP URL 路径还原原始中文路径
|
*/
|
@PostMapping("/getFileName")
|
public String extractFileName(@RequestBody String httpPath) {
|
try {
|
// 1. 处理空值
|
if (httpPath == null || httpPath.trim().isEmpty()) {
|
return "";
|
}
|
|
// 2. 移除URL协议、域名和查询参数
|
String pathOnly = httpPath;
|
|
// 移除协议和域名
|
if (pathOnly.contains("://")) {
|
pathOnly = pathOnly.substring(pathOnly.indexOf("://") + 3);
|
pathOnly = pathOnly.substring(pathOnly.indexOf('/'));
|
}
|
|
// 移除查询参数(如 ?token=123)
|
int queryStart = pathOnly.indexOf('?');
|
if (queryStart > 0) {
|
pathOnly = pathOnly.substring(0, queryStart);
|
}
|
|
// 3. URL解码
|
String decodedPath = URLDecoder.decode(pathOnly, StandardCharsets.UTF_8.name());
|
|
// 4. 提取文件名(处理Windows路径)
|
decodedPath = decodedPath.replace("\\", "/");
|
|
// 获取最后一个非空路径组件
|
int lastSlash = decodedPath.lastIndexOf('/');
|
String fileName = (lastSlash >= 0 && lastSlash < decodedPath.length() - 1) ?
|
decodedPath.substring(lastSlash + 1) : decodedPath;
|
|
// 5. 处理特殊情况(如结尾斜杠)
|
if (fileName.isEmpty()) {
|
// 尝试获取倒数第二个组件
|
int prevSlash = decodedPath.lastIndexOf('/', lastSlash - 1);
|
if (prevSlash >= 0) {
|
fileName = decodedPath.substring(prevSlash + 1, lastSlash);
|
}
|
}
|
|
return fileName;
|
} catch (UnsupportedEncodingException e) {
|
throw new RuntimeException("UTF-8 encoding not supported", e);
|
}
|
}
|
|
|
/**
|
* 本地资源通用下载
|
*/
|
@GetMapping("/download/resource")
|
public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
|
throws Exception {
|
try {
|
if (!FileUtils.checkAllowDownload(resource)) {
|
throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));
|
}
|
// 本地资源路径
|
String localPath = RuoYiConfig.getProfile();
|
// 数据库资源地址
|
String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
|
// 下载名称
|
String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
FileUtils.setAttachmentResponseHeader(response, downloadName);
|
FileUtils.writeBytes(downloadPath, response.getOutputStream());
|
} catch (Exception e) {
|
log.error("下载文件失败", e);
|
}
|
|
}
|
|
|
|
/**
|
* 压缩图片或提取视频封面并返回Base64
|
*
|
* @param file 图片或视频文件
|
* @param width 目标宽度,默认100
|
* @param height 目标高度,默认100
|
* @param quality 图片质量 0.1-1.0,默认0.8
|
* @return Map包含压缩结果和Base64
|
*/
|
@Anonymous
|
@PostMapping(value = "/image/zip", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
public Map<String, Object> compressMediaToBase64(
|
@RequestParam(value = "file", required = false) MultipartFile file,
|
@RequestParam(value = "filePath", required = false) String filePath,
|
@RequestParam(value = "width", defaultValue = "0") int width,
|
@RequestParam(value = "height", defaultValue = "0") int height,
|
@RequestParam(value = "quality", defaultValue = "0.8") float quality) {
|
Map<String, Object> result = new HashMap<>();
|
|
File trueFile;
|
boolean deleteY = true;
|
|
try {
|
// 验证文件
|
if ((file == null || file.isEmpty()) && filePath == null ) {
|
result.put("success", false);
|
result.put("message", "文件不能为空");
|
return result;
|
}
|
|
if ((file == null || file.isEmpty())){
|
deleteY = false;
|
String fileUel = RuoYiConfig.getProfile() + filePath.replace("/profile","");
|
trueFile = new File(fileUel);
|
}else {
|
trueFile = videoProcessService.convertToFile(file);
|
}
|
String fileName = trueFile.getName();
|
if (fileName.isEmpty() && filePath == null) {
|
result.put("success", false);
|
result.put("message", "文件名不能为空");
|
return result;
|
}
|
|
System.out.println("-----------"+fileName);
|
|
// 获取文件扩展名
|
String extension = getFileExtension(fileName).toLowerCase();
|
System.out.println("-----------"+extension);
|
|
// 判断文件类型
|
if (isImageFile(extension)) {
|
// 处理图片文件
|
return videoProcessService.processImage(trueFile, width, height, quality, extension,deleteY);
|
} else if (isVideoFile(extension)) {
|
// 处理视频文件
|
return videoProcessService.processVideo(trueFile,width,height,quality,extension,deleteY);
|
} else {
|
result.put("success", false);
|
result.put("message", "不支持的文件格式:" + extension);
|
return result;
|
}
|
|
} catch (Exception e) {
|
log.error("文件处理失败", e);
|
result.put("success", false);
|
result.put("message", "处理失败: " + e.getMessage());
|
return result;
|
}
|
}
|
|
/**
|
* 判断是否是图片文件
|
*/
|
private boolean isImageFile(String extension) {
|
for (String format : IMAGE_FORMATS) {
|
if (format.equalsIgnoreCase(extension)) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
/**
|
* 判断是否是视频文件
|
*/
|
private boolean isVideoFile(String extension) {
|
for (String format : VIDEO_FORMATS) {
|
if (format.equalsIgnoreCase(extension)) {
|
return true;
|
}
|
}
|
return false;
|
}
|
|
/**
|
* 获取文件扩展名
|
*/
|
private String getFileExtension(String fileName) {
|
if (StringUtils.isEmpty(fileName)) {
|
return "";
|
}
|
|
int lastDot = fileName.lastIndexOf('.');
|
if (lastDot > 0 && lastDot < fileName.length() - 1) {
|
return fileName.substring(lastDot + 1).toLowerCase();
|
}
|
return "";
|
}
|
|
|
|
}
|