本文介绍: 首先创建excel的临时文件,并写入。然后创建线程池,调用zipArchiveOutputStream来写入图片和excel。前言:之前实现的需求由于导出时需要的时间过于长,需要优化采用多线程的方式进行导出。
前言:之前实现的需求由于导出时需要的时间过于长,需要优化采用多线程的方式进行导出
更改之后的代码:
首先创建excel的临时文件,并写入。然后创建线程池,调用zipArchiveOutputStream来写入图片和excel
@PostMapping("/export3")
public void exportZip(HttpServletResponse response,
@RequestParam(value = "startTime",required = false)String startTime,
@RequestParam(value = "endTime",required = false)String endTime,
@RequestParam(value = "deviceName",required = false)String deviceName) throws IOException {
List<CutterImageVO> cutterImageVOList = cutterImageService.getCutterImageList(startTime,endTime,deviceName);
long start = System.currentTimeMillis();
String zipFileName = "豁口图片数据";
response.setCharacterEncoding("utf-8");
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName + ".zip", "UTF-8"));
//创建excel临时文件
final File tempFile = File.createTempFile("tempExcel", ".xls");
EasyExcel.write(tempFile.getCanonicalPath(),CutterImageVO.class)
.excelType(ExcelTypeEnum.XLS)
.registerWriteHandler(new ExcelHyperlinkHandler(1, new int[]{7}))
.sheet("豁口图片数据")
.doWrite(cutterImageVOList);
logger.info("临时文件所在的本地路径:" + tempFile.getCanonicalPath());
ExecutorService executor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), new MyRejectedExecutionHandler());
ParallelScatterZipCreator parallelScatterZipCreator = new ParallelScatterZipCreator(executor);
OutputStream outputStream = response.getOutputStream();
ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputStream);
zipArchiveOutputStream.setEncoding("UTF-8");
try {
for (int i = 0; i <= cutterImageVOList.size(); i++) {
int finalI = i;
String fileName = null;
if (finalI == cutterImageVOList.size()) {
fileName = "豁口图片数据.xls";
} else {
fileName = cutterImageVOList.get(i).getImageUrl();
}
String finalFileName = fileName;
final InputStreamSupplier inputStreamSupplier = () -> {
try {
InputStream inputStream = null;
if (finalI == cutterImageVOList.size()) {
inputStream = new FileInputStream(tempFile.getCanonicalPath());
} else {
inputStream = minioUtil.getDownloadInputStream(finalFileName);
}
return inputStream;
} catch (Exception e) {
e.printStackTrace();
return new NullInputStream(0);
}
};
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(fileName);
zipArchiveEntry.setMethod(ZipArchiveEntry.DEFLATED);
zipArchiveEntry.setUnixMode(UnixStat.FILE_FLAG | 436);
parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, inputStreamSupplier);
}
parallelScatterZipCreator.writeTo(zipArchiveOutputStream);
}catch(Exception e){
log.error("文件流读取失败",e);
e.printStackTrace();
}finally {
IOUtils.closeQuietly(zipArchiveOutputStream);
IOUtils.closeQuietly(outputStream);
}
//程序退出时删除临时文件
tempFile.deleteOnExit();
long end = System.currentTimeMillis();
log.info("耗时:"+(end-start));
}
Minio工具类
public InputStream getDownloadInputStream(String fileName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
InputStream is = client.getObject(GetObjectArgs.builder()
.bucket(minioConfig.getBucketName())
.object(fileName)
.build());
return is;
}
原文地址:https://blog.csdn.net/qq_41481924/article/details/135990169
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_67523.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。