生成用户登录页面中验证码图片的Servlet

代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.qixin.web;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
*
@author qixin
*/
public class ImageExample extends HttpServlet {
//用于生成随机数
private Random rand;
//显示数字所用的字体
private Font font;
//图片的宽和高
private int width;
private int height;

/**
* 传入的参数为输出流。该方法生成图片并将数据写入输出流
*
@param out
*
@throws IOException
*/
private void outputImage(OutputStream out) throws IOException{
//生成1000-9999的随机数并转为字符串
String str = Integer.toString(1000 + rand.nextInt(9000));
//在内存中建立一个指定宽、高的图像
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//随机填入20个白色的像素点
for (int i = 0; i < 20; i++) {
int x = rand.nextInt(width);
int y = rand.nextInt(height);
bi.setRGB(x, y,
0xFFFFFF);
}
//获取该对象用于画字符图形
Graphics g = bi.getGraphics();
g.setFont(font);
//画字符。注意后两个参数指定的是第一个字符的左下角坐标
g.drawString(str, 0, height-1);
//Graphics用完后立刻释放资源,避免等到垃圾回收时才进行。可减少资源占用率
g.dispose();
//将图像数据用jpg方式发送到输出流,该方法可能抛出IOException
ImageIO.write(bi, "jpg", out);
}

@Override
public void init(){
rand
= new Random(System.currentTimeMillis());
font
= new Font("Courier New",Font.ITALIC,20);
width
=50;
height
=20;
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
*
@param request servlet request
*
@param response servlet response
*
@throws ServletException if a servlet-specific error occurs
*
@throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置MIME类型为jpeg图像
response.setContentType("image/jpeg");
//获取二进制输出流,并传给生成图像的方法以返回数据
outputImage(response.getOutputStream());
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
@param request servlet request
*
@param response servlet response
*
@throws ServletException if a servlet-specific error occurs
*
@throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
@param request servlet request
*
@param response servlet response
*
@throws ServletException if a servlet-specific error occurs
*
@throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
@return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}
// </editor-fold>

}
原文地址:https://www.cnblogs.com/qixin622/p/1773364.html