Commit 06afdd10 authored by fangxinjiang's avatar fangxinjiang

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

parent a30e3090
......@@ -200,10 +200,9 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T
//获取要导出的列表
List<TDispatchInfoExportVo> list = new ArrayList<>();
long count = noPageCountDiy(searchVo);
ServletOutputStream out = null;
HashMap<String, String> userMap = new HashMap<>();
ExcelWriter excelWriter = null;
try {
// 获取用户信息
R<AllUserNaVo> userListR = upmsDaprUtils.getAllUserName();
if (Common.isNotNull(userListR)) {
AllUserNaVo userList = userListR.getData();
......@@ -211,26 +210,28 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T
userMap = userList.getUserNames();
}
}
out = response.getOutputStream();
// 设置响应头
response.setContentType(CommonConstants.MULTIPART_FORM_DATA);
response.setCharacterEncoding("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);
// 获取所有字典type
Map<String, String> nameAndDicTypeMap = util.getConverterDicType();
// 获取所有字典对应的值
Map<String, String> redisLabelMap = (Map<String, String>) RedisUtil.redis.opsForValue().get(CacheConstants.REDIS_DICT_LABLE);
if (redisLabelMap != null) {
redisLabelMap.putAll(userMap);
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,然后文件流会自动关闭
excelWriter = EasyExcel.write(out, TDispatchInfoExportVo.class)
}
// 使用 try-with-resources 确保输出流正确关闭
try (ServletOutputStream out = response.getOutputStream()) {
ExcelWriter excelWriter = EasyExcel.write(out, TDispatchInfoExportVo.class)
.registerConverter(new DictConverter(nameAndDicTypeMap, redisLabelMap))
.includeColumnFieldNames(searchVo.getExportFields()).build();
.includeColumnFieldNames(searchVo.getExportFields())
.build();
int index = 0;
if (count > CommonConstants.ZERO_INT) {
WriteSheet writeSheet;
for (int i = 0; i <= count; ) {
// 获取实际记录
searchVo.setLimitStart(i);
searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
list = exportDispatch(searchVo);
......@@ -248,22 +249,20 @@ public class TDispatchInfoServiceImpl extends ServiceImpl<TDispatchInfoMapper, T
WriteSheet writeSheet = EasyExcel.writerSheet("派单" + index).build();
excelWriter.write(list, writeSheet);
}
if (Common.isNotNull(list)) {
list.clear();
}
out.flush();
if (null != excelWriter) {
excelWriter.finish();
out.flush();
}
} catch (IOException e) {
log.error("导出文件时发生IO异常", e);
throw new RuntimeException("导出文件失败", e); // 可以根据需要转换为业务异常
} catch (Exception e) {
log.error("执行异常", e);
log.error("执行导出时发生未知异常", e);
throw new RuntimeException("导出过程中发生错误", e);
} finally {
try {
if (null != out) {
out.close();
}
} catch (IOException e) {
log.error("执行异常", e);
// 清理列表数据
if (Common.isNotNull(list)) {
list.clear();
}
}
}
......
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