添加文件上传视频类型判断——修改上传报错问题
This commit is contained in:
@@ -25,7 +25,7 @@ public class CmsFileJobUploadController extends BaseController {
|
||||
private IFileJobUploadService fileJobUploadService;
|
||||
|
||||
//文件大小限制 10MB
|
||||
private static final long MAX_FILE_SIZE = 100 * 1024 * 1024;
|
||||
private static final long MAX_FILE_SIZE = 1024 * 1024 * 1024;
|
||||
|
||||
@ApiOperation("上传文件")
|
||||
@PreAuthorize("@ss.hasPermi('cms:fileJobUpload:upload')")
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.ruoyi.cms.util.file;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@@ -32,7 +34,16 @@ public class FileValid {
|
||||
"application/vnd.ms-excel",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"application/vnd.ms-powerpoint",
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||
//视频
|
||||
"video/mp4",
|
||||
"video/x-msvideo", // avi
|
||||
"video/quicktime", // mov
|
||||
"video/x-matroska", // mkv
|
||||
"video/x-flv", // flv
|
||||
"video/x-ms-wmv", // wmv
|
||||
"video/webm", // webm
|
||||
"video/x-m4v" // m4v
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -51,22 +62,35 @@ public class FileValid {
|
||||
* @return
|
||||
*/
|
||||
public static boolean validateFile(MultipartFile file) {
|
||||
try {
|
||||
// 白名单配置
|
||||
|
||||
String originalFilename = file.getOriginalFilename().toLowerCase();
|
||||
String contentType = file.getContentType();
|
||||
String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
|
||||
|
||||
// 扩展名 + MIME 校验
|
||||
if (!allowedExt.contains(ext) || !allowedMime.contains(contentType)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
if (file == null || file.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
// 白名单配置
|
||||
String originalFilename = file.getOriginalFilename().toLowerCase();
|
||||
String contentType = file.getContentType();
|
||||
if (originalFilename == null || !originalFilename.contains(".")) {
|
||||
return false;
|
||||
}
|
||||
String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
|
||||
|
||||
// 扩展名 + MIME 校验
|
||||
if (!allowedExt.contains(ext) || !allowedMime.contains(contentType)) {
|
||||
return false;
|
||||
}
|
||||
try (InputStream is = file.getInputStream()) {
|
||||
byte[] header = new byte[16];
|
||||
int read = is.read(header);
|
||||
if (read <= 0) {
|
||||
return false;
|
||||
}
|
||||
String fileMagic = bytesToHex(header);
|
||||
if (!matchMagicByExt(ext, fileMagic)) {
|
||||
return false;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,4 +106,48 @@ public class FileValid {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据后缀匹配合法魔数(视频魔数已补充)
|
||||
*/
|
||||
private static boolean matchMagicByExt(String ext, String magic) {
|
||||
switch (ext) {
|
||||
// 图片
|
||||
case "jpg":
|
||||
case "jpeg":
|
||||
return magic.startsWith("FFD8FF");
|
||||
case "png":
|
||||
return magic.startsWith("89504E47");
|
||||
case "gif":
|
||||
return magic.startsWith("47494638");
|
||||
// PDF
|
||||
case "pdf":
|
||||
return magic.startsWith("25504446");
|
||||
// Office文档
|
||||
case "doc":
|
||||
return magic.startsWith("D0CF11E0");
|
||||
case "docx":
|
||||
case "xlsx":
|
||||
case "pptx":
|
||||
return magic.startsWith("504B0304");
|
||||
// 视频 mp4/mov/m4v
|
||||
case "mp4":
|
||||
case "mov":
|
||||
case "m4v":
|
||||
return magic.contains("6D6F6F76") || magic.contains("66747970");
|
||||
// mkv
|
||||
case "mkv":
|
||||
return magic.startsWith("1A45DFA3");
|
||||
// avi
|
||||
case "avi":
|
||||
return magic.startsWith("52494646") && magic.contains("41564920");
|
||||
// flv
|
||||
case "flv":
|
||||
return magic.startsWith("464C5601");
|
||||
// webm
|
||||
case "webm":
|
||||
return magic.startsWith("1A45DFA3");
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user