Commit 06afdd10 authored by fangxinjiang's avatar fangxinjiang

代码优化-IO异常处理优化-fxj

parent a30e3090
...@@ -200,10 +200,9 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T ...@@ -200,10 +200,9 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T
//获取要导出的列表 //获取要导出的列表
List<TDispatchInfoExportVo> list = new ArrayList<>(); List<TDispatchInfoExportVo> list = new ArrayList<>();
long count = noPageCountDiy(searchVo); long count = noPageCountDiy(searchVo);
ServletOutputStream out = null;
HashMap<String, String> userMap = new HashMap<>(); HashMap<String, String> userMap = new HashMap<>();
ExcelWriter excelWriter = null;
try { try {
// 获取用户信息
R<AllUserNaVo> userListR = upmsDaprUtils.getAllUserName(); R<AllUserNaVo> userListR = upmsDaprUtils.getAllUserName();
if (Common.isNotNull(userListR)) { if (Common.isNotNull(userListR)) {
AllUserNaVo userList = userListR.getData(); AllUserNaVo userList = userListR.getData();
...@@ -211,59 +210,59 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T ...@@ -211,59 +210,59 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T
userMap = userList.getUserNames(); userMap = userList.getUserNames();
} }
} }
out = response.getOutputStream(); // 设置响应头
response.setContentType(CommonConstants.MULTIPART_FORM_DATA); response.setContentType(CommonConstants.MULTIPART_FORM_DATA);
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
response.setHeader(CommonConstants.CONTENT_DISPOSITION, CommonConstants.ATTACHMENT_FILENAME + URLEncoder.encode(fileName, "UTF-8")); response.setHeader(CommonConstants.CONTENT_DISPOSITION, CommonConstants.ATTACHMENT_FILENAME + URLEncoder.encode(fileName, "UTF-8"));
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,然后文件流会自动关闭 // 初始化Excel工具
ExcelUtil<TDispatchInfoExportVo> util = new ExcelUtil<>(TDispatchInfoExportVo.class); ExcelUtil<TDispatchInfoExportVo> util = new ExcelUtil<>(TDispatchInfoExportVo.class);
// 获取所有字典type
Map<String, String> nameAndDicTypeMap = util.getConverterDicType(); Map<String, String> nameAndDicTypeMap = util.getConverterDicType();
// 获取所有字典对应的值
Map<String, String> redisLabelMap = (Map<String, String>) RedisUtil.redis.opsForValue().get(CacheConstants.REDIS_DICT_LABLE); Map<String, String> redisLabelMap = (Map<String, String>) RedisUtil.redis.opsForValue().get(CacheConstants.REDIS_DICT_LABLE);
redisLabelMap.putAll(userMap); if (redisLabelMap != null) {
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,然后文件流会自动关闭 redisLabelMap.putAll(userMap);
excelWriter = EasyExcel.write(out, TDispatchInfoExportVo.class) }
.registerConverter(new DictConverter(nameAndDicTypeMap, redisLabelMap)) // 使用 try-with-resources 确保输出流正确关闭
.includeColumnFieldNames(searchVo.getExportFields()).build(); try (ServletOutputStream out = response.getOutputStream()) {
int index = 0; ExcelWriter excelWriter = EasyExcel.write(out, TDispatchInfoExportVo.class)
if (count > CommonConstants.ZERO_INT) { .registerConverter(new DictConverter(nameAndDicTypeMap, redisLabelMap))
WriteSheet writeSheet; .includeColumnFieldNames(searchVo.getExportFields())
for (int i = 0; i <= count; ) { .build();
// 获取实际记录
searchVo.setLimitStart(i); int index = 0;
searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT); if (count > CommonConstants.ZERO_INT) {
list = exportDispatch(searchVo); WriteSheet writeSheet;
if (Common.isNotNull(list)) { for (int i = 0; i <= count; ) {
writeSheet = EasyExcel.writerSheet("派单" + index).build(); searchVo.setLimitStart(i);
excelWriter.write(list, writeSheet); searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
index++; list = exportDispatch(searchVo);
} if (Common.isNotNull(list)) {
i = i + CommonConstants.EXCEL_EXPORT_LIMIT; writeSheet = EasyExcel.writerSheet("派单" + index).build();
if (Common.isNotNull(list)) { excelWriter.write(list, writeSheet);
list.clear(); index++;
}
i = i + CommonConstants.EXCEL_EXPORT_LIMIT;
if (Common.isNotNull(list)) {
list.clear();
}
} }
} else {
WriteSheet writeSheet = EasyExcel.writerSheet("派单" + index).build();
excelWriter.write(list, writeSheet);
} }
} else {
WriteSheet writeSheet = EasyExcel.writerSheet("派单" + index).build();
excelWriter.write(list, writeSheet);
}
if (Common.isNotNull(list)) {
list.clear();
}
out.flush();
if (null != excelWriter) {
excelWriter.finish(); excelWriter.finish();
out.flush();
} }
} catch (IOException e) {
log.error("导出文件时发生IO异常", e);
throw new RuntimeException("导出文件失败", e); // 可以根据需要转换为业务异常
} catch (Exception e) { } catch (Exception e) {
log.error("执行异常", e); log.error("执行导出时发生未知异常", e);
throw new RuntimeException("导出过程中发生错误", e);
} finally { } finally {
try { // 清理列表数据
if (null != out) { if (Common.isNotNull(list)) {
out.close(); list.clear();
}
} catch (IOException e) {
log.error("执行异常", e);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment