增加对excel导入文件扩展名的校验

This commit is contained in:
jishenghua
2025-11-14 21:39:59 +08:00
parent c56d7d2e81
commit eae6f266ed
5 changed files with 49 additions and 16 deletions

View File

@@ -39,6 +39,13 @@ public class ExceptionConstants {
**/
public static final int DELETE_FORCE_CONFIRM_CODE = 601;
public static final String DELETE_FORCE_CONFIRM_MSG = "检测到存在依赖数据,不能删除!";
/**
* 文件扩展名必须为xls
**/
public static final int FILE_EXTENSION_ERROR_CODE = 701;
public static final String FILE_EXTENSION_ERROR_MSG = "文件扩展名必须为xls";
/**
* 用户信息
* type = 5
@@ -296,9 +303,6 @@ public class ExceptionConstants {
//序列号和批号只能有一项
public static final int MATERIAL_ENABLE_MUST_ONE_CODE = 8000008;
public static final String MATERIAL_ENABLE_MUST_ONE_MSG = "抱歉,商品条码:%s的序列号和批号不能同时填1";
//抱歉文件扩展名必须为xls
public static final int MATERIAL_EXTENSION_ERROR_CODE = 8000009;
public static final String MATERIAL_EXTENSION_ERROR_MSG = "抱歉文件扩展名必须为xls";
//名称为空
public static final int MATERIAL_NAME_EMPTY_CODE = 8000010;
public static final String MATERIAL_NAME_EMPTY_MSG = "第%s行名称为空";

View File

@@ -1063,17 +1063,17 @@ public class DepotItemController {
String message = "";
try {
String barCodes = "";
//文件合法性校验
Sheet src = null;
try {
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
src = workbook.getSheet(0);
} catch (Exception e) {
message = "导入文件不合法,请检查";
data.put("message", message);
res.code = 400;
res.data = data;
//文件扩展名只能为xls
String fileName = file.getOriginalFilename();
if(StringUtil.isNotEmpty(fileName)) {
String fileExt = fileName.substring(fileName.indexOf(".")+1);
if(!"xls".equals(fileExt)) {
throw new BusinessRunTimeException(ExceptionConstants.FILE_EXTENSION_ERROR_CODE,
ExceptionConstants.FILE_EXTENSION_ERROR_MSG);
}
}
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
Sheet src = workbook.getSheet(0);
if(src.getRows()>1000) {
message = "导入失败明细不能超出1000条";
res.code = 500;

View File

@@ -7,6 +7,7 @@ import com.jsh.erp.base.TableDataInfo;
import com.jsh.erp.datasource.entities.Supplier;
import com.jsh.erp.datasource.entities.User;
import com.jsh.erp.datasource.vo.SupplierSimple;
import com.jsh.erp.exception.BusinessRunTimeException;
import com.jsh.erp.service.SupplierService;
import com.jsh.erp.service.SystemConfigService;
import com.jsh.erp.service.UserService;
@@ -403,9 +404,13 @@ public class SupplierController extends BaseController {
HttpServletRequest request, HttpServletResponse response) throws Exception{
BaseResponseInfo res = new BaseResponseInfo();
try {
supplierService.checkFileExt(file);
supplierService.importVendor(file, request);
res.code = 200;
res.data = "导入成功";
} catch(BusinessRunTimeException e) {
res.code = e.getCode();
res.data = e.getData().get("message");
} catch(Exception e){
logger.error(e.getMessage(), e);
res.code = 500;
@@ -427,9 +432,13 @@ public class SupplierController extends BaseController {
HttpServletRequest request, HttpServletResponse response) throws Exception{
BaseResponseInfo res = new BaseResponseInfo();
try {
supplierService.checkFileExt(file);
supplierService.importCustomer(file, request);
res.code = 200;
res.data = "导入成功";
} catch(BusinessRunTimeException e) {
res.code = e.getCode();
res.data = e.getData().get("message");
} catch(Exception e){
logger.error(e.getMessage(), e);
res.code = 500;
@@ -451,9 +460,13 @@ public class SupplierController extends BaseController {
HttpServletRequest request, HttpServletResponse response) throws Exception{
BaseResponseInfo res = new BaseResponseInfo();
try {
supplierService.checkFileExt(file);
supplierService.importMember(file, request);
res.code = 200;
res.data = "导入成功";
} catch(BusinessRunTimeException e) {
res.code = e.getCode();
res.data = e.getData().get("message");
} catch(Exception e){
logger.error(e.getMessage(), e);
res.code = 500;

View File

@@ -571,8 +571,8 @@ public class MaterialService {
if(StringUtil.isNotEmpty(fileName)) {
String fileExt = fileName.substring(fileName.indexOf(".")+1);
if(!"xls".equals(fileExt)) {
throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_EXTENSION_ERROR_CODE,
ExceptionConstants.MATERIAL_EXTENSION_ERROR_MSG);
throw new BusinessRunTimeException(ExceptionConstants.FILE_EXTENSION_ERROR_CODE,
ExceptionConstants.FILE_EXTENSION_ERROR_MSG);
}
}
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
@@ -1518,4 +1518,4 @@ public class MaterialService {
}
return list;
}
}
}

View File

@@ -430,6 +430,22 @@ public class SupplierService {
return map;
}
/**
* 校验文件格式
* @param file
*/
public void checkFileExt(MultipartFile file) {
//文件扩展名只能为xls
String fileName = file.getOriginalFilename();
if(StringUtil.isNotEmpty(fileName)) {
String fileExt = fileName.substring(fileName.indexOf(".")+1);
if(!"xls".equals(fileExt)) {
throw new BusinessRunTimeException(ExceptionConstants.FILE_EXTENSION_ERROR_CODE,
ExceptionConstants.FILE_EXTENSION_ERROR_MSG);
}
}
}
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public void importVendor(MultipartFile file, HttpServletRequest request) throws Exception{
String type = "供应商";