add企业端资料批量下载

This commit is contained in:
zhangqinlin
2021-09-30 10:55:25 +08:00
parent 249ac92f2e
commit ee0ba5d1e3
6 changed files with 181 additions and 107 deletions
@@ -1,8 +1,13 @@
package com.jero.project.controller;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -19,18 +24,24 @@ import com.jero.company.entity.PayContactsManagement;
import com.jero.data.entity.PayCaseCommitteeSubitem;
import com.jero.data.service.IPayCaseCommitteeSubitemService;
import com.jero.modules.message.handle.impl.EmailSendMsgHandle;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.project.common.PayProjectCommon;
import com.jero.project.entity.DownloadFileRecord;
import com.jero.project.entity.PayWorkingGroupDistributionRecord;
import com.jero.project.entity.PayWorkingGroupSubItem;
import com.jero.project.service.IPayWorkingGroupDistributionRecordService;
import com.jero.project.service.IPayWorkingGroupSubItemService;
import io.swagger.annotations.ApiImplicitParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@@ -52,6 +63,11 @@ public class PayWorkingGroupDistributionRecordController extends JeroController<
private IPayWorkingGroupSubItemService payWorkingGroupSubItemService;
@Autowired
private IPayCaseCommitteeSubitemService payCaseCommitteeSubitemService;
@Resource
private IOSSFileService iossFileService;
@Value("${jero.path.upload}")
private String upLoadPath;
/**
* 企业端-资料下载列表查询
@@ -83,6 +99,94 @@ public class PayWorkingGroupDistributionRecordController extends JeroController<
}
return Result.OK(pageList);
}
/**
* 导出下载zip(多个以逗号隔开)
*
*/
@ApiOperation(value="企业端-资料下载多个zip(多个以逗号隔开)", notes="企业端-资料下载多个zip(多个以逗号隔开")
@GetMapping(value = "/zipFile")
public void zipFile(@RequestParam(name="ids",required = true) String ids) {
ArrayList<String> fileIdList = new ArrayList<>(Arrays.asList(ids.split(",")));
ArrayList<String> objects = new ArrayList<>();
for(String id :fileIdList){
OSSFile file = iossFileService.getById(id);
objects.add(file.getUrl());
}
zipFileDownload(objects);
}
/**
* 打包下载
* @param paths 目前处理是文件绝对路径.
*/
public void zipFileDownload(List<String> paths){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
//循环将文件写入压缩流
DataOutputStream os = null;
//设置压缩流:直接写入response,实现边压缩边下载
ZipOutputStream zipos = null;
try {
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
//设置压缩包的名字
//解决不同浏览器压缩包名字含有中文时乱码的问题
String downloadName = UUID.randomUUID().toString() +".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 {
//非IE浏览器的处理:
downloadName = new String(downloadName.getBytes("UTF-8"),"ISO-8859-1");
}
} catch (Exception e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");
zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
zipos.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
for (String path : paths){
File file = new File(upLoadPath + "\\" + path);
if(file.exists()){
try {
//添加ZipEntry,并ZipEntry中写入文件流w
//这里,防止要下载的文件有重名的导致下载失败
zipos.putNextEntry(new ZipEntry("folder/"+ file.getName()));
os = new DataOutputStream(zipos);
InputStream is = new FileInputStream(file);
byte[] b = new byte[100];
int length = 0;
while((length = is.read(b))!= -1){
os.write(b, 0, length);
}
is.close();
zipos.closeEntry();
}catch (Exception e){
e.printStackTrace();
}
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if (os!= null){
os.flush();
os.close();
}
if (zipos != null){
zipos.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
/**
* 分发成员列表