java后台文件下载打包成压缩包
1.service
public List<Map<String,List>> downDpStyleImgList(List taskIds) throws Exception {
List<Map<String,List>> resultList = new LinkedList<>();
List<List<BufferedImage>> listList = new LinkedList<>();
//获取任务包下详细信息
for (int i = 0; i < taskIds.size(); i++) {
List<BufferedImage> list = new ArrayList<>();
List<InputStream> inputStreamList = new ArrayList<>();
List<String> taskNameList = new ArrayList<>();
List<String> houseNameList = new ArrayList<>();
Map<String,List> result = new HashMap<>(4);
//获取页面勾选的每个任务包下的门牌地址数据
Long taskId = taskIds.get(i);
//任务包信息
TaskPackage taskPackage = taskPackageMapper.selectById(taskId);
//任务包名取新的任务包名称
taskNameList.add(taskPackage.getNewTaskPackageName());
//查询条件
QueryWrapper<HouseAddress> houseAddressQueryWrapper = new QueryWrapper<>();
houseAddressQueryWrapper.eq("audit_status",HouseAddressConstants.AUDIT_STATUS_AUDIT);
houseAddressQueryWrapper.eq("recommend_addr",HouseAddressConstants.RESERVE_YEW);
houseAddressQueryWrapper.eq("task_id",taskId);
houseAddressQueryWrapper.in("status",HouseAddressConstants.STATUS_PRODUCER,HouseAddressConstants.STATUS_BEFOREPRODUCER);
houseAddressQueryWrapper.orderByAsc("num_code");
houseAddressQueryWrapper.in("task_status",HouseAddressConstants.TASK_STATUS_NOPRODUCER,HouseAddressConstants.TASK_STATUS_PRODUCER,HouseAddressConstants.TASK_STATUS_PRODUCER_FINISH);
houseAddressQueryWrapper.ne("operation_type",HouseAddressConstants.OPERATION_TYPE_DELETE);
List<HouseAddress> houseAddressList = this.list(houseAddressQueryWrapper);
if(CollectionUtils.isNotEmpty(houseAddressList)){
for (HouseAddress house:houseAddressList) {
Map<String,String> map = new HashMap<>(4);
String stdId = house.getStdId();
DpStyleDto dpStyleDto = getDpStyle(stdId);
if(dpStyleDto != null){
String type = dpStyleDto.getDoorClass();
//查询条件
QueryWrapper<DpStyleConfig> dpStyleConfigQueryWrapper = new QueryWrapper<>();
dpStyleConfigQueryWrapper.eq("name", type);
DpStyleConfig dpStyleConfig = dpStyleConfigMapper.selectOne(dpStyleConfigQueryWrapper);
getStyleMap(house, map, dpStyleDto, type, dpStyleConfig);
BufferedImage bufferedImage= HtmlImageGeneratorUtil.graphicsHtmlGeneration(map,type,house.getDoorNum());
list.add(bufferedImage);
if(bufferedImage != null){
//BufferedImage 转 InputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
ImageIO.write(bufferedImage, "png", imageOutput);
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
long length = imageOutput.length();
inputStreamList.add(inputStream);
}
houseNameList.add(house.getDoorNum());
}
}
}
result.put("taskName",taskNameList);
result.put("InputStream",inputStreamList);
result.put("houseNameList",houseNameList);
resultList.add(result);
}
return resultList;
}
2.controller
@RequestMapping(value = “/downDpStyleImgListNew”,method = RequestMethod.POST )
public void downDpStyleImgListNew(@RequestParam(value = “list”)List list ,HttpServletRequest request, HttpServletResponse response) throws Exception {
List<Map<String, List>> lists = houseAddressService.downDpStyleImgList(list);
String outputFileName = "门牌样式" + System.currentTimeMillis()+ ".zip";
//名称处理即可解决文件名下载后乱码的问题
String userAgent = request.getHeader("User-Agent");
// 针对IE或者以IE为内核的浏览器:
if (userAgent.contains(CommonConstants.MSIE) || userAgent.contains(CommonConstants.TRIDENT)) {
outputFileName = java.net.URLEncoder.encode(outputFileName, "UTF-8");
} else {
// 非IE浏览器的处理:
outputFileName = new String(outputFileName.getBytes("UTF-8"), "ISO-8859-1");
}
// 设置response参数
response.setContentType("content-type:octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((outputFileName).getBytes(), "UTF-8"));
ServletOutputStream out = response.getOutputStream();
if(CollectionUtils.isNotEmpty(lists)){
ZipOutputStream zous = new ZipOutputStream (out);
try {
for (Map<String, List> map:lists) {
List<String> taskNameList = map.get("taskName");
List<String> houseNameList = map.get("houseNameList");
List<InputStream> inputStreamList = map.get("InputStream");
if(CollectionUtils.isNotEmpty(inputStreamList)){
for (int i = 0; i < inputStreamList.size(); i++) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStreamList.get(i).read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
if (baos != null) {
baos.flush();
}
byte[] bytes = baos.toByteArray();
//设置文件名
ZipEntry entry = new ZipEntry(taskNameList.get(0)+ File.separator+ houseNameList.get(i)+i+ System.currentTimeMillis()+ ".png");
zous.putNextEntry(entry);
zous.write(bytes);
zous.closeEntry();
if (baos != null) {
baos.close();
}
}
}
}
}catch (IOException e) {
logger.error(e.getMessage(),e);
}finally{
if (zous != null) {
zous.close();
}
}
}
}