EasyExcel上传校验文件错误信息放到文件里以Base64 返回给前端

news/2025/1/13 12:56:01/

产品需求:

前端上传个csv 或 excel 文件,文件共4列,验证文件大小,类型,文件名长度,文件内容,如果某行某个单元格数据验证不通过,就把错误信息放到这行第五列,然后把带有错误信息的文件,返回给前端

    @ApiOperation(value = "上传新增")@PostMapping(path = "uploadFile")public ResultMsg<?> uploadExcelFile(@RequestParam("file") MultipartFile file) throws IOException {if (file.getOriginalFilename().length() > 256) {return ResultMsg.failed("文件名超长");}if (file.getSize() > 3 * 1024 * 1024) {return ResultMsg.failed("文件太大");}String fileType = Optional.ofNullable(file.getOriginalFilename()).map(name -> name.substring(name.lastIndexOf("."))).orElse(null);if (!".xls".equalsIgnoreCase(fileType) && !".xlsx".equalsIgnoreCase(fileType) && !".csv".equalsIgnoreCase(fileType)) {return ResultMsg.failed("仅支持 [xls xlsx csv] 格式");}StyleOfWritingExcelDTO dto = null;try {dto = this.validateAndReadExcel(file.getInputStream());} catch (IOException e) {throw new RuntimeException(e);}if (dto.getHasError()) {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();EasyExcel.write(byteArrayOutputStream, StyleOfWritingExcelModel.class)
//                        .excelType(ExcelTypeEnum.XLSX).sheet("sheet1").doWrite(dto.getStyleOfWritingExcelModelList());String base64Str = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());Map<Object, Object> base64Map = new HashMap<>();base64Map.put("errStr", "文件内容不合格");base64Map.put("fileData", base64Str);return ResultMsg.failed(4001, "文件内容不合格", base64Map);}if (dto.getStyleOfWritingExcelModelList().isEmpty()) return ResultMsg.failed("文件内容为空");saveData(dto.getStyleOfWritingExcelModelList());return ResultMsg.success();}private StyleOfWritingExcelDTO validateAndReadExcel(InputStream inputStream) {StyleOfWritingExcelDTO result = new StyleOfWritingExcelDTO();List<StyleOfWritingExcelModel> dataList = new ArrayList<>();EasyExcel.read(inputStream, StyleOfWritingExcelModel.class, new AnalysisEventListener<StyleOfWritingExcelModel>() {@Overridepublic void invoke(StyleOfWritingExcelModel styleOfWritingExcelModel, AnalysisContext analysisContext) {StringBuilder errorInfo = new StringBuilder();CategoryEntity firstCategory = null;if (StringUtils.isBlank(styleOfWritingExcelModel.getFirstCategoryName())) {errorInfo.append("品类不能为空;");} else if (styleOfWritingExcelModel.getFirstCategoryName().length() > 50) {errorInfo.append(String.format("品类名长度超限,限制最大[%d]字符;", 50));} else {firstCategory = this.getCategoryCode(styleOfWritingExcelModel.getFirstCategoryName());if (ObjectUtils.isEmpty(firstCategory))errorInfo.append(String.format("品类在库里未找到;"));elsestyleOfWritingExcelModel.setFirstCategory(firstCategory.getCode());}CategoryEntity secondCategory = null;if (StringUtils.isBlank(styleOfWritingExcelModel.getSecondCategoryName())) {errorInfo.append("二级品类不能为空; ");} else if (styleOfWritingExcelModel.getSecondCategoryName().length() > 50) {errorInfo.append(String.format("二级品类名长度超限,限制最大[%d]字符", 50));} else {secondCategory = this.getCategoryCode(styleOfWritingExcelModel.getSecondCategoryName());if (ObjectUtils.isEmpty(secondCategory))errorInfo.append(String.format("二级品类在库里未找到;"));elsestyleOfWritingExcelModel.setSecondCategory(secondCategory.getCode());}if (ObjectUtils.isNotEmpty(firstCategory) && ObjectUtils.isNotEmpty(secondCategory)) {if (!firstCategory.getCode().equals(secondCategory.getParentCode())) {errorInfo.append(String.format("二级品类 和 品类 不匹配"));}}if (StringUtils.isBlank(styleOfWritingExcelModel.getContent())) {errorInfo.append("内容不能为空; ");} else if (styleOfWritingExcelModel.getContent().length() > 3000) {errorInfo.append(String.format("内容长度超限,限制最大[%d]字符", 3000));}styleOfWritingExcelModel.setErrorInfo(errorInfo.toString());dataList.add(styleOfWritingExcelModel);if (!result.getHasError() && StringUtils.isNotBlank(errorInfo.toString())) {result.setHasError(true);}}private CategoryEntity getCategoryCode(String categoryName) {List<CategoryEntity> categoryList = categoryService.list(new QueryWrapper<CategoryEntity>().lambda().eq(CategoryEntity::getName, categoryName));return categoryList.stream().findFirst().orElse(null);}@Overridepublic void doAfterAllAnalysed(AnalysisContext context) {// 解析完成后的操作}}).sheet().doRead();result.setStyleOfWritingExcelModelList(dataList);return result;}返回:{"code": 4001,"message": "文件内容不合格","data": {"fileData": "UEsDBC0ACAAIAAAAAAAAAAAAAAAAAAAAAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbLVTy27CMBD8lcjXKjb0UFUVgUMfxxap9ANce5NY+CWvofD3XQc4lFKJCnHyY2ZnZlf2ZLZxtlpDQhN8w8Z8xCrwKmjju4Z9LF7qe1Zhll5LGzw0bAvIZtPJYhsBK6r12LA+5/ggBKoenEQeInhC2pCczHRMnYhSLWUH4nY0uhMq+Aw+17losOnkCVq5srl63N0X6YbJGK1RMlMssfb6SLTeC/IEduBgbyLeEIFVzxtS2bVDKDJxhsNxYTlT3RsNJhkN/4oW2tYo0EGtHJVwKKoadB0TEVM2sM85lym/SkeCgshzQlGQNL/E+zAWFRKcZViIFzkedYsxgdTYA2RnOfYygX7PiV7T7xAbK34Qrpgjb+2JKZQAA3LNCdDKnTT+lPtXSMvPEJbX8y8Ow/4v+wFEMSzjQw4xfO/pN1BLBwiRLCi8OwEAAAAAAAAdBAAAAAAAAFBLAwQtAAgACAAAAAAAAAAAAAAAAAAAAAAACwAAAF9yZWxzLy5yZWxzrZLBSgMxEIZfJcy9m20FEWnaiwi9idQHGJPZ3bCbTEhG3b69wYu2bEHB4zAz3/8xyXY/h0m9Uy6eo4F104KiaNn52Bt4OT6u7kAVwehw4kgGTlRgv9s+04RSV8rgU1GVEYuBQSTda13sQAFLw4li7XScA0otc68T2hF70pu2vdX5JwPOmergDOSDW4M6Yu5JDMyT/uA8vjKPTcXWxinRb0K567ylB7ZvgaIsZF9MgF522Xy7OLZPmesmpvTfMjQLRUdulWoCZfH14leMbhaMLGf6m9L1R9GBBB0KflEvhPTZH9h9AlBLBwhuMghL5QAAAAAAAABKAgAAAAAAAFBLAwQtAAgACAAAAAAAAAAAAAAAAAAAAAAAEAAAAGRvY1Byb3BzL2F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AAAAAAAFBLAQItABQACAAIAAAAAACRLCi8OwEAAB0EAAATAAAAAAAAAAAAAAAAAAAAAABbQ29udGVudF9UeXBlc10ueG1sUEsBAi0AFAAIAAgAAAAAAG4yCEvlAAAASgIAAAsAAAAAAAAAAAAAAAAAhAEAAF9yZWxzLy5yZWxzUEsBAi0AFAAIAAgAAAAAADZugyGTAAAAuAAAABAAAAAAAAAAAAAAAAAAqgIAAGRvY1Byb3BzL2FwcC54bWxQSwECLQAUAAgACAAAAAAAneLlzQcBAACxAQAAEQAAAAAAAAAAAAAAAACDAwAAZG9jUHJvcHMvY29yZS54bWxQSwECLQAUAAgACAAAAAAAaK+3jnkAAACKAAAAFAAAAAAAAAAAAAAAAADRBAAAeGwvc2hhcmVkU3RyaW5ncy54bWxQSwECLQAUAAgACAAAAAAApQaYYgcCAADfBQAADQAAAAAAAAAAAAAAAACUBQAAeGwvc3R5bGVzLnhtbFBLAQItABQACAAIAAAAAACtBMn72gAAAF4BAAAPAAAAAAAAAAAAAAAAAN4HAAB4bC93b3JrYm9vay54bWxQSwECLQAUAAgACAAAAAAAZ+uiqNUAAAA0AgAAGgAAAAAAAAAAAAAAAAD9CAAAeGwvX3JlbHMvd29ya2Jvb2sueG1sLnJlbHNQSwECLQAUAAgACAAAAAAAx2iaZ9wBAADMAwAAGAAAAAAAAAAAAAAAAAAiCgAAeGwvd29ya3NoZWV0cy9zaGVldDEueG1sUEsFBgAAAAAJAAkAPwIAAEwMAAAAAA==","errStr": "文件内容不合格"},"timestamp": 63417752000000
}


http://www.ppmy.cn/news/1562811.html

相关文章

STM32 物联网智能家居 (一) 方案设计STM32+ESP8266+TCP/UDP/MQTT

STM32 物联网智能家居 (一) 方案设计STM32ESP8266TCP/UDP/MQTT 下面我们要开展物联网智能家居的博客专栏&#xff0c;该专栏我们会将STM32各种外设模块I2c、Usart、Wifi、ESP8266、分层编程思想以及调试的方法融入到整个专栏中&#xff0c;让你从一个单片机小白&#xff0c;进…

浅聊MySQL中的LBCC和MVCC

MySQL中的LBCC&#xff08;Lock-Based Concurrency Control&#xff0c;基于锁的并发控制&#xff09;和MVCC&#xff08;Multi-Version Concurrency Control&#xff0c;多版本并发控制&#xff09;是两种不同的并发控制机制&#xff0c;它们在实现方式、作用以及应用场景上存…

MySQL教程之:获取有关数据库和表的信息

如果您忘记了数据库或表的名称&#xff0c;或者给定表的结构是什么&#xff08;例如&#xff0c;它的列叫什么&#xff09;怎么办&#xff1f;MySQL通过几个语句来解决这个问题&#xff0c;这些语句提供了有关它支持的数据库和表的信息。 您之前看过SHOW DATABASES&#xff0c…

隐私计算,构建安全的未来数据空间

大数据产业创新服务媒体 ——聚焦数据 改变商业 在医疗领域&#xff0c;不同医院之间需要共享患者数据&#xff0c;以提供更全面准确的诊断和治疗方案。 传统的数据处理方式通常是数据经过转换隐藏重要数据后再进行分析&#xff0c;虽然可以保护数据隐私&#xff0c;但在数据源…

Guilite字库工具

目录 前言 使用方法 离线字库解析 工具链接 前言 最近通过Qt写了一个Guilite字库工具&#xff0c;相比原始工具&#xff0c;主要有以下几个优点&#xff1a; &#xff08;1&#xff09;支持同时生成多套字库 &#xff08;2&#xff09;支持离线字库生成 &#xff08;3&a…

Hive SQL必刷练习题:连续问题 间断连续

问题描述&#xff1a; 1&#xff09; 连续问题&#xff1a;找出连续三天&#xff08;或者连续几天的啥啥啥&#xff09;。 2&#xff09; 间断连续&#xff1a;统计各用户连续登录最长天数&#xff0c;间断一天也算连续&#xff0c;比如1、3、4、6也算登陆了6天 问题分析&am…

(长期更新)《零基础入门 ArcGIS(ArcScene) 》实验七----城市三维建模与分析(超超超详细!!!)

城市三维建模与分析 三维城市模型已经成为一种非常普遍的地理空间数据资源,成为城市的必需品,对城市能化管理至关重要。语义信息丰富的三维城市模型可以有效实现不同领域数据与IS相信息的高层次集成及互操作,从而在城市规划、环境模拟、应急响应和辅助决策等众多领域公挥作用、…

使用Cilium/eBPF实现大规模云原生网络和安全

大家读完觉得有帮助记得关注和点赞&#xff01;&#xff01;&#xff01; 目录 抽象 1 Trip.com 云基础设施 1.1 分层架构 1.2 更多细节 2 纤毛在 Trip.com 2.1 推出时间表 2.2 自定义 2.3 优化和调整 2.3.1 解耦安装 2.3.2 避免重试/重启风暴 2.3.3 稳定性优先 2…