java產生二維碼,java產生

來源:互聯網
上載者:User

java產生二維碼,java產生

@RequestMapping(value="/123.html")    public void yaz(HttpServletResponse response,HttpServletRequest request) throws Exception{                request.setCharacterEncoding("UTF-8");        response.setCharacterEncoding("text/html;charset=utf-8");        int width=80;int height=40;        //畫板        BufferedImage b =new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);        //工具        Graphics g= b.getGraphics();                g.setColor(new Color(40, 200, 255));        g.fillRect(0, 0, width, height);                char[] ch="QWERTYUIOPASDFGHJKLZXCVBNM1234567890".toCharArray();        Random random = new Random();        int len = ch.length,index;        StringBuffer sb = new StringBuffer();        for (int i = 0; i < 4; i++) {            index = random.nextInt(len);            g.setColor(new Color(random.nextInt(88), random.nextInt(188), random.nextInt(255)));            g.setFont(new Font("微軟雅黑", Font.BOLD, 18));            g.drawString(ch[index]+"", i*15+3, 18);            sb.append(ch[index]);                }        //幹擾線        for(int i=1;i<10000;i++){            g.setColor(new Color(random.nextInt(88), random.nextInt(188), random.nextInt(255)));            g.drawLine(random.nextInt(i*3+2), random.nextInt(i*5+1), random.nextInt(i*3+1),random.nextInt(i*9));        }                //結束        g.dispose();        b.flush();        //儲存        request.getSession().setAttribute("yzm", sb.toString());        try {            //輸出圖片到頁面            ImageIO.write(b, "png", response.getOutputStream());        } catch (IOException e) {                    }                    }

前台頁面怎麼用?

<body><!-- 圖片的路徑直接是驗證碼的請求路徑即可 --><img alt="" src="<%=request.getContextPath()%>/123.html" id="reload" onclick="reload2()"><script type="text/javascript">function reload2(){    var time = new Date().getTime();    document.getElementById("reload").src="<%=request.getContextPath()%>/123.html?d="+time;}//為什麼請求多了一個參數呢?  是為了告訴瀏覽器這是一個不同的請求,否則驗證碼不重新整理</script></body>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.