From 385e869ff550731cd9591f70a90d85f857d5de2f Mon Sep 17 00:00:00 2001 From: hansha Date: Mon, 17 Jun 2024 15:58:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=BC=E5=87=BA=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E6=8E=A5=E5=8F=A3=EF=BC=8C=E4=BD=BF=E7=94=A8=E5=A4=9A?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DaPicturesRecardController.java | 122 +++++++++++++++++- .../da/framework/config/ThreadPoolConfig.java | 19 ++- 2 files changed, 137 insertions(+), 4 deletions(-) diff --git a/dangan-dangan/src/main/java/com/da/dangan/controller/DaPicturesRecardController.java b/dangan-dangan/src/main/java/com/da/dangan/controller/DaPicturesRecardController.java index 3050243..b87e72d 100644 --- a/dangan-dangan/src/main/java/com/da/dangan/controller/DaPicturesRecardController.java +++ b/dangan-dangan/src/main/java/com/da/dangan/controller/DaPicturesRecardController.java @@ -1,5 +1,6 @@ package com.da.dangan.controller; +import cn.hutool.core.util.URLUtil; import com.da.common.annotation.Log; import com.da.common.core.controller.BaseController; import com.da.common.core.domain.AjaxResult; @@ -8,12 +9,23 @@ import com.da.common.enums.BusinessType; import com.da.common.utils.poi.ExcelUtil; import com.da.dangan.domain.DaPicturesRecard; import com.da.dangan.service.IDaPicturesRecardService; +import org.apache.commons.compress.archivers.zip.ParallelScatterZipCreator; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; +import org.apache.commons.compress.parallel.InputStreamSupplier; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.*; import java.util.List; +import java.util.concurrent.*; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; /** * 档案图片信息记录Controller @@ -25,9 +37,10 @@ import java.util.List; @RequestMapping("/dangan/pictureRecard") public class DaPicturesRecardController extends BaseController { + @Autowired + private ExecutorService executorService; @Autowired private IDaPicturesRecardService daPicturesRecardService; - /** * 查询档案图片信息记录列表 */ @@ -53,6 +66,20 @@ public class DaPicturesRecardController extends BaseController util.exportExcel(response, list, "档案图片信息记录数据"); } + /** + * 导出档案图片信息记录列表 + */ + @PreAuthorize("@ss.hasPermi('dangan:pictureRecard:export')") + @Log(title = "图片下载", businessType = BusinessType.EXPORT) + @PostMapping("/exportImage") + public void export2(HttpServletRequest request,HttpServletResponse response, DaPicturesRecard daPicturesRecard) + { + List list = daPicturesRecardService.selectDaPicturesRecardList(daPicturesRecard); + List images = list.stream().map(DaPicturesRecard ::getPicUrl).collect(Collectors.toList()); + //exportImages(request,response,images,"档案图片"); + exportImages1(response,images,"档案图片"); + } + /** * 获取档案图片信息记录详细信息 */ @@ -106,4 +133,97 @@ public class DaPicturesRecardController extends BaseController { return daPicturesRecardService.deleteDaPicturesRecardByIds(ids); } + /** + * 使用多线程打包下载 + */ + private void exportImages1( HttpServletResponse response, List images, String name) { + response.setContentType("application/zip"); + response.setHeader("Content-Disposition", "attachment;filename=demo.zip"); + ParallelScatterZipCreator parallelScatterZipCreator = new ParallelScatterZipCreator(executorService); + try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(response.getOutputStream())) { + zipArchiveOutputStream.setLevel(0); + images.forEach(x -> { + ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(StringUtils.getFilename(x)); + zipArchiveEntry.setMethod(ZipArchiveEntry.STORED); + InputStreamSupplier inputStreamSupplier = () -> URLUtil.getStream(URLUtil.url(x)); + parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, inputStreamSupplier); + }); + parallelScatterZipCreator.writeTo(zipArchiveOutputStream); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void exportImages(HttpServletRequest request, HttpServletResponse response, List images, String name) { + //响应头的设置 + response.reset(); + response.setCharacterEncoding("utf-8"); + response.setContentType("multipart/form-data"); + //设置压缩包的名字 + //解决不同浏览器压缩包名字含有中文乱码的问题 + String billname ="workerCard"; + String downloadName=name+".zip"; + //返回客户端浏览器的版本号、类型 + String agent = request.getHeader("USER-AGENT"); + try { + //针对IE或者以IE为内核的浏览器处理 + if (agent.contains("MSIE")||agent.contains("Trident")){ + + downloadName=java.net.URLEncoder.encode(downloadName, "UTF-8"); + }else { + downloadName=new String(downloadName.getBytes("UTF-8"),"ISO-8859-1"); + } + }catch (Exception e) { + e.printStackTrace(); + } + response.setHeader("Content-Disposition","attachment;fileName=\"" + downloadName + "\""); + //设置压缩流 + ZipOutputStream zip=null; + + try { + zip=new ZipOutputStream(new BufferedOutputStream(response.getOutputStream())); + //设置压缩方法 + zip.setMethod(ZipOutputStream.DEFLATED); + } catch (IOException e) { + e.printStackTrace(); + } + DataOutputStream os=null; + //从数据库中取出要下载的图片路径 + + for (String image : images) { + File file = new File(image); + if (file.exists()){ + //添加ZipEntry,并ZipEntry中写入文件流 + //这里,加上i是防止要下载的文件有重名的导致下载失败 + try { + zip.putNextEntry(new ZipEntry(file.getName())); + os=new DataOutputStream(zip); + InputStream is = new FileInputStream(file); + byte[]b= new byte [1024]; + int length =0; + while ((length=is.read(b))!=-1){ + os.write(b,0,length); + } + is.close(); + //zip.closeEntry(); + } catch (IOException e) { + e.printStackTrace(); + } + } + //关闭流 + try { + if (os!=null){ + os.flush(); + os.close(); + zip.close(); + }else { + break; + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + + } } diff --git a/dangan-framework/src/main/java/com/da/framework/config/ThreadPoolConfig.java b/dangan-framework/src/main/java/com/da/framework/config/ThreadPoolConfig.java index 1aa4749..31ec1e4 100644 --- a/dangan-framework/src/main/java/com/da/framework/config/ThreadPoolConfig.java +++ b/dangan-framework/src/main/java/com/da/framework/config/ThreadPoolConfig.java @@ -5,9 +5,8 @@ import org.apache.commons.lang3.concurrent.BasicThreadFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.ThreadPoolExecutor; + +import java.util.concurrent.*; /** * 线程池配置 @@ -60,4 +59,18 @@ public class ThreadPoolConfig } }; } + /** + * ExecutorService + */ + @Bean(name = "executorService") + protected ExecutorService executorService() + { + return new ThreadPoolExecutor( + corePoolSize, + maxPoolSize, + keepAliveSeconds, + TimeUnit.MILLISECONDS, + new LinkedBlockingQueue() + ); + } }