java 生产图片验证码

package com.chiataicloud.servlert;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.security.auth.Subject;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RandImg extends HttpServlet {

private String drawImg(ByteArrayOutputStream output){
    String code = "";
    for(int i=0; i<4; i++){
        code += randomChar();
    }
    int width = 70;
    int height = 25;
    BufferedImage bi = new                          BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
    Font font = new Font("Times New Roman",Font.PLAIN,20);
    Graphics2D g = bi.createGraphics();
    g.setFont(font);
    Color color = new Color(66,2,82);
    g.setColor(color);
    g.setBackground(new Color(226,226,240));
    g.clearRect(0, 0, width, height);
    FontRenderContext context = g.getFontRenderContext();
    Rectangle2D bounds = font.getStringBounds(code, context);
    double x = (width - bounds.getWidth()) / 2;
    double y = (height - bounds.getHeight()) / 2;
    double ascent = bounds.getY();
    double baseY = y - ascent;
    g.drawString(code, (int)x, (int)baseY);
    g.dispose();
    try {
        ImageIO.write(bi, "jpg", output);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return code;
}

private char randomChar(){
    Random r = new Random();
    String s = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789";
    return s.charAt(r.nextInt(s.length()));
}





/**
 * Constructor of the object.
 */
public RandImg() {
    super();
}

/**
 * Destruction of the servlet. <br>
 */
public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
}

/**
 * The doDelete method of the servlet. <br>
 *
 * This method is called when a HTTP delete request is received.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doDelete(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    // Put your code here
}

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("RanImg begin  doGet");
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    String code = drawImg(output);

    request.getSession().setAttribute("logocode", code);
    System.out.println("request.getSession().getAttribute(logocode) :" +request.getSession().getAttribute("logocode"));


    try {
        ServletOutputStream out = response.getOutputStream();
        output.writeTo(out);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

/**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {


}


/**
 * Returns information about the servlet, such as 
 * author, version, and copyright. 
 *
 * @return String information about this servlet
 */
public String getServletInfo() {
    return "This is my default servlet created by Eclipse";
}

/**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
public void init() throws ServletException {
    // Put your code here
}

}

前段页面jsp调用格式

>  <div class="form-group"  style="padding-left:10px;
> padding-right:10px">
>     <img id="randimg" src="RandImg" align="absbottom"/>&nbsp;<a href="javascript:refresh();">看不清?换一个</a>   </div>

`

function refresh()
{
$(“#randimg”).attr(“src”,”RandImg?time=”+ new Date().getTime());
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_29956725/article/details/52287980