Commit 97f873a3 authored by fangxinjiang's avatar fangxinjiang

代码优化-fxj

parent a787f0c5
...@@ -234,9 +234,12 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T ...@@ -234,9 +234,12 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T
if (redisLabelMap != null) { if (redisLabelMap != null) {
redisLabelMap.putAll(userMap); redisLabelMap.putAll(userMap);
} }
// 使用 try-with-resources 确保输出流正确关闭 // 获取输出流
try (ServletOutputStream out = response.getOutputStream()) { OutputStream out = null;
ExcelWriter excelWriter = EasyExcel.write(out, TDispatchInfoExportVo.class) ExcelWriter excelWriter = null;
try {
out = response.getOutputStream();
excelWriter = EasyExcel.write(out, TDispatchInfoExportVo.class)
.registerConverter(new DictConverter(nameAndDicTypeMap, redisLabelMap)) .registerConverter(new DictConverter(nameAndDicTypeMap, redisLabelMap))
.includeColumnFieldNames(searchVo.getExportFields()) .includeColumnFieldNames(searchVo.getExportFields())
.build(); .build();
...@@ -263,13 +266,19 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T ...@@ -263,13 +266,19 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T
excelWriter.write(list, writeSheet); excelWriter.write(list, writeSheet);
} }
excelWriter.finish();
out.flush();
}
} catch (IOException e) { } catch (IOException e) {
log.error("导出文件时发生IO异常", e); log.error("导出文件时发生IO异常", e);
throw new RuntimeException("导出文件失败", e); // 可以根据需要转换为业务异常 throw new RuntimeException("导出文件失败", e); // 可以根据需要转换为业务异常
} finally {
if (excelWriter != null) {
try {
excelWriter.finish();
} catch (Exception e) { } catch (Exception e) {
log.error("关闭ExcelWriter时发生异常", e);
}
}
}
}catch (Exception e) {
log.error("执行导出时发生未知异常", e); log.error("执行导出时发生未知异常", e);
throw new RuntimeException("导出过程中发生错误", e); throw new RuntimeException("导出过程中发生错误", e);
} finally { } finally {
......
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