下载身份证正反面图片到压缩包

	@ApiOperation("下载身份证图片到压缩包")
	@ApiImplicitParams({ @ApiImplicitParam(name = "ids", value = "多个id用,分隔", dataType = "String") })
	@RequestMapping(value = "/downloadPicZip", method = RequestMethod.GET)
	public Result downloadPicZip(HttpServletRequest request, HttpServletResponse response, String ids)
			throws IOException {
		// if(null == ids || StringUtils.isBlank(ids)){
		// throw new BusinessException(BizExceptionEnum.PARAM_LOSS);
		// }
		try {
			List> IdCardPicList = partnerUserService.getIdCardPic(ids);
			String downloadFilename = "身份证正反面图片" + DateUtils.formatDate(new Date(), DateUtils.FORMAT_8) + ".zip";// 文件的名称
			downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");// 转换中文否则可能会产生乱码
			response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
			response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
			ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
			for (int i = 0; i < IdCardPicList.size(); i++) {
				if (null == IdCardPicList.get(i)) {
					continue;
				} else {
					Map idCardPic = IdCardPicList.get(i);
					String id_card_front = MapUtils.getString(idCardPic, "id_card_front");
					String id_card_contrary = MapUtils.getString(idCardPic, "id_card_contrary");
					String mobile = MapUtils.getString(idCardPic, "mobile");
					String partnerName = MapUtils.getString(idCardPic, "partner_name");
					InputStream fis = null;
					if (!StringUtil.isEmptyStr(id_card_front)) {
						zos.putNextEntry(new ZipEntry(partnerName + "-" + mobile + "-正面" + ".jpg"));
						fis = FileUtil.getInputStreamByGet(id_card_front);
						if (null == fis) {
							break;
						} else {
							byte[] buffer = new byte[1024];
							int r = 0;
							while ((r = fis.read(buffer)) != -1) {
								zos.write(buffer, 0, r);
							}
							fis.close();
						}
					}
					if (!StringUtil.isEmptyStr(id_card_contrary)) {
						zos.putNextEntry(new ZipEntry(partnerName + "-" + mobile + "-反面" + ".jpg"));
						fis = FileUtil.getInputStreamByGet(id_card_contrary);
						if (null == fis) {
							break;
						} else {
							byte[] buffer = new byte[1024];
							int r = 0;
							while ((r = fis.read(buffer)) != -1) {
								zos.write(buffer, 0, r);
							}
							fis.close();
						}
					}
					zos.flush();
				}
			}
			zos.close();
		} catch (BusinessException be) {
			log.error("捕获业务异常: {}", be);
			return new Result(be.getCode(), be.getMessage());
		} catch (Exception e) {
			log.error("发生异常情况异常类型{}", e);
			return new Result(ResultCode.SERVER_ERROR.getCode(), "系统异常");
		}
		return null;
	}

 

你可能感兴趣的:(下载身份证正反面图片到压缩包)