1. 程式人生 > >java指定由若干url生成二維碼png,然後打包為zip下載

java指定由若干url生成二維碼png,然後打包為zip下載

額,又應專案要求,需要將客戶配置的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(); } } } } }