/**
* 机构名称批量生成二维码下载
*
* @param request 请求对象
* @param response 返回对象
* @param ids 机构主键ID
*/
@Override
public void downloadQrCodeZip(HttpServletRequest request, HttpServletResponse response, String ids) {
ZipOutputStream zos = null;
try {
// 查询机构名称
List<Map<String, Object>> institutionList = qryInstitutionByIds(ids);
if (!CommonUtil.isEmpty(institutionList) && institutionList.size() > 0) {
//压缩包文件名称
String downloadFilename = "活动机构扫描二维码";
// 指明response的返回对象是文件流
response.setContentType("application/octet-stream");
// 设置在下载框默认显示的文件名
String agent = request.getHeader("USER-AGENT");
// 解决火狐浏览器下载文件中文名乱码问题
if (agent != null && agent.toLowerCase().indexOf("firefox") > 0) {
downloadFilename = "=?UTF-8?B?" + (new String(Base64Util.encode(downloadFilename.getBytes("UTF-8")))) + "?=";
response.setHeader("Content-disposition", "attachment; filename=" + downloadFilename.concat(".zip"));
}
// 解决Safari浏览器下载文件中文名乱码问题
else if (agent != null && agent.toLowerCase().indexOf("safari") > 0) {
response.setHeader("Content-disposition", "attachment; filename="
+ (new String(downloadFilename.getBytes("UTF-8"), "ISO-8859-1")).concat(".zip"));
} else {
response.setHeader("Content-disposition", "attachment; filename="
+ URLEncoder.encode(downloadFilename.concat(".zip"), "UTF-8"));
}
zos = new ZipOutputStream(response.getOutputStream());
for (Map<String, Object> map : institutionList) {
StringBuilder qrCode = new StringBuilder(""); // 扫描二维码图片获取对应机构编码
StringBuilder qrCodeName = new StringBuilder(""); // 二维码图片名称
// 开始拼接机构编码
qrCode.append((CommonUtil.isEmpty(map.get("companycode")) ? "##" : String.valueOf(map.get("companycode"))).concat("_")
+ (CommonUtil.isEmpty(map.get("provincecode")) ? "##" : String.valueOf(map.get("provincecode"))).concat("_")
+ (CommonUtil.isEmpty(map.get("citycode")) ? "##" : String.valueOf(map.get("citycode"))).concat("_")
+ (CommonUtil.isEmpty(map.get("countycode")) ? "##" : String.valueOf(map.get("countycode"))).concat("_")
+ (CommonUtil.isEmpty(map.get("sitecode")) ? "##" : String.valueOf(map.get("sitecode"))).concat("_")
+ (CommonUtil.isEmpty(map.get("activitysession")) ? "##" : String.valueOf(map.get("activitysession"))).concat("_")
+ map.get("activityid"));
// 开始拼接机构名称
if (!CommonUtil.isEmpty(map.get("companyname"))) {
qrCodeName.append(String.valueOf(map.get("companyname")).concat("_"));
}
if (!CommonUtil.isEmpty(map.get("provincename"))) {
qrCodeName.append(String.valueOf(map.get("provincename")).concat("_"));
}
if (!CommonUtil.isEmpty(map.get("cityname"))) {
qrCodeName.append(String.valueOf(map.get("cityname")).concat("_"));
}
if (!CommonUtil.isEmpty(map.get("countyname"))) {
qrCodeName.append(String.valueOf(map.get("countyname")).concat("_"));
}
if (!CommonUtil.isEmpty(map.get("sitename"))) {
qrCodeName.append(String.valueOf(map.get("sitename")).concat("_"));
}
if (!CommonUtil.isEmpty(map.get("activitysession"))) {
qrCodeName.append("场次" + String.valueOf(map.get("activitysession")).concat("_"));
}
qrCodeName = new StringBuilder(qrCodeName.substring(0, qrCodeName.length() - 1));
// 生成扫描机构二维码图片名称
zos.putNextEntry(new ZipEntry(qrCodeName + ".png"));
// 生成扫描机构二维码图片
getQrCodeImg(qrCode.toString(), zos);
}
zos.close();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (WriterException e) {
e.printStackTrace();
} finally {
try {
if (!CommonUtil.isEmpty(zos)) {
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
log.error("ZipOutputStream资源释放失败,原因:" + e.getMessage());
}
}
}
/**
* 根据机构主键ID组查询机构名称集合
*
* @param ids
* @return
*/
private List<Map<String, Object>> qryInstitutionByIds(String ids) {
try {
// 拼装id主键条件参数
String[] idArr = ids.split(",");
ids = "";
for (String id : idArr) {
ids += id + ",";
}
ids = ids.substring(0, ids.length() - 1);
// 查询脚本
StringBuilder sb = new StringBuilder("SELECT " +
" i.*," +
" c.institutionname AS companyname," +
" c1.institutionname provincename," +
" c2.institutionname cityname," +
" c3.institutionname countyname," +
" c4.institutionname sitename " +
"FROM " +
" institutionactivity i " +
" LEFT JOIN " +
" institutioncodetable c ON c.institutioncode = i.companycode " +
" LEFT JOIN " +
" institutioncodetable c1 ON c1.institutioncode = i.provincecode " +
" LEFT JOIN " +
" institutioncodetable c2 ON c2.institutioncode = i.citycode " +
" LEFT JOIN " +
" institutioncodetable c3 ON c3.institutioncode = i.countycode " +
" LEFT JOIN " +
" institutioncodetable c4 ON c4.institutioncode = i.sitecode " +
"WHERE " +
" i.id IN (" + ids + ")");
// List<Map<String, Object>> list = (List<Map<String, Object>>) institutionactivityDao
// .getList(sb.toString(), new Object[]{ids});
List<Map<String, Object>> list = (List<Map<String, Object>>) institutionactivityDao
.getList(sb.toString());
if (!CommonUtil.isEmpty(list) && list.size() > 0) {
return list;
}
return null;
} catch (Exception e) {
e.printStackTrace();
log.error("根据机构主键ID组查询机构名称集合失败,原因:" + e.getMessage());
return null;
}
}
/**
* 生成二维码
*
* @param content 扫描二维码显示内容
* @param os 流
* @throws WriterException
* @throws IOException
*/
private void getQrCodeImg(String content, OutputStream os) throws WriterException, IOException {
//二维码参数
int width = 200; // 图像宽度
int height = 200; // 图像高度
String format = "png";// 图像类型
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix;
bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, format, os);
}