额,又应项目要求,需要将客户配置的url生成二维码,然后打包为zip下载
上一篇是根据url生成图片打包下载,此篇仅仅多个根据url生成二维码
我沉默,话不多,赢的时候才开口.上代码
//公共方法根据url生成二维码图片后写入输出流里
public static void getBarCodeImgByUrl(String url,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(url,BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, format, os);
}
//入口
@RequestMapping("download")
public void download(HttpServletRequest request, HttpServletResponse response,BcMerchantAccount userInfo,String identy){
//通过活动标识和商户id查询活动
List<info> infoList = xxService.getInfoList(XX,XX);
if(infoList != null && infoList.size()>0){
ZipOutputStream zos = null;
try {
String downloadFilename = infoList.get(0).getfileName();//文件的名称
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename+".zip");// 设置在下载框默认显示的文件名
zos = new ZipOutputStream(response.getOutputStream());
for(info info:infoList ){
zos.putNextEntry(new ZipEntry(info.getBarCode_name()+".png"));//命名
getBarCodeImgByUrl(info.getUrl, zos);//拼接了url
}
zos.flush();
zos.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (WriterException e) {
e.printStackTrace();
} finally{
if(zos != null){
try {
zos.flush();
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}