友情链接1:21点 足球投注 投注网 明升体育 hg0088 葡京赌场 网上赌球 诈金花 澳门金沙网址 澳门银河网址 澳门银河 澳门银河官网 银河官网 威尼斯人 澳门威尼斯人 澳门威尼斯人官网 威尼斯人官网 金沙 澳门金沙 澳门金沙官网 金沙官网 现金网 赌球网 全讯网 明升国际 足球开户 ******官网 ******网址 现金赌博 澳门现金网 ******官方网站

Java鐢熸垚楠岃瘉鐮 | Mingo's Blog

Java鐢熸垚楠岃瘉鐮

鏃堕棿锛2012骞03鏈20鏃浣滆咃細Mingo琚洿瑙備簡锛5,642 娆璇勮娆℃暟锛0

Java鐢熸垚楠岃瘉鐮 浠g爜濡備笅锛

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.qy.pro.util.Constant;

public class CheckCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	// 楠岃瘉鐮佸浘鐗囩殑瀹藉害銆
		private int width = 60;
		// 楠岃瘉鐮佸浘鐗囩殑楂樺害銆
		private int height = 16;

		protected void service(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, java.io.IOException {
			BufferedImage buffimg = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = buffimg.createGraphics();
			// 鍒涘缓涓涓殢鏈烘暟鐢熸垚鍣ㄧ被銆
			Random random = new Random();
			g.setColor(Color.white);
			g.fillRect(0, 0, width, height);

			// 鍒涘缓瀛椾綋锛屽瓧浣撶殑澶у皬搴旇鏍规嵁鍥剧墖鐨勯珮搴︽潵瀹氥
			Font font = new Font("times new roman", Font.PLAIN, 20);
			// 璁剧疆瀛椾綋銆
			g.setFont(font);

			// 鐢昏竟妗嗐
			g.setColor(Color.white);
			g.drawRect(0, 0, width - 1, height - 1);

			// 闅忔満浜х敓10鏉″共鎵扮嚎锛屼娇鍥捐薄涓殑璁よ瘉鐮佷笉鏄撹鍏跺畠绋嬪簭鎺㈡祴鍒般
			g.setColor(Color.gray);
			for (int i = 0; i < 5; i++) {
				int x = random.nextInt(width);
				int y = random.nextInt(height);
				int xl = random.nextInt(12);
				int yl = random.nextInt(12);
				g.drawLine(x, y, x + xl, y + yl);
			}

			// randomcode鐢ㄤ簬淇濆瓨闅忔満浜х敓鐨勯獙璇佺爜锛屼互渚跨敤鎴风櫥褰曞悗杩涜楠岃瘉銆
			StringBuffer randomcode = new StringBuffer();
			int red = 0, green = 0, blue = 0;

			// 闅忔満浜х敓4浣嶆暟瀛楃殑楠岃瘉鐮併
			for (int i = 0; i < 4; i++) {
				// 寰楀埌闅忔満浜х敓鐨勯獙璇佺爜鏁板瓧銆
				String strrand = "";
//				int n = random.nextInt(123);
				int n = random.nextInt(10);
				if (n >= 65) // 濡傛灉澶т簬65鍒欒浆鎹负涓涓瓧绗
				{
					if (n > 90 && n < 97) { // 濡傛灉涓轰笉鍚堟硶鐨勫瓧绗﹀垯鍐嶈浆涓烘暟瀛
						n = random.nextInt(10);
						strrand += n;
					} else { // 鐢熸垚涓涓瓧绗
						strrand = (char) n + "";
					}
				} else {
					n = random.nextInt(10);
					strrand += n;
				}
				// 浜х敓闅忔満鐨勯鑹插垎閲忔潵鏋勯犻鑹插硷紝杩欐牱杈撳嚭鐨勬瘡浣嶆暟瀛楃殑棰滆壊鍊奸兘灏嗕笉鍚屻
				red = random.nextInt(200);
				green = random.nextInt(200);
				blue = random.nextInt(200);

				// 浜х敓闅忔満楂樺害 10鑷砲eight涔嬮棿
				float imght = 0;
				while (imght <= 13) {
					imght = Float
							.parseFloat(String.valueOf(random.nextInt(height)));
				}
				// 鐢ㄩ殢鏈轰骇鐢熺殑棰滆壊灏嗛獙璇佺爜缁樺埗鍒板浘鍍忎腑銆
				g.setColor(new Color(red, green, blue));
				g.drawString(strrand, 13 * i+10, imght);

				// 灏嗕骇鐢熺殑鍥涗釜闅忔満鏁扮粍鍚堝湪涓璧枫
				randomcode.append(strrand);
			}
			g.dispose();
			// 灏嗗洓浣嶆暟瀛楃殑楠岃瘉鐮佷繚瀛樺埌session涓傚叾閿负 validateCode
			HttpSession session = req.getSession();
			session.setAttribute(Constant.CHECK_CODE, randomcode.toString());

			// 绂佹鍥惧儚缂撳瓨銆
			resp.setHeader("pragma", "no-cache");
			resp.setHeader("cache-control", "no-cache");
			resp.setDateHeader("expires", 0);
			resp.setContentType("image/png");

			// 灏嗗浘鍍忚緭鍑哄埌servlet杈撳嚭娴佷腑銆
			ServletOutputStream sos = resp.getOutputStream();
			try{
			ImageIO.write(buffimg, "png", sos);
			}catch(Exception c){}
			sos.close();
		}

}

 

澹版槑: 鏈枃閲囩敤 BY-NC-SA 鍗忚杩涜鎺堟潈 | Mingo's Blog
杞浇璇锋敞鏄庤浆鑷Java鐢熸垚楠岃瘉鐮

鏍囩锛,鍒嗙被锛Java缂栫▼
0鏉¤瘎璁

鏆傛椂娌℃湁璇勮锛

鍙戣〃璇勮

*

*