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
}
}
}
------解决方案--------------------
这个是生成验证码的代码,给你看看:
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