java web 程序---登陆的验证码实现显示

是一个java文件

package com.sss;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
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 com.sun.org.apache.commons.digester.rss.Image; public class Love2 extends HttpServlet { @Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("image/jpeg");//类型---->一个验证码的图片
BufferedImage image=new BufferedImage(120, 30, BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g.setColor(Color.yellow);
g.setColor(new Color(0xFF7F00));
g.setFont(new Font("",Font.BOLD,34));
g.drawRect(1, 1, 100, 50);
String str=run()+run()+run()+run()+run();
g.drawString(str, 3, 30);
HttpSession session=request.getSession();
session.setAttribute("yan", str);
ImageIO.write(image, "jpeg",response.getOutputStream() ); } public String run(){
Random r=new Random();
int x=r.nextInt(3);
char c='\u0000';
switch(x){
case 0:
c=(char) (r.nextInt(10)+48);break;
case 1:
c=(char)(r.nextInt(10)+97);break;
case 2:
c=(char)(r.nextInt(10)+65);break; }
return String.valueOf(c); }
}

  

上一篇:在Visual Studio 中使用git——给Visual Studio安装 git插件(二)


下一篇:Visual Studio 禁用诊断工具