java登入驗證碼

來源:互聯網
上載者:User

標籤:img   graph   png   verify   登入驗證   -name   void   set   idt   

一、建立web項目

  使用簡單servlet來示範java驗證碼圖片產生,servlet簡單使用參考http://www.cnblogs.com/ywlaker/p/6038676.html

二、產生驗證碼

  建立VcodeObject.java,儲存驗證碼與圖片

package com.demo.vcode;import java.io.InputStream;public class VcodeObject {private String code;private InputStream in;public VcodeObject() {}public VcodeObject(String code, InputStream in) {this.code = code;this.in = in;}//getters & setters}

  建立Vcode.java產生驗證碼與圖片

package com.demo.vcode;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Random;import javax.imageio.ImageIO;public class Vcode {public VcodeObject create() {// 產生隨機驗證碼String code = this.generateRandomVerifyCode(4);// 產生附帶驗證碼的圖片BufferedImage img = this.generateImgWithCode(code, 80, 34);InputStream in = this.putImgInStream(img);return new VcodeObject(code, in);}/** * 隨機產生驗證碼 *  * @param length * @return String */private String generateRandomVerifyCode(int length) {// 需要產生驗證碼的長度if (length <= 0 || length >= 8)length = 4;// 隨機產生字元的範圍String scope = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";StringBuffer code = new StringBuffer();Random random = new Random();for (int i = 0; i < length; i++) {code.append(scope.charAt(random.nextInt(62)));}return code.toString();}/** * 產生帶有驗證碼的圖片 *  * @param code * @param width * @param height * @return BufferedImage */private BufferedImage generateImgWithCode(String code, int width, int height) {BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics brush = img.getGraphics();// 設定背景和邊框顏色brush.setColor(this.getColor());brush.fillRect(0, 0, width - 1, height - 1);brush.drawRect(0, 0, width - 1, height - 1);// 寫入驗證碼brush.setColor(this.getColor());brush.setFont(new Font("Times New Roman", Font.ITALIC, 28));brush.drawString(code, 7, 22);// 繪製幹擾線條// 圖片畫完,關閉資源brush.dispose();return img;}/** * 擷取隨機顏色 *  * @return Color */private Color getColor() {Random random = new Random();int r = random.nextInt(255);int g = random.nextInt(255);int b = random.nextInt(255);return new Color(r, g, b);}/** * 將圖片放入輸入資料流中 *  * @param image * @return InputStream */private InputStream putImgInStream(BufferedImage image) {ByteArrayOutputStream out = new ByteArrayOutputStream();try {ImageIO.write(image, "JPEG", out);} catch (IOException e) {e.printStackTrace();}InputStream in = new ByteArrayInputStream(out.toByteArray());return in;}}
三、輸出驗證碼

  建立DemoServlet.java

package com.demo.vcode;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DemoServlet extends HttpServlet {private static final long serialVersionUID = 1L;public void init(ServletConfig config) throws ServletException {}public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException {res.setContentType("image/png");Vcode vcode = new Vcode();VcodeObject vcodeObject = vcode.create();InputStream in = vcodeObject.getIn();req.getSession().setAttribute("vcode", vcodeObject.getCode());byte[] bytes = new byte[1024];int length = 0;try {OutputStream out = res.getOutputStream();while ((length = in.read(bytes)) != -1) {out.write(bytes, 0, length);}in.close();out.flush();} catch (Exception e) {}}public void doPost(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException {}public void destroy() {}}

  配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><display-name>vcode</display-name><servlet><servlet-name>vcode</servlet-name><servlet-class>com.demo.vcode.DemoServlet</servlet-class></servlet><servlet-mapping><servlet-name>vcode</servlet-name><url-pattern>/vcode</url-pattern></servlet-mapping></web-app>

  瀏覽器地址欄訪問

http://127.0.0.1:8080/vcode/vcode

java登入驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.