生成二维码,扫码跳转连接


首先前台使用的是vue,用到的生成二维码工具类ZxingUtil

 <a v-bind:href="'${ctx}/admin/activity/activityDetailQRCode.do?recno='+item.recno"
                                               class="btn btn-danger btn-xs" v-bind:download="item.name+'.jpg'"
                                               style="padding:1px 5px;font-size: 14px;margin-top: 5px">下载活动报名二维码</a>

后台

 //生成活动报名二维码
    @RequestMapping("activityDetailQRCode")
    @ResponseBody
    public void activityDetailQRCode(HttpServletRequest request,HttpServletResponse response,Integer recno){
        String realPath = request.getServletContext().getRealPath("/");
        File dir = new File(realPath+File.separator+"activityDetailQRCode");
        if(!dir.exists()){
            dir.mkdir();
        }

        String activityQrcodeLink = dir.getAbsolutePath() + File.separator  + recno + ".jpg";
        File file = new File(activityQrcodeLink);
        if (!file.exists()) {
            try {
                file.createNewFile();
                //获取网络协议
                String networkProtocol = request.getScheme();
                //网络IP
                String ip = request.getServerName();
                //端口号
                int port = request.getServerPort();
                //项目发布路径
                String webApp = request.getContextPath();
                //url:${ctx}/mobile/activity/activityDetail.do?activityRecno=" + recno
                String activityDetailQrcodeLink = networkProtocol+"://"+ip+":"+port+webApp+"/mobile/activity/activityDetail.do?activityRecno="+recno;
                ZxingUtil.encode2DCode(file, activityDetailQrcodeLink);
            } catch (WriterException | IOException e) {
                e.printStackTrace();
            }
        }
        response.setContentType("image/jpg");
        BufferedInputStream inputStream =null;
        BufferedOutputStream outputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(file));
            outputStream = new BufferedOutputStream(response.getOutputStream());
            flushStream(inputStream, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(inputStream!=null){
                    inputStream.close();
                }
                if(outputStream!=null){
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

然后调用一个方法,刷新缓冲流

private void flushStream(InputStream fis, OutputStream os) throws IOException {//刷新字节流
int count = 0;
byte[] buffer = new byte[1024 * 8];
while ((count = fis.read(buffer)) != -1) {
os.write(buffer, 0, count);
os.flush();
}
}
打赏

免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2021 CODEPRJ.COM