代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
实现简单汉字验证码
作者:
小蘑菇炖小鸡
/ 发布于
2015/2/15
/
525
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; public class ChineseCheckCode extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.setContentType("image/jpeg"); response.setDateHeader("expries", -1); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); int width=166; int height = 80; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics g=image.getGraphics(); Random randome = new Random(); Font myFont = new Font("宋体",Font.ITALIC,26); g.fillRect(0, 0, width, height); g.setFont(myFont); String sRand = ""; String ctmp = ""; String itmp = ""; for(int i=0;i<4;i++){ String[] rBase={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; int r1=randome.nextInt(3)+11; String str_r1=rBase[r1]; int r2; if(r1==13){ r2=randome.nextInt(7); }else{ r2=randome.nextInt(16); } String str_r2=rBase[r2]; int r3=randome.nextInt(6)+10; String str_r3=rBase[r3]; int r4; if(r3==10){ r4=randome.nextInt(15)+1; }else if(r3==15){ r4=randome.nextInt(15); }else{ r4=randome.nextInt(16); } String str_r4=rBase[r4]; byte[] bytes = new byte[2]; String r12=str_r1+str_r2; int tempLow=Integer.parseInt(r12, 16); bytes[0]=(byte) tempLow; String r34=str_r3+str_r4; int tempHeigh=Integer.parseInt(r34, 16); bytes[1]=(byte) tempHeigh; ctmp=new String(bytes); sRand+=ctmp; Color color = new Color(20+randome.nextInt(110)); g.setColor(color); g.drawString(ctmp, width/6*i+23, height/3); } HttpSession session=request.getSession(true); session.setAttribute("randCheckCode", sRand); g.dispose(); ImageIO.write(image, "JPEG", response.getOutputStream()); } }
试试其它关键字
验证码
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
小蘑菇炖小鸡
贡献的其它代码
(
1
)
.
实现简单汉字验证码
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3