驗(yàn)證碼
public class CodeImg extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int width = 110;
int height = 25;
// 在內(nèi)存中創(chuàng)建一個(gè)圖片
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 創(chuàng)建畫(huà)筆
Graphics g = img.getGraphics();
// 添加背景色
g.setColor(Color.PINK);
g.fillRect(1, 1, width - 2, height - 2);
// 添加字樣
Random rm = new Random();
g.setColor(Color.BLUE);
g.setFont(new Font("宋體", Font.BOLD | Font.ITALIC, 18));
int position = 20;
for (int i = 0; i < 4; i++) {
g.drawString(rm.nextInt(10) + "", position, 20);
position += 20;
}
// 加入干擾線
g.setColor(Color.BLACK);
for (int i = 0; i < 9; i++) {
g.drawLine(rm.nextInt(width), rm.nextInt(height), rm.nextInt(width), rm.nextInt(height));
}
// 以流的方式響應(yīng)給客戶端
ImageIO.write(img, "jpg", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者