由于业务需要,获取多个pdf的时候需要将多个pdf存入zip类型的压缩包中实现下载,导出的过程使用轮询的方式实现进度查询,每次操作将当前进度存储入redis中,获取进度的接口从redis查询信息,下面只详细展开zip下载的代码
这个操作步骤主要分两步
返回查询进度的id,操作完成后将把流信息存储入redis,提供下载使用
javaif (!redisUtil.isPing()) {
throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(), "导出失败,Redis服务未连接!");
}
String id = StringUtil.generateId();
redisUtil.set(markKey, id, 3600);
Map<String, Object> map = new HashMap<>();
map.put("current", 0);
map.put("total", list.size());
String mapkey = markKey + id + ":";
redisUtil.set(mapkey, map, 3600);
//单线程线程池
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardPolicy());
try {
poolExecutor.execute(() -> {
List<byte[]> pdfBytesList = new ArrayList<>();
List<String> filenames=new ArrayList<>();
for (PatientRequest p : list) {
byte[] bytes = null;
try {
bytes = getPdfByte(p);
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
Map<String, Object> map1 = redisUtil.get(mapkey, Map.class);
map1.put("current", (Integer) map1.get("current") + 1);
if (ArrayUtil.isNotEmpty(bytes)) {
filenames.add(String.format("%s_%s_%s_%s.pdf", province, name, p.getPatientId(), DateUtil.format(new Date(), "yyyyMMdd")));
pdfBytesList.add(bytes);
}
if (list.indexOf(p) == list.size() - 1) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos)) {
for (int i = 0; i < pdfBytesList.size(); i++) {
byte[] pdfBytes = pdfBytesList.get(i);
ZipEntry entry = new ZipEntry(filenames.get(i));
zos.putNextEntry(entry);
zos.write(pdfBytes);
zos.closeEntry();
}
zos.flush();
zos.close();
baos.flush();
map1.put("data", Base64.getEncoder().encodeToString(baos.toByteArray()));
} catch (IOException e) {
// 处理可能的异常
e.printStackTrace(); // 实际应用中应使用合适的日志记录方式
map1.put("data", null);
}
}
redisUtil.set(mapkey, map1);
}
});
} catch (Exception e) {
throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(), e.getMessage());
} finally {
poolExecutor.shutdown();
}
return id;
java
public void downloadBatchPdfZip(String markNo, HttpServletResponse response) {
String mapkey = markKey + markNo + ":";
if (!redisUtil.hasKey(mapkey)) {
throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(), "未找到对应数据!");
}
Map<String, Object> map = redisUtil.get(mapkey, Map.class);
String zipBase64 = (String) map.get("data");
if (StrUtil.isBlank(zipBase64)) {
throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(), "下载压缩包出错!");
}
try (ServletOutputStream out = response.getOutputStream()) {
byte[] bytes = Base64.getDecoder().decode(zipBase64);
//FileUtils.base64ToFile("d:\\1.zip",Base64.getEncoder().encodeToString(bytes));
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
BufferedInputStream bis = new BufferedInputStream(inputStream);
String fileName = String.format("%s_%s_全病案_%s.zip", province, name, DateUtil.format(new Date(), "yyyyMMdd"));
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
IoUtil.copy(bis, out);
} catch (IOException e) {
e.printStackTrace();
throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(), "下载压缩包失败!");
}
}
本文作者:Weee
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!