i 绝望

依然

Miss Lang

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);
		
		
	}
}

  

posted on 2014-04-26 22:12  juewang  阅读(405)  评论(0编辑  收藏  举报

绝望依然

Miss Lang