Convert excel format exception.You can try specifying the ‘excelType‘ yourself解决方案

Convert excel format exception.You can try specifying the 'excelType' yourself报错解决方案


需求是 需要上传文件到ftp上,生成工单文件夹,文件放在工单文件夹里边

这个需求需要在数据库中暂存二进制文件,因为工单生成在文件上传之后,同时也要解析动态excel,所以需要暂存数据库,上传完文件到FTP后删除。

错误代码如下:

 //上传并解析excel
        for (SelfServiceAnalysisFileBufferEntity selfServiceAnalysisFileBuffer : fileBufferList) {
            String itCode = UserUtils.getUserItCode();
            var date = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
            String ftpPath = DMMSFolderPrefixEnum.DMMS_SSA + "/" + SSAFolderTypeEnum.USER.getName() + "/" + workOrderId;
            //首先存入FileMapping表中
            SelfServiceAnalysisFileMappingEntity fileMapping = new SelfServiceAnalysisFileMappingEntity();
            fileMapping.setFileName(selfServiceAnalysisFileBuffer.getFileName());
            fileMapping.setFtpPath(ftpPath);
            SetValueUtils.setMethodCreateValVoid(fileMapping, itCode, date);
            selfServiceAnalysisFileMappingMapper.add(fileMapping);
            //处理fileId
            fileMapping.setFileId(SerialNumberUtils.serialNumberGenerate(fileMapping.getId()));
            selfServiceAnalysisFileMappingMapper.update(fileMapping);
            //如果pgsql存在说明需要解析上传到ftp中,如果不存在不需要重新解析上传ftp
            InputStream inputStream = new ByteArrayInputStream((byte[]) selfServiceAnalysisFileBuffer.getFileContent());
            //因为EasyExcel解析完excel之后流为空了 所以必须copy一份源流数据,不然上传的文件为空文件,然后 上传ftp,
            InputStream inputStream2 = new ByteArrayInputStream((byte[]) selfServiceAnalysisFileBuffer.getFileContent());
            try {
                ExcelReader excelReader = EasyExcel.read(inputStream).build();
                excelReader.read(EasyExcel.readSheet(0).registerReadListener(new EasyExcelSSAUserInputListener(selfServiceAnalysisFileInputTitleMapper, selfServiceAnalysisFileInputValueMapper, selfServiceAnalysisUserTableMappingMapper, selfServiceAnalysisUserInputOutputFieldMapper, ruleType, configInputOutputId, ssaRuleId, workOrderId, technicalField, fileMapping.getFileId(), itCode, date)).build());
            } catch (Exception e) {
                throw new BaseException("-999", e.getMessage());
            }
            FTPClientUtils.ftpUpload(ftpIp, ftpUserName, ftpPassword, ftpPath, selfServiceAnalysisFileBuffer.getFileName(), inputStream2);
            //删除pgsql中的源文件
            selfServiceAnalysisFileBufferMapper.delete(selfServiceAnalysisFileBuffer.getId());
        }

主要问题出在 在文件解析之前已经上传FTP了

先上传FTP后,这块然后再去解析 inputStream后提示‘Convert excel format exception.You can try specifying the ‘excelType’ yourself’ 。

然后会搜索相关错误会让你指定excelType因为EasyExcel现在无法智能解析文件流,然后指定ExcelType 为 xlsx重新解析,就会提示‘The supplied file was empty (zero bytes long)

这块证明inputStream为空了,因为已经上传FTP了

正确代码如下:

//上传并解析excel
        for (SelfServiceAnalysisFileBufferEntity selfServiceAnalysisFileBuffer : fileBufferList) {
            String itCode = UserUtils.getUserItCode();
            var date = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
            String ftpPath = DMMSFolderPrefixEnum.DMMS_SSA + "/" + SSAFolderTypeEnum.USER.getName() + "/" + workOrderId;
            //首先存入FileMapping表中
            SelfServiceAnalysisFileMappingEntity fileMapping = new SelfServiceAnalysisFileMappingEntity();
            fileMapping.setFileName(selfServiceAnalysisFileBuffer.getFileName());
            fileMapping.setFtpPath(ftpPath);
            SetValueUtils.setMethodCreateValVoid(fileMapping, itCode, date);
            selfServiceAnalysisFileMappingMapper.add(fileMapping);
            //处理fileId
            fileMapping.setFileId(SerialNumberUtils.serialNumberGenerate(fileMapping.getId()));
            selfServiceAnalysisFileMappingMapper.update(fileMapping);
            //如果pgsql存在说明需要解析上传到ftp中,如果不存在不需要重新解析上传ftp
            InputStream inputStream = new ByteArrayInputStream((byte[]) selfServiceAnalysisFileBuffer.getFileContent());
            try {
                ExcelReader excelReader = EasyExcel.read(inputStream).build();
                excelReader.read(EasyExcel.readSheet(0).registerReadListener(new EasyExcelSSAUserInputListener(selfServiceAnalysisFileInputTitleMapper, selfServiceAnalysisFileInputValueMapper, ssaRuleId, workOrderId, technicalField, fileMapping.getFileId(), itCode, date)).build());
            } catch (Exception e) {
                throw new BaseException("-999", e.getMessage());
            }
            FTPClientUtils.ftpUpload(ftpIp, ftpUserName, ftpPassword, ftpPath, selfServiceAnalysisFileBuffer.getFileName(), inputStream);
            //删除pgsql中的源文件
            selfServiceAnalysisFileBufferMapper.delete(selfServiceAnalysisFileBuffer.getId());
        }

将上传文件的逻辑移动到解析Excel逻辑之后就能正常执行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值