生成二维码图片,并保存压缩包到本地
需求:之前写过生成二维码到excel中,客户有要求生成的一张张图片到一个压缩包,方便打印~
/**
	 *
	 * 生成二维码图片压缩输出到客户端
	 * @param request
	 * @param response
	 * @throws WriterException
	 * @throws IOException
	 */
	@RequestMapping(params = "exportQrcodeZIP")
	public void exportQrcodeZIP(TDProduct tDProduct,
			HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid) throws WriterException, IOException {
		ZipOutputStream zipos = null;
		//压缩文件名
		String zipName = "二维码";
		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment;filename="
				+ new String((zipName+ ".zip").getBytes(), "iso-8859-1"));
		// 根据浏览器进行转码,使其支持中文文件名
		if (BrowserUtils.isIE(request)) {
			response.setHeader(
					"content-disposition",
					"attachment;filename="
							+ java.net.URLEncoder.encode(zipName, "UTF-8")
							+ ".zip");
		} else {
			String newtitle = new String(zipName.getBytes("UTF-8"),
					"ISO8859-1");
			response.setHeader("content-disposition",
					"attachment;filename=" + newtitle + ".zip");
		}
		
		
		
		try {
			CriteriaQuery cq = new CriteriaQuery(TDProduct.class, dataGrid);
			org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil
					.installHql(cq, tDProduct, request.getParameterMap());
			TSUser user = ResourceUtil.getSessionUserName();// 获取当前登录用户信息
			boolean isMerchant = systemService.isMerchantRole();
			if (isMerchant) {
				cq.eq("merchantId", user.getId());
			}
			cq.eq("deviceMode", Constants.DEVICE_TYPE.NONLINE);
			cq.eq("statusid", "6");
			cq.isNull("productdate");//激活日期为空
			cq.add();
			List<TDProduct> tDProducts = this.systemService
					.getListByCriteriaQuery(cq, Boolean.valueOf(false));
			MerchantEntity merchantEntity = systemService.getEntity(
					MerchantEntity.class, user.getId());// 表示当前登录的这个用户的一个
			String referer = request.getHeader("REFERER");
			String path = referer.substring(0,
					referer.indexOf("productController"));
			// 如果客户使用的是wpiot这个域名,则更换为wateriot
			if (path.substring(7, 12).equals("wpiot")) {
				path = "http://wateriot.huamar.com/wateriot/";
			}
			String redi_url = "要生成二维码的链接地址";//要生成二维码的链接地址,自己的项目就隐藏了,可以把自己的放上面
			String[] str = new String[tDProducts.size()];
			if (merchantEntity != null) {
				for (TDProduct tdProd : tDProducts) {
					if (null != merchantEntity.getIshref()
							&& 1 == merchantEntity.getIshref()) {
						// 中间跳转
						redi_url = merchantEntity.getUrl().toString()
								+ "?appid=APPID" + "&redirect_uri=REDIRECTURI"
								+ "&response_type=code&scope=snsapi_userinfo"
								+ "&state=STATE#wechat_redirect";
						if (StringUtil.isNotEmpty(merchantEntity.getDomain())) {
							redi_url = redi_url.replaceAll("REDIRECTURI",
									merchantEntity.getDomain()
											+ "/wateriot/oauth/uniotBind");
						} else {
							redi_url = redi_url.replaceAll("REDIRECTURI", path
									+ "oauth/uniotBind");
						}
					} else {
						if (StringUtil.isNotEmpty(merchantEntity.getDomain())) {
							redi_url = redi_url.replaceAll("REDIRECTURI",
									merchantEntity.getDomain()
											+ "/wateriot/oauth/uniotBind");
						} else {
							redi_url = redi_url.replaceAll("REDIRECTURI", path
									+ "oauth/uniotBind");
						}
					}
					redi_url = redi_url.replaceAll("APPID",
							merchantEntity.getAppid());
					String state = merchantEntity.getAppid() + ","
							+ merchantEntity.getSecret() + "," + tdProd.getId();
					redi_url = redi_url.replaceAll("STATE", state);
					logger.info("---------------------" + redi_url);
					tdProd.setQrode(redi_url);
				}
				for (int i = 0; i < tDProducts.size(); i++) {
					str[i] = tDProducts.get(i).getQrode();
				}
			}
              //---------------压缩开始---------------------------------
			zipos = new ZipOutputStream(response.getOutputStream());
			for (int k = 0; k < tDProducts.size(); k++) {
				// image是生成的图片
				String content1 = tDProducts.get(k).getQrode();
				Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
				// 指定纠错等级
				hints.put(EncodeHintType.ERROR_CORRECTION,
						ErrorCorrectionLevel.M);
				// 指定编码格式
				hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
				BitMatrix bitMatrix = new MultiFormatWriter().encode(content1,
						BarcodeFormat.QR_CODE, 400, 400, hints);
				BufferedImage image = MatrixToImageWriter.writeToFile(
						bitMatrix, "jpeg");
				BufferedImage bufferImg = image;// 图片一
				try {
					// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
					ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
					ImageIO.write(bufferImg, "jpeg", byteArrayOut);
					// 设置文件名
					String fileName = "";
					fileName = tDProducts.get(k).getId() + ".jpg";
					zipos.putNextEntry(new ZipEntry(fileName));
					zipos.write(byteArrayOut.toByteArray());
					zipos.closeEntry();
				} catch (Exception e) {
				}
			}
			zipos.finish();
		} catch (IOException io) {
			io.printStackTrace();
			System.out.println("io erorr : " + io.getMessage());
		} finally {
			if (zipos != null) {
				try {
					zipos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
		} catch (IOException io) {
			io.printStackTrace();
			System.out.println("io erorr : " + io.getMessage());
		} finally {
			if (zipos != null) {
				try {
					zipos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
                    
                
                
            
        
浙公网安备 33010602011771号