验证码(网页中用到的)
package com.hygj.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; 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; public class GraphicsCodeServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int width=80; int height=30; response.setContentType("image/jpeg"); OutputStream output=response.getOutputStream(); BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR); Graphics gs=image.getGraphics(); //232,243,255//240,248,255//215,234,255//238,247,255//225,249,255 gs.setColor(new Color(225,249,255)); gs.setFont(new Font("黑体",Font.BOLD,20)); gs.fillRect(0, 0, width, height); //247,247,0//255,136,136//94,0,0//0,255,0//0,128,255//255,128,255//255,0,128//255,157,60//255,87,6//235,240,21 Color[] cor=new Color[10]; cor[0]=new Color(247,247,0); cor[1]=new Color(255,136,136); cor[2]=new Color(94,0,0); cor[3]=new Color(0,255,0); cor[4]=new Color(0,128,255); cor[5]=new Color(255,128,255); cor[6]=new Color(255,0,128); cor[7]=new Color(255,157,60); cor[8]=new Color(255,87,6); cor[9]=new Color(235,240,21); Random ran=new Random(); for(int i=0; i<5; i++) { //gs.setColor(Color.black); gs.setColor(cor[ran.nextInt(10)]); gs.drawString(String.valueOf(ran.nextInt(10)), 12*(i+1), 20); gs.drawLine(ran.nextInt(width), ran.nextInt(height), ran.nextInt(width), ran.nextInt(height)); } gs.dispose(); ImageIO.write(image, "jpeg",output); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
- 1
- 海绵B宝的追逐2015-07-16还行,谢谢分享
- 粉丝: 1
- 资源: 14
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于matlab实现图像处理,本程序使用背景差分法对来往车辆进行检测和跟踪.rar
- 基于matlab实现视频监控中车型识别代码,自己写的,希望和大家多多交流.rar
- sdk.config
- 基于matlab实现配电网三相潮流计算方法,对几种常用的配电网潮流计算方法进行了对比分析.rar
- 基于matlab实现配电网潮流 经典33节点 前推回代法潮流计算 回代电流 前推电压 带注释.rar
- 基于matlab实现模拟退火遗传算法的车辆调度问题研究,用MATLAB语言加以实现.rar
- 基于matlab实现蒙特卡洛的的移动传感器节点定位算法仿真代码.rar
- 华中数控系统818用户说明书
- 基于matlab实现卡尔曼滤波器完成多传感器数据融合 对多个机器人的不同传感器数据进行融合估计足球精确位置.rar
- 基于matlab实现进行简单车辆识别-车辆检测.rar