package com;import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;import sun.misc.OSEnvironment;public class TopologyServlet extends HttpServlet { private static final long serialVersionUID = 1L; /**
 * 计数器
 */
private static int count = 0;

/**
 * 计算的周期 2s
 */
private static final int countPeriod = 2;

/**
 * 图像宽度
 */
private static int WIDTH = 800;
/**
 * 图像高度
 */
private static int HEIGHT = 400;

//@SuppressWarnings("unused")
private static boolean COLORFUL_LINK = false;

/**
 * 设置图片的宽度
 * 
 * @param w宽度的像素
 */
public void setWidth(int w) {
WIDTH = w;
} /**
 * 设置图片的高度
 * 
 * @param h高度的像素
 */
public void setHeight(int h) {
HEIGHT = h;
} /**
 * 定时执行并生成图像缓存
 */
public static void OnTimer() { //ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
count ++;

//多少计算一次
if( !(count % countPeriod ==0) ) return;

try {
COLORFUL_LINK = false; BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics();
g.setColor(Color.WHITE);// 画框
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.GRAY);// 画框
g.drawRect(0, 0, WIDTH-5, HEIGHT-5);

} catch (Exception e) {
// TODO: handle exception
}
}
}

解决方案 »

  1.   

    这个是生成验证码的代码,给你看看:
    package com.qhit;import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    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;
    import javax.servlet.http.HttpSession;import org.apache.log4j.Logger;public class ShowCode extends HttpServlet { /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static Logger log = Logger.getLogger(ShowCode.class);
    // 设置图形验证码中的字符串的字体的大小 private Font mFont = new Font("Arial Black", Font.PLAIN, 16);
    /**
     * 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 { doPost(request,response);
    } /**
     * 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 { //生成服务器相应的service方法
    //阻止生成的页面内容被缓存,保证每次重新生成随机验证码
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");

    //指定图形验证码图片的大小;
    int width = 80;//宽度
    int height = 20;//高度
    BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); //准备在图片中绘制内容
    Graphics g = image.getGraphics();
    Random random = new Random();
    g.setColor(getRandColor(200, 250));
    g.fillRect(1, 1, width - 1, height - 1);
    g.setColor(new Color(102, 102, 102));
    g.drawRect(0, 0, width - 1, height - 1);
    g.setFont(mFont); g.setColor(getRandColor(160, 200)); //生成随机线条
    for (int i = 0; i < 155; i++) {
    int x = random.nextInt(width - 1);
    int y = random.nextInt(height - 1);
    int xl = random.nextInt(6) + 1;
    int yl = random.nextInt(12) + 1;
    g.drawLine(x, y, x + xl, y + yl);
    } for (int i = 0; i < 70; i++) {
    int x = random.nextInt(width - 1);
    int y = random.nextInt(height - 1);
    int xl = random.nextInt(12) + 1;
    int yl = random.nextInt(6) + 1;
    g.drawLine(x, y, x - xl, y - yl);
    } String sRand = "";
    //生成随机的字符串并加入到图片中
    int LEN = 4; //控制随机码的长度
    for (int i = 0; i < LEN; i++) {
    String tmp = getRandomChar();
    sRand += tmp;
    g.setColor(new Color(20 + random.nextInt(110), 20 + random
    .nextInt(110), 20 + random.nextInt(110)));
    g.drawString(tmp, 15 * i + 10, 15);
    } HttpSession session = request.getSession(true); log.info("随机生成的字符串为"+sRand);
    System.out.println("随机生成的字符串为"+sRand);
    //将其自动转换为小写。也就是说用户在输入验证码的时候,不需要区分大小写,方便输入。
    session.setAttribute("code", sRand.toLowerCase());
    log.info("从session中取出来"+session.getAttribute("code"));
    g.dispose();
    ImageIO.write(image, "JPEG", response.getOutputStream());
    }

    /**
    * 生成随机颜色
    * @param fc
    * @param bc
    * @return
    */ private Color getRandColor(int fc, int bc) {
    Random random = new Random();
    if (fc > 255)
    fc = 255;
    if (bc > 255)
    bc = 255;
    int r = fc + random.nextInt(bc - fc);
    int g = fc + random.nextInt(bc - fc);
    int b = fc + random.nextInt(bc - fc);

    return new Color(r, g, b); }

    /**
    * 随机生成字符串
    * @return
    */ private String getRandomChar() {
    int rand = (int) Math.round(Math.random() * 2);
    long itmp = 0;
    char ctmp = '\u0000';
    switch (rand) {
    case 1:
    itmp = Math.round(Math.random() * 25 + 65);
    ctmp = (char) itmp;
    return String.valueOf(ctmp);
    case 2:
    itmp = Math.round(Math.random() * 25 + 97);
    ctmp = (char) itmp;
    return String.valueOf(ctmp);
    default:
    itmp = Math.round(Math.random() * 9);
    return String.valueOf(itmp);
    }
    }}