提交文件校验
This commit is contained in:
@@ -84,6 +84,8 @@ public enum CommonResponseEnum {
|
||||
|
||||
FILE_EXIST("A0096", "文件已存在"),
|
||||
|
||||
FILE_NAME_ERROR("A0096", "文件格式有误"),
|
||||
|
||||
FILE_SIZE_ERROR("A0096", "文件过大"),
|
||||
|
||||
FILE_XLSX_ERROR("A0096", "请上传excel文件"),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.njcn.common.utils;
|
||||
|
||||
import cn.hutool.core.text.StrPool;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@@ -56,5 +57,90 @@ public class FileUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件是否为word格式
|
||||
*
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
public static boolean judgeFileIsWord(String fileName) {
|
||||
// 检查文件名是否为空
|
||||
if (StrUtil.isBlank(fileName)) {
|
||||
return false;
|
||||
}
|
||||
// 获取文件扩展名
|
||||
String fileExtension = getFileExtension(fileName);
|
||||
// 定义支持的文件类型
|
||||
String[] allowedExtensions = {"doc", "docx"};
|
||||
|
||||
// 检查扩展名是否在允许的列表中
|
||||
for (String ext : allowedExtensions) {
|
||||
if (ext.equalsIgnoreCase(fileExtension)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件是否为pdf格式
|
||||
*
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
public static boolean judgeFileIsPdf(String fileName) {
|
||||
// 检查文件名是否为空
|
||||
if (StrUtil.isBlank(fileName)) {
|
||||
return false;
|
||||
}
|
||||
// 获取文件扩展名
|
||||
String fileExtension = getFileExtension(fileName);
|
||||
// 定义支持的文件类型
|
||||
String[] allowedExtensions = {"pdf"};
|
||||
|
||||
// 检查扩展名是否在允许的列表中
|
||||
for (String ext : allowedExtensions) {
|
||||
if (ext.equalsIgnoreCase(fileExtension)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件是否为excel格式
|
||||
*
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
public static boolean judgeFileIsExcel(String fileName) {
|
||||
// 检查文件名是否为空
|
||||
if (StrUtil.isBlank(fileName)) {
|
||||
return false;
|
||||
}
|
||||
// 获取文件扩展名
|
||||
String fileExtension = getFileExtension(fileName);
|
||||
// 定义支持的文件类型
|
||||
String[] allowedExtensions = {"xls", "xlsx"};
|
||||
|
||||
// 检查扩展名是否在允许的列表中
|
||||
for (String ext : allowedExtensions) {
|
||||
if (ext.equalsIgnoreCase(fileExtension)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件名中提取扩展名
|
||||
*
|
||||
* @param fileName 文件名
|
||||
* @return 扩展名
|
||||
*/
|
||||
private static String getFileExtension(String fileName) {
|
||||
int dotIndex = fileName.lastIndexOf('.');
|
||||
if (dotIndex == -1) {
|
||||
return "";
|
||||
}
|
||||
return fileName.substring(dotIndex + 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user