添加导出图片接口,使用多线程

master
hansha 2 years ago
parent 2e06e39c2a
commit 385e869ff5

@ -1,5 +1,6 @@
package com.da.dangan.controller; package com.da.dangan.controller;
import cn.hutool.core.util.URLUtil;
import com.da.common.annotation.Log; import com.da.common.annotation.Log;
import com.da.common.core.controller.BaseController; import com.da.common.core.controller.BaseController;
import com.da.common.core.domain.AjaxResult; 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.common.utils.poi.ExcelUtil;
import com.da.dangan.domain.DaPicturesRecard; import com.da.dangan.domain.DaPicturesRecard;
import com.da.dangan.service.IDaPicturesRecardService; 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.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List; import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/** /**
* Controller * Controller
@ -25,9 +37,10 @@ import java.util.List;
@RequestMapping("/dangan/pictureRecard") @RequestMapping("/dangan/pictureRecard")
public class DaPicturesRecardController extends BaseController public class DaPicturesRecardController extends BaseController
{ {
@Autowired
private ExecutorService executorService;
@Autowired @Autowired
private IDaPicturesRecardService daPicturesRecardService; private IDaPicturesRecardService daPicturesRecardService;
/** /**
* *
*/ */
@ -53,6 +66,20 @@ public class DaPicturesRecardController extends BaseController
util.exportExcel(response, list, "档案图片信息记录数据"); 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<DaPicturesRecard> list = daPicturesRecardService.selectDaPicturesRecardList(daPicturesRecard);
List<String> 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); return daPicturesRecardService.deleteDaPicturesRecardByIds(ids);
} }
/**
* 使线
*/
private void exportImages1( HttpServletResponse response, List<String> 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<String> 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();
}
}
}
} }

@ -5,9 +5,8 @@ import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.*;
import java.util.concurrent.ThreadPoolExecutor;
/** /**
* 线 * 线
@ -60,4 +59,18 @@ public class ThreadPoolConfig
} }
}; };
} }
/**
* ExecutorService
*/
@Bean(name = "executorService")
protected ExecutorService executorService()
{
return new ThreadPoolExecutor(
corePoolSize,
maxPoolSize,
keepAliveSeconds,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()
);
}
} }

Loading…
Cancel
Save