生活随笔
收集整理的這篇文章主要介紹了
图片验证码代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
生成圖片驗證碼代碼
public class Code extends HttpServlet{private static final long serialVersionUID =
1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
int width=
60;
int height=
35;String suiji=
"";BufferedImageimg=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);Graphics g=img.getGraphics();g.setColor(Color.BLUE);g.drawRect(
0,
0, width, height);g.setColor(Color.PINK);g.fillRect(
1,
1, width-
2, height-
2);g.setColor(Color.BLACK);g.setFont(
new Font(
"宋體",Font.BOLD,
15));Random rd=
new Random();
int x=
10;
for(
int i=
0;i<
4;i++){String m=rd.nextInt(
10)+
"";g.drawString(m, x,
23);x+=
10;suiji+=m;}HttpSession session = req.getSession();session.setAttribute(
"suiName",suiji);
for(
int i=
0;i<
8;i++){g.drawLine(rd.nextInt(width), rd.nextInt(height), rd.nextInt(width), rd.nextInt(height));}ServletOutputStream pw = resp.getOutputStream();ImageIO.write(img,
"jpg", pw);}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {doGet(req,resp);}}
總結
以上是生活随笔為你收集整理的图片验证码代码的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。