Commit fae5cea5 authored by 廖旭伟's avatar 廖旭伟

批量下载附件

parent 36bdc8b5
......@@ -6,12 +6,18 @@ import com.mortals.framework.model.BaseEntity;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.service.IUser;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.utils.BeanUtil;
import com.mortals.framework.utils.ReflectUtils;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.module.apply.model.MatterApplyDatumEntity;
import com.mortals.xhx.module.apply.model.MatterApplyDatumQuery;
import com.mortals.xhx.module.apply.service.MatterApplyDatumService;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
......@@ -24,8 +30,14 @@ import com.mortals.xhx.module.apply.service.MatterApplyService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
......@@ -43,6 +55,12 @@ public class MatterApplyController extends BaseCRUDJsonBodyMappingController<Mat
@Autowired
private ParamService paramService;
@Autowired
private MatterApplyDatumService matterApplyDatumService;
@Value("${upload.path}")
@Getter
private String filePath;
public MatterApplyController(){
super.setModuleDesc( "单事项申报");
......@@ -261,4 +279,120 @@ public class MatterApplyController extends BaseCRUDJsonBodyMappingController<Mat
return ret;
}
@PostMapping("/batchDownloadFile")
public void batchDownloadFile(@RequestBody MatterApplyDatumEntity query,
HttpServletRequest request,
HttpServletResponse response) {
List<MatterApplyDatumEntity> datumEntityList = matterApplyDatumService.find(query);
//List<String> fileList = splitToLong(ids);
batchDownloadFile(datumEntityList, request, response);
}
@PostMapping("/batchDownloadAllFile")
public void batchDownloadAllFile(@RequestBody MatterApplyEntity query,
HttpServletRequest request,
HttpServletResponse response) {
List<MatterApplyDatumEntity> datumEntityList = matterApplyDatumService.find(new MatterApplyDatumQuery().applyId(query.getId()));
//List<String> fileList = splitToLong(ids);
batchDownloadFile(datumEntityList, request, response);
}
/**
* 批量下载文件
*
* @param list 批量文件下载路径集合
* @param request request
* @param response response
*/
public void batchDownloadFile(List<MatterApplyDatumEntity> list, HttpServletRequest request, HttpServletResponse response) {
//设置响应头信息
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
//设置压缩包的名字,date为时间戳
String date = DateUtils.getCurrDateTime("yyyyMMddHHmmss");
String downloadName = "压缩包" + date + ".zip";
//返回客户端浏览器的版本号、类型
String agent = request.getHeader("USER-AGENT");
try {
//针对IE或者以IE为内核的浏览器:
if (agent.contains("MSIE") || agent.contains("Trident")) {
downloadName = URLEncoder.encode(downloadName, "UTF-8");
} else {
//非IE浏览器的处理:
downloadName = new String(downloadName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
} catch (Exception e) {
log.error("系统异常", e);
}
response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");
//设置压缩流:直接写入response,实现边压缩边下载
ZipOutputStream zipOs = null;
//循环将文件写入压缩流
DataOutputStream os = null;
//文件
File file;
try {
zipOs = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
//设置压缩方法
zipOs.setMethod(ZipOutputStream.DEFLATED);
//遍历文件信息(主要获取文件名/文件路径等)
int fileNum = 1;
for (MatterApplyDatumEntity item : list) {
try {
// 文件存储路径
String path = item.getFileUrl();
// 压缩文件中每个文件的文件名称
String name = item.getFileName();
log.info(String.format("batchDownloadFile:[filePath:%s]", path));
String rootPath = this.filePath.endsWith("/") ? this.filePath : this.filePath + "/";
String filePath = rootPath + path;
file = new File(filePath);
if (!file.exists()) {
throw new RuntimeException("文件不存在");
}
FileInputStream fs = null;
try {
//添加ZipEntry,并将ZipEntry中写入文件流
zipOs.putNextEntry(new ZipEntry(name));
os = new DataOutputStream(zipOs);
fs = new FileInputStream(file);
byte[] b = new byte[100];
int length;
//读入需要下载的文件的内容,打包到zip文件
while ((length = fs.read(b)) != -1) {
os.write(b, 0, length);
}
} catch (Exception e) {
log.error("系统异常", e);
} finally {
//关闭流
assert fs != null;
fs.close();
zipOs.closeEntry();
}
} catch (Exception e) {
log.error(String.format("下载文件出错![%s]", e.getMessage()));
}
}
} catch (Exception e) {
log.error("系统异常", e);
} finally {
//关闭流
try {
if (os != null) {
os.flush();
os.close();
}
if (zipOs != null) {
zipOs.close();
}
} catch (IOException e) {
log.error("系统异常", e);
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment