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

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
/*
* 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/archive/2010/07/08/1773364.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值